1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! The base JSON-RPC types
//!
//! This module exposes type structs, traits, and helper methods to build valid
//! Odoo JSON-RPC requests.
//!
//! As a crate user, you shouldn't need to interact with these directly. Instead, see [`crate::client`].

use serde::{Deserialize, Serialize};
use std::fmt::Debug;

pub use request::{
    JsonRpcParams, JsonRpcRequest, OdooApiContainer, OdooApiMethod, OdooWebContainer, OdooWebMethod,
};
pub use response::JsonRpcResponse;

/// A JSON-RPC request id
pub type JsonRpcId = u32;

/// An Odoo record id
///
/// Note that this *is* signed, as some Odoo models (e.g. the `PurchaseBillUnion`)
/// use positive ids to represent one model (`purchase.order`), and negative ids
/// to represent another (`account.move`).
pub type OdooId = i32;

/// A string representing the JSON-RPC version
///
/// At the time of writing, this is always set to "2.0"
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum JsonRpcVersion {
    /// Odoo JSON-RCP API version 2.0
    #[serde(rename = "2.0")]
    V2,
}

/// A string representing the JSON-RPC "method"
///
/// At the time of writing, this is always set to "call"
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum JsonRpcMethod {
    #[serde(rename = "call")]
    Call,
}

pub mod request {
    //! JSON-RPC Requests

    use super::{JsonRpcId, JsonRpcMethod, JsonRpcVersion};
    use serde::de::DeserializeOwned;
    use serde::Serialize;
    use std::fmt::Debug;

    pub use api::{OdooApiContainer, OdooApiMethod};
    pub use web::{OdooWebContainer, OdooWebMethod};

    /// Implemented by Odoo "method" types (e.g.,
    /// [`Execute`](crate::service::object::Execute) or
    /// [`SessionAuthenticate`](crate::service::web::SessionAuthenticate))
    ///
    /// When building an [`JsonRpcRequest`] object, the `params` field is actually
    /// set to [`JsonRpcParams::Container`], not the concrete "method" type. This
    /// allows for flexibility in the [`Serialize`] impl.
    ///
    /// For example, the `Execute` method uses [`OdooApiContainer`] as its container,
    /// which injects the `service` and `method` keys into the request struct:
    /// ```json
    /// {
    ///     "jsonrpc": "2.0",
    ///     "method": "call",
    ///     "id": 1000,
    ///     "params": {
    ///         "service": "object",
    ///         "method": "execute",
    ///         "args": <Execute is serialized here>
    ///     }
    /// }
    /// ```
    ///
    /// Whereas the `SessionAuthenticate` method's container ([`OdooWebContainer`])
    /// has a transparent Serialize impl, so the `SessionAuthenticate` data is set
    /// directly on the `params` key:
    /// ```json
    /// {
    ///     "jsonrpc": "2.0",
    ///     "method": "call",
    ///     "id": 1000,
    ///     "params": <SessionAuthenticate is serialized here>
    /// }
    /// ```
    pub trait JsonRpcParams
    where
        Self: Sized + Debug + Serialize,
    {
        type Container<T>: Debug + Serialize;
        type Response: Debug + DeserializeOwned;

        fn build(self) -> JsonRpcRequest<Self>;
    }

    /// A struct representing the full JSON-RPC request body
    ///
    /// See [`JsonRpcParams`] for more info about the strange `params` field type.
    #[derive(Debug, Serialize)]
    pub struct JsonRpcRequest<T>
    where
        T: JsonRpcParams + Serialize + Debug,
        T::Container<T>: Debug + Serialize,
    {
        /// The JSON-RPC version (`2.0`)
        pub(crate) jsonrpc: JsonRpcVersion,

        /// The JSON-RPC method (`call`)
        pub(crate) method: JsonRpcMethod,

        /// The request id
        ///
        /// This is not used for any stateful behaviour on the Odoo/Python side
        pub(crate) id: JsonRpcId,

        /// The request params (service, method, and arguments)
        pub(crate) params: <T as JsonRpcParams>::Container<T>,
    }

    mod api {
        use super::{JsonRpcMethod, JsonRpcParams, JsonRpcRequest, JsonRpcVersion};
        use serde::ser::{SerializeStruct, Serializer};
        use serde::Serialize;
        use std::fmt::Debug;

        /// The container type for an Odoo "API" (JSON-RPC) request
        ///
        /// For more info, see [`super::JsonRpcParams`]
        #[derive(Debug)]
        pub struct OdooApiContainer<T>
        where
            T: OdooApiMethod + JsonRpcParams<Container<T> = Self>,
        {
            pub(crate) inner: T,
        }

        // Custom "man-in-the-middle" serialize impl
        impl<T> Serialize for OdooApiContainer<T>
        where
            T: OdooApiMethod + JsonRpcParams<Container<T> = Self>,
        {
            fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                let mut state = serializer.serialize_struct("args", 3)?;
                let (service, method) = self.inner.describe();
                state.serialize_field("service", service)?;
                state.serialize_field("method", method)?;
                state.serialize_field("args", &self.inner)?;
                state.end()
            }
        }

