Skip to main content

seher/glm/
client.rs

1use std::time::Duration;
2
3use super::types::GlmUsageResponse;
4
5const QUOTA_URL: &str = "https://open.bigmodel.cn/api/monitor/usage/quota/limit";
6
7pub struct GlmClient;
8
9impl GlmClient {
10    /// # Errors
11    ///
12    /// Returns an error if the API request fails or the response cannot be parsed.
13    pub async fn fetch_quota(
14        api_key: &str,
15    ) -> Result<GlmUsageResponse, Box<dyn std::error::Error>> {
16        let client = Self::build_client()?;
17        let response = client
18            .get(QUOTA_URL)
19            .header("Authorization", format!("Bearer {api_key}"))
20            .header("Accept", "application/json")
21            .send()
22            .await?;
23
24        let status = response.status();
25        if !status.is_success() {
26            let body = response.text().await.unwrap_or_default();
27            return Err(format!("GLM API error {status}: {body}").into());
28        }
29
30        let quota: GlmUsageResponse = response.json().await?;
31        Ok(quota)
32    }
33
34    fn build_client() -> Result<reqwest::Client, reqwest::Error> {
35        reqwest::Client::builder()
36            .timeout(Duration::from_secs(30))
37            .build()
38    }
39}