Attribute Macro odoo_api

Source
#[odoo_api]
Expand description

Implement traits for an “API” method struct

The input should be a struct with named field. Zero-sized structs and generics are supported.

Arguments:

  • Service: The Odoo “service” for this method
  • Method: The method name
  • Auth: Whether authentication is required, optional, or ignored

For example, consider the following:

// service: "object"
// method: "execute"
// auth: "yes"
#[derive(Debug, Serialize)]
#[odoo_api("object", "execute", "yes")]
struct Execute {
    database: String,
    uid: OdooId,
    password: String,

    model: String,
    method: String,
    args: Vec<Value>
}

Then the following impls will be generated:

// The `Execute` is able to be used as JSON-RPC `params`
impl JsonRpcParams for Execute {
    // Set the container; in this case, the container will use `describe` below
    // to implement a custom Serialize
    type Container<T> = OdooApiContainer<Self>;
    type Response = ExecuteResponse;

    fn build(self) -> JsonRpcRequest<Self> { self._build() }
}

// Which JSON-RPC service and method does `Execute` belong to?
impl OdooApiMethod for Execute {
    fn describe(&self) -> (&'static str, &'static str) {
        ("object", "execute")
    }
}

// Implement a method for this struct, so users can write `client.execute(...)`
// Note that we specified "yes" for auth, so this impl is bound to `Authed`
// clients only
impl<I: RequestImpl> OdooClient<Authed, I> {
    pub fn execute(&self, model: &str, method: &str, args: Vec<Value>) -> OdooRequest<Execute, I> {
        let execute = Execute {
            // Auth info is pulled from the Client
            database: self.auth.database.clone(),
            uid: self.auth.uid,
            password: self.auth.password.clone(),

            // Strings are passed as &str then converted to Strings
            model: model.into(),
            method: method.into(),
            args
        };

        // Finally, build the request
        self.build_request(
            execute,
            &self.url_jsonrpc
        )
    }
}