odoo_api/jsonrpc/
request.rs

1//! JSON-RPC Requests
2
3use super::{JsonRpcId, JsonRpcMethod, JsonRpcVersion};
4use serde::de::DeserializeOwned;
5use serde::Serialize;
6use std::fmt::Debug;
7
8mod api;
9mod orm;
10mod web;
11
12pub use api::{OdooApiContainer, OdooApiMethod};
13pub use orm::{OdooOrmContainer, OdooOrmMethod};
14pub use web::{OdooWebContainer, OdooWebMethod};
15
16/// Implemented by Odoo "method" types (e.g.,
17/// [`Execute`](crate::service::object::Execute) or
18/// [`SessionAuthenticate`](crate::service::web::SessionAuthenticate))
19///
20/// When building an [`JsonRpcRequest`] object, the `params` field is actually
21/// set to [`JsonRpcParams::Container`], not the concrete "method" type. This
22/// allows for flexibility in the [`Serialize`] impl.
23///
24/// For example, the `Execute` method uses [`OdooApiContainer`] as its container,
25/// which injects the `service` and `method` keys into the request struct:
26/// ```json
27/// {
28///     "jsonrpc": "2.0",
29///     "method": "call",
30///     "id": 1000,
31///     "params": {
32///         "service": "object",
33///         "method": "execute",
34///         "args": <Execute is serialized here>
35///     }
36/// }
37/// ```
38///
39/// Whereas the `SessionAuthenticate` method's container ([`OdooWebContainer`])
40/// has a transparent Serialize impl, so the `SessionAuthenticate` data is set
41/// directly on the `params` key:
42/// ```json
43/// {
44///     "jsonrpc": "2.0",
45///     "method": "call",
46///     "id": 1000,
47///     "params": <SessionAuthenticate is serialized here>
48/// }
49/// ```
50pub trait JsonRpcParams
51where
52    Self: Sized + Debug + Serialize,
53{
54    type Container<T>: Debug + Serialize;
55    type Response: Debug + DeserializeOwned;
56
57    fn build(self, id: JsonRpcId) -> JsonRpcRequest<Self>;
58}
59
60/// A struct representing the full JSON-RPC request body
61///
62/// See [`JsonRpcParams`] for more info about the strange `params` field type.
63#[derive(Debug, Serialize)]
64pub struct JsonRpcRequest<T>
65where
66    T: JsonRpcParams + Serialize + Debug,
67    T::Container<T>: Debug + Serialize,
68{
69    /// The JSON-RPC version (`2.0`)
70    pub(crate) jsonrpc: JsonRpcVersion,
71
72    /// The JSON-RPC method (`call`)
73    pub(crate) method: JsonRpcMethod,
74
75    /// The request id
76    ///
77    /// This is not used for any stateful behaviour on the Odoo/Python side
78    pub(crate) id: JsonRpcId,
79
80    /// The request params (service, method, and arguments)
81    pub(crate) params: <T as JsonRpcParams>::Container<T>,
82}