clientix_core/client/blocking/
client.rs

1use std::sync::{Arc, Mutex};
2use http::Method;
3use reqwest::blocking::Client as ReqwestClient;
4use crate::client::blocking::request::BlockingRequestBuilder;
5use crate::client::ClientConfig;
6
7#[derive(Clone)]
8pub struct BlockingClient {
9    pub client: Arc<Mutex<ReqwestClient>>,
10    pub url: String,
11    pub path: String
12}
13
14impl BlockingClient {
15
16    pub fn get(&self) -> BlockingRequestBuilder {
17        BlockingRequestBuilder::builder(self.clone(), Method::GET)
18    }
19
20    pub fn post(&self) -> BlockingRequestBuilder {
21        BlockingRequestBuilder::builder(self.clone(), Method::POST)
22    }
23
24    pub fn put(&self) -> BlockingRequestBuilder {
25        BlockingRequestBuilder::builder(self.clone(), Method::PUT)
26    }
27
28    pub fn delete(&self) -> BlockingRequestBuilder {
29        BlockingRequestBuilder::builder(self.clone(), Method::DELETE)
30    }
31
32}
33
34impl From<ClientConfig> for BlockingClient {
35
36    fn from(config: ClientConfig) -> Self {
37        let mut client = ReqwestClient::builder();
38
39        if let Some(user_agent) = config.user_agent {
40            client = client.user_agent(user_agent.clone());
41        }
42
43        if !config.headers.is_empty() {
44            client = client.default_headers(config.headers.clone());
45        }
46
47        if let Some(timeout) = config.timeout {
48            client = client.timeout(timeout);
49        }
50
51        if let Some(connect_timeout) = config.connect_timeout {
52            client = client.connect_timeout(connect_timeout);
53            client = client.connect_timeout(connect_timeout);
54        }
55
56        client = client.connection_verbose(config.connection_verbose);
57
58        let url = config.url.expect("missing url");
59        let path = config.path.unwrap_or(String::new());
60        let client = Arc::new(Mutex::new(client.build().expect("failed to build blocking client")));
61
62        BlockingClient { client, url, path }
63    }
64
65}