        /// An Odoo "API" (JSON-RPC) request type
        pub trait OdooApiMethod
        where
            Self:
                Sized + Debug + Serialize + JsonRpcParams<Container<Self> = OdooApiContainer<Self>>,
            Self::Container<Self>: Debug + Serialize,
        {
            /// Describe the JSON-RPC service and method for this type
            fn describe(&self) -> (&'static str, &'static str);

            /// Build `self` into a full [`JsonRpcRequest`]
            fn _build(self) -> JsonRpcRequest<Self> {
                JsonRpcRequest {
                    jsonrpc: JsonRpcVersion::V2,
                    method: JsonRpcMethod::Call,
                    id: 1000,
                    params: OdooApiContainer { inner: self },
                }
            }
        }
    }

    mod web {
        use serde::Serialize;
        use std::fmt::Debug;

        use super::{JsonRpcMethod, JsonRpcParams, JsonRpcRequest, JsonRpcVersion};

        /// The container type for an Odoo "Web" request
        ///
        /// This type covers (almost) any request whose endpoint starts with `/web`,
        /// for example:
        ///  - `/web/session/authenticate`
        ///  - `/web/session/destroy`
        ///  - `/web/dataset/call`
        ///  - And many more
        ///
        /// For more info, see [`super::JsonRpcParams`]
        #[derive(Debug, Serialize)]
        #[serde(transparent)]
        pub struct OdooWebContainer<T>
        where
            T: OdooWebMethod + JsonRpcParams<Container<T> = Self>,
        {
            pub(crate) inner: T,
        }

        /// An Odoo "Web" request type
        pub trait OdooWebMethod
        where
            Self:
                Sized + Debug + Serialize + JsonRpcParams<Container<Self> = OdooWebContainer<Self>>,
            Self::Container<Self>: Debug + Serialize,
        {
            /// Describe the "Web" method endpoint (e.g., "/web/session/authenticate")
            fn describe(&self) -> &'static str;

            /// Build `self` into a full [`JsonRpcRequest`]
            fn _build(self) -> JsonRpcRequest<Self> {
                JsonRpcRequest {
                    jsonrpc: JsonRpcVersion::V2,
                    method: JsonRpcMethod::Call,
                    id: 1000,
                    params: OdooWebContainer { inner: self },
                }
            }
        }
    }
}

pub mod response {
    //! JSON-RPC Responses

    use super::{JsonRpcId, JsonRpcVersion};
    use serde::{Deserialize, Serialize};
    use serde_json::{Map, Value};
    use std::fmt::Debug;

    /// An Odoo JSON-RPC API response
    ///
    /// This struct represents the base JSON data, and is paramterized over the
    /// *request* [`OdooApiMethod`](super::OdooApiMethod). The deserialization struct is chosen by
    /// looking at the associated type [`OdooApiMethod::Response`](super::OdooApiMethod).
    ///
    /// See: [odoo/http.py](https://github.com/odoo/odoo/blob/b6e195ccb3a6c37b0d980af159e546bdc67b1e42/odoo/http.py#L1805-L1841)
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    #[serde(untagged)]
    pub enum JsonRpcResponse<T>
    where
        T: Debug,
    {
        Success(JsonRpcResponseSuccess<T>),
        Error(JsonRpcResponseError),
    }

    /// A successful Odoo API response
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct JsonRpcResponseSuccess<T>
    where
        T:,
    {
        /// The JSON-RPC version (`2.0`)
        pub(crate) jsonrpc: JsonRpcVersion,

        /// The request id
        ///
        /// This is not used for any stateful behaviour on the Odoo/Python side
        pub(crate) id: JsonRpcId,

        /// The response data, parameterized on the *request* [`OdooApiMethod::Response`](super::OdooApiMethod)
        /// associated type.
        pub(crate) result: T,
    }

    /// A failed Odoo API response
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct JsonRpcResponseError {
        /// The JSON-RPC version (`2.0`)
        pub(crate) jsonrpc: JsonRpcVersion,

        /// The request id
        ///
        /// This is not used for any stateful behaviour on the Odoo/Python side
        pub(crate) id: JsonRpcId,

        /// A struct containing the error information
        pub(crate) error: JsonRpcError,
    }

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct JsonRpcError {
        /// The error code. Currently hardcoded to `200`
        pub code: u32,

        /// The error "message". This is a short string indicating the type of
        /// error. Some examples are:
        ///  * `Odoo Server Error`
        ///  * `404: Not Found`
        ///  * `Odoo Session Expired`
        pub message: String,

        /// The actual error data
        pub data: JsonRpcErrorData,
    }

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct JsonRpcErrorData {
        /// The module? and type of the object where the exception was raised
        ///
        /// For example:
        ///  * `builtins.TypeError`
        ///  * `odoo.addons.account.models.account_move.AccountMove`
        pub name: String,

        /// The Python exception stack trace
        pub debug: String,

        /// The Python exception message (e.g. `str(exception)`)
        pub message: String,

        /// The Python exception arguments (e.g. `excetion.args`)
        pub arguments: Vec<Value>,

        /// The Python exception context (e.g. `excetion.context`)
        pub context: Map<String, Value>,
    }
}