1use std::time::Duration;
2
3use super::types::KimiK2CreditsResponse;
4
5const CREDITS_URL: &str = "https://kimi-k2.ai/api/user/credits";
6
7pub struct KimiK2Client;
8
9impl KimiK2Client {
10 pub async fn fetch_credits(
14 api_key: &str,
15 ) -> Result<KimiK2CreditsResponse, Box<dyn std::error::Error>> {
16 let client = Self::build_client()?;
17 let response = client
18 .get(CREDITS_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!("Kimi-K2 API error {status}: {body}").into());
28 }
29
30 let credits: KimiK2CreditsResponse = response.json().await?;
31 Ok(credits)
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}