deepseek_api/async_impl/
client.rs

1use crate::completions::ChatCompletions;
2use crate::response::{BalanceResp, ModelResp};
3use anyhow::Result;
4use reqwest::Client as ReqwestClient;
5
6#[derive(Clone)]
7/// A client for interacting with the DeepSeek API.
8///
9/// # Example
10///
11/// ```no_run
12/// #[tokio::main]
13/// async fn main() {
14///     use deepseek_api::ClientBuilder;
15///
16///     let api_key = "your_api_key".to_string();
17///     let client = ClientBuilder::new(api_key).build().unwrap();
18///
19///     // Get available models
20///     let models = client.models().await.unwrap();
21///
22///     // Get user balance
23///     let balance = client.balance().await.unwrap();
24///
25///     // Create a chat completion
26///     let chat = client.chat();
27/// }
28/// ```
29///
30/// # Fields
31///
32/// * `client` - The underlying HTTP client.
33/// * `host` - The base URL for the DeepSeek API.
34pub struct Client {
35    pub(crate) client: ReqwestClient,
36    pub(crate) host: String,
37}
38
39impl Client {
40    pub fn chat(&self) -> ChatCompletions {
41        ChatCompletions {
42            client: self.client.clone(),
43            host: self.host.clone(),
44        }
45    }
46
47    /// Retrieves the list of available models from the DeepSeek API.
48    ///
49    /// This method sends a GET request to the `/models` endpoint of the DeepSeek API
50    /// and returns a `Result` containing a `ModelResp` on success.
51    ///
52    /// # Errors
53    ///
54    /// This function will return an error if the request fails or if the response
55    /// cannot be deserialized into a `ModelResp`.
56    ///
57    /// # Example
58    ///
59    /// ```no_run
60    /// #[tokio::main]
61    /// async fn main() {
62    ///     use deepseek_api::ClientBuilder;
63    ///
64    ///     let api_key = "your_api_key".to_string();
65    ///     let client = ClientBuilder::new(api_key).build().unwrap();
66    ///     let models = client.models().await.unwrap();
67    ///     println!("{:?}", models);
68    /// }
69    /// ```
70    ///
71    /// For more information, see the [DeepSeek API documentation](https://api-docs.deepseek.com/zh-cn/api/list-models).
72    pub async fn models(&self) -> Result<ModelResp> {
73        Ok(self
74            .client
75            .get(self.host.to_owned() + "/models")
76            .send()
77            .await?
78            .json()
79            .await?)
80    }
81
82    /// Retrieves the balance information of the user from the DeepSeek API.
83    ///
84    /// This method sends a GET request to the `/user/balance` endpoint of the DeepSeek API
85    /// and returns a `Result` containing a `BalanceResp` on success.
86    ///
87    /// # Errors
88    ///
89    /// This function will return an error if the request fails or if the response
90    /// cannot be deserialized into a `BalanceResp`.
91    ///
92    /// # Example
93    ///
94    /// ```no_run
95    /// #[tokio::main]
96    /// async fn main() {
97    ///     use deepseek_api::ClientBuilder;
98    ///
99    ///     let api_key = "your_api_key".to_string();
100    ///     let client = ClientBuilder::new(api_key).build().unwrap();
101    ///     let balance = client.balance().await.unwrap();
102    ///     println!("{:?}", balance);
103    /// }
104    /// ```
105    ///
106    /// For more information, see the [DeepSeek API documentation](https://api-docs.deepseek.com/zh-cn/api/get-user-balance).
107    pub async fn balance(&self) -> Result<BalanceResp> {
108        Ok(self
109            .client
110            .get(self.host.to_owned() + "/user/balance")
111            .send()
112            .await?
113            .json()
114            .await?)
115    }
116}