hydrus_api/api_core/endpoints/
client_builder.rs

1use crate::error::{Error, Result};
2use crate::Client;
3use std::time::Duration;
4
5pub struct ClientBuilder {
6    reqwest_builder: reqwest::ClientBuilder,
7    base_url: String,
8    access_key: Option<String>,
9}
10
11impl Default for ClientBuilder {
12    fn default() -> Self {
13        Self {
14            reqwest_builder: Default::default(),
15            base_url: "127.0.0.1:45869".to_string(),
16            access_key: None,
17        }
18    }
19}
20
21impl ClientBuilder {
22    /// Set the base url with port for the client api
23    /// The default value is `127.0.0.1:45869`
24    pub fn url<S: ToString>(mut self, url: S) -> Self {
25        self.base_url = url.to_string();
26
27        self
28    }
29
30    /// Sets the access key for the client.
31    /// The key is required
32    pub fn access_key<S: ToString>(mut self, key: S) -> Self {
33        self.access_key = Some(key.to_string());
34
35        self
36    }
37
38    /// Sets the default timeout for requests to the API
39    pub fn timeout(mut self, timeout: Duration) -> Self {
40        self.reqwest_builder = self.reqwest_builder.timeout(timeout);
41
42        self
43    }
44
45    /// Builds the client
46    pub fn build(self) -> Result<Client> {
47        let access_key = self
48            .access_key
49            .ok_or_else(|| Error::BuildError(String::from("missing access key")))?;
50        Ok(Client {
51            inner: self.reqwest_builder.build()?,
52            base_url: self.base_url,
53            access_key,
54        })
55    }
56}