odoo_api/jsonrpc/request/
api.rs

1use crate::jsonrpc::JsonRpcId;
2
3use super::{JsonRpcMethod, JsonRpcParams, JsonRpcRequest, JsonRpcVersion};
4use serde::ser::{SerializeStruct, Serializer};
5use serde::Serialize;
6use std::fmt::Debug;
7
8/// The container type for an Odoo "API" (JSON-RPC) request
9///
10/// For more info, see [`super::JsonRpcParams`]
11#[derive(Debug)]
12pub struct OdooApiContainer<T>
13where
14    T: OdooApiMethod + JsonRpcParams<Container<T> = Self>,
15{
16    pub(crate) inner: T,
17}
18
19// Custom "man-in-the-middle" serialize impl
20impl<T> Serialize for OdooApiContainer<T>
21where
22    T: OdooApiMethod + JsonRpcParams<Container<T> = Self>,
23{
24    fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
25    where
26        S: Serializer,
27    {
28        let mut state = serializer.serialize_struct("args", 3)?;
29        let (service, method) = self.inner.describe();
30        state.serialize_field("service", service)?;
31        state.serialize_field("method", method)?;
32        state.serialize_field("args", &self.inner)?;
33        state.end()
34    }
35}
36
37/// An Odoo "API" (JSON-RPC) request type
38pub trait OdooApiMethod
39where
40    Self: Sized + Debug + Serialize + JsonRpcParams<Container<Self> = OdooApiContainer<Self>>,
41    Self::Container<Self>: Debug + Serialize,
42{
43    /// Describe the JSON-RPC service and method for this type
44    fn describe(&self) -> (&'static str, &'static str);
45
46    /// Describe method endpoint (e.g., "/web/session/authenticate")
47    fn endpoint(&self) -> &'static str;
48
49    /// Build `self` into a full [`JsonRpcRequest`]
50    fn _build(self, id: JsonRpcId) -> JsonRpcRequest<Self> {
51        JsonRpcRequest {
52            jsonrpc: JsonRpcVersion::V2,
53            method: JsonRpcMethod::Call,
54            id,
55            params: OdooApiContainer { inner: self },
56        }
57    }
58}