odoo_api/client/http_impl/
reqwest_blocking.rs

1use crate::client::error::{ReqwestAuthResult, ReqwestError, ReqwestResult};
2use crate::client::{AuthState, Authed, NotAuthed, OdooClient, OdooRequest, RequestImpl};
3use crate::jsonrpc::JsonRpcParams;
4use reqwest::blocking::Client;
5use serde::Serialize;
6use std::fmt::Debug;
7
8pub struct ReqwestBlocking {
9    client: Client,
10}
11impl RequestImpl for ReqwestBlocking {
12    type Error = ReqwestError;
13}
14
15impl OdooClient<NotAuthed, ReqwestBlocking> {
16    pub fn new_reqwest_blocking(url: &str) -> Result<Self, reqwest::Error> {
17        let client = Client::builder().cookie_store(true).build()?;
18
19        Ok(Self::new(url, ReqwestBlocking { client }))
20    }
21}
22
23impl<S> OdooClient<S, ReqwestBlocking>
24where
25    S: AuthState,
26{
27    pub fn authenticate(
28        mut self,
29        db: &str,
30        login: &str,
31        password: &str,
32    ) -> ReqwestAuthResult<OdooClient<Authed, ReqwestBlocking>> {
33        let request = self.get_auth_request(db, login, password);
34        let (response, session_id) = request.send_internal()?;
35        Ok(self.parse_auth_response(db, login, password, response, session_id)?)
36    }
37}
38
39impl<'a, T> OdooRequest<'a, T, ReqwestBlocking>
40where
41    T: JsonRpcParams + Debug + Serialize,
42    T::Container<T>: Debug + Serialize,
43{
44    pub fn send(self) -> ReqwestResult<T::Response> {
45        Ok(self.send_internal()?.0)
46    }
47
48    fn send_internal(self) -> ReqwestResult<(T::Response, Option<String>)> {
49        let request = self._impl.client.post(&self.url).json(&self.data);
50        let response = request.send()?;
51        Ok((self.parse_response(&response.text()?)?, None))
52    }
53}