odoo_api/client/http_impl/
closure_blocking.rs1use crate::client::error::{ClosureAuthResult, ClosureError, ClosureResult};
2use crate::client::{AuthState, Authed, NotAuthed, OdooClient, OdooRequest, RequestImpl};
3use crate::jsonrpc::JsonRpcParams;
4use serde::Serialize;
5use serde_json::{to_value, Value};
6use std::fmt::Debug;
7
8pub type ClosureReturn = ClosureResult<(String, Option<String>)>;
10type Closure = Box<dyn Fn(&str, Value, Option<&str>) -> ClosureReturn>;
11
12pub struct ClosureBlocking {
13 closure: Closure,
14}
15impl RequestImpl for ClosureBlocking {
16 type Error = ClosureError;
17}
18
19impl OdooClient<NotAuthed, ClosureBlocking> {
20 pub fn new_closure_blocking<
21 F: Fn(&str, Value, Option<&str>) -> ClosureResult<(String, Option<String>)> + 'static,
22 >(
23 url: &str,
24 closure: F,
25 ) -> Self {
26 Self::new(
27 url,
28 ClosureBlocking {
29 closure: Box::new(closure),
30 },
31 )
32 }
33}
34
35impl<S> OdooClient<S, ClosureBlocking>
36where
37 S: AuthState,
38{
39 pub fn authenticate(
40 mut self,
41 db: &str,
42 login: &str,
43 password: &str,
44 ) -> ClosureAuthResult<OdooClient<Authed, ClosureBlocking>> {
45 let request = self.get_auth_request(db, login, password);
46 let (response, session_id) = request.send_internal()?;
47 Ok(self.parse_auth_response(db, login, password, response, session_id)?)
48 }
49}
50
51impl<'a, T> OdooRequest<'a, T, ClosureBlocking>
52where
53 T: JsonRpcParams + Debug + Serialize,
54 T::Container<T>: Debug + Serialize,
55{
56 pub fn send(self) -> ClosureResult<T::Response> {
57 Ok(self.send_internal()?.0)
58 }
59
60 fn send_internal(self) -> ClosureResult<(T::Response, Option<String>)> {
61 let data = to_value(&self.data)?;
62 let (response, session_id) = self._impl.closure.as_ref()(&self.url, data, self.session_id)?;
63 Ok((self.parse_response(&response)?, session_id))
64 }
65}