Skip to main content

seher/warp/
client.rs

1use std::time::Duration;
2
3use super::types::WarpLimitInfoResponse;
4
5const GRAPHQL_URL: &str = "https://api.warp.dev/graphql";
6
7const QUERY: &str = r#"{ "query": "{ getRequestLimitInfo { limit used resetInSeconds } }" }"#;
8
9pub struct WarpClient;
10
11impl WarpClient {
12    /// # Errors
13    ///
14    /// Returns an error if the GraphQL request fails or the response cannot be parsed.
15    pub async fn fetch_limit_info(
16        api_key: &str,
17    ) -> Result<WarpLimitInfoResponse, Box<dyn std::error::Error>> {
18        let client = Self::build_client()?;
19        let response = client
20            .post(GRAPHQL_URL)
21            .header("Authorization", format!("Bearer {api_key}"))
22            .header("Content-Type", "application/json")
23            .body(QUERY)
24            .send()
25            .await?;
26
27        let status = response.status();
28        if !status.is_success() {
29            let body = response.text().await.unwrap_or_default();
30            return Err(format!("Warp API error {status}: {body}").into());
31        }
32
33        let info: WarpLimitInfoResponse = response.json().await?;
34        Ok(info)
35    }
36
37    fn build_client() -> Result<reqwest::Client, reqwest::Error> {
38        reqwest::Client::builder()
39            .timeout(Duration::from_secs(30))
40            .build()
41    }
42}