deepseek_api/
builder.rs

1use anyhow::{Ok, Result};
2cfg_if::cfg_if! {
3    if #[cfg(feature = "is_sync")] {
4        use reqwest::blocking::ClientBuilder as ReqwestClientBuilder;
5    } else {
6        use reqwest::ClientBuilder as ReqwestClientBuilder;
7    }
8}
9use crate::Client;
10use reqwest::header::HeaderMap;
11use std::time::Duration;
12
13/// A builder for constructing a `Client` instance with customizable options.
14///
15/// The `ClientBuilder` allows you to configure the API key, timeout, and host
16/// for the `Client` before building it.
17///
18/// # Examples
19///
20/// ```ignore
21/// let client = ClientBuilder::new("your_api_key".to_string())
22///     .timeout(30)
23///     .build()
24///     .expect("Failed to build client");
25/// ```
26pub struct ClientBuilder {
27    api_key: String,
28    timeout: Option<u64>,
29    host: String,
30}
31
32impl ClientBuilder {
33    /// Creates a new `ClientBuilder` with the specified API key.
34    ///
35    /// # Arguments
36    ///
37    /// * `api_key` - A `String` containing the API key to be used for authentication.
38    ///
39    /// # Returns
40    ///
41    /// A new instance of `ClientBuilder` with default settings.
42    ///
43    /// The default host is set to `"https://api.deepseek.com"`, and no timeout is configured.
44    pub fn new(api_key: String) -> Self {
45        Self {
46            api_key,
47            timeout: None,
48            host: "https://api.deepseek.com".to_string(),
49        }
50    }
51
52    /// Sets the timeout duration for the client.
53    ///
54    /// # Arguments
55    ///
56    /// * `duration` - A `u64` value representing the timeout duration in seconds.
57    ///
58    /// # Returns
59    ///
60    /// The `ClientBuilder` instance with the timeout configured.
61    /// ```ignore
62    /// let builder = ClientBuilder::new("your_api_key".to_string())
63    ///     .timeout(30);
64    /// ```
65    pub fn timeout(mut self, duration: u64) -> Self {
66        self.timeout = Some(duration);
67        self
68    }
69
70    /// Builds the `Client` instance using the configured options.
71    ///
72    /// # Returns
73    ///
74    /// A `Result` containing the constructed `Client` on success, or an error
75    /// if the client could not be built.
76    ///
77    /// # Errors
78    ///
79    /// This method will return an error if the underlying `reqwest::blocking::ClientBuilder`
80    /// fails to build the client.
81    ///
82    /// # Examples
83    ///
84    /// ```ignore
85    /// let client = ClientBuilder::new("your_api_key".to_string())
86    ///     .timeout(30)
87    ///     .build()
88    ///     .expect("Failed to build client");
89    /// ```
90    pub fn build(self) -> Result<Client> {
91        let mut headers = HeaderMap::new();
92        headers.insert("Authorization", format!("Bearer {}", self.api_key).parse()?);
93
94        let client_builder = ReqwestClientBuilder::new().default_headers(headers);
95        let client_builder = if let Some(secs) = self.timeout {
96            client_builder.timeout(Duration::from_secs(secs))
97        } else {
98            client_builder
99        };
100
101        let client = client_builder.build()?;
102        Ok(Client {
103            client,
104            host: self.host,
105        })
106    }
107}