1use crate::client::XaiClient;
4use crate::models::auth::ApiKeyInfo;
5use crate::{Error, Result};
6
7#[derive(Debug, Clone)]
9pub struct AuthApi {
10 client: XaiClient,
11}
12
13impl AuthApi {
14 pub(crate) fn new(client: XaiClient) -> Self {
15 Self { client }
16 }
17
18 pub async fn api_key(&self) -> Result<ApiKeyInfo> {
33 let url = format!("{}/api-key", self.client.base_url());
34 let response = self.client.send(self.client.http().get(&url)).await?;
35
36 if !response.status().is_success() {
37 return Err(Error::from_response(response).await);
38 }
39
40 Ok(response.json().await?)
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use serde_json::json;
48 use wiremock::matchers::{method, path};
49 use wiremock::{Mock, MockServer, ResponseTemplate};
50
51 #[tokio::test]
52 async fn api_key() {
53 let server = MockServer::start().await;
54
55 Mock::given(method("GET"))
56 .and(path("/api-key"))
57 .respond_with(ResponseTemplate::new(200).set_body_json(json!({
58 "api_key": "k_123",
59 "object": "api_key",
60 "created_at": 1700000000
61 })))
62 .mount(&server)
63 .await;
64
65 let client = XaiClient::builder()
66 .api_key("test-key")
67 .base_url(server.uri())
68 .build()
69 .unwrap();
70
71 let info = client.auth().api_key().await.unwrap();
72 assert_eq!(info.api_key, "k_123");
73 assert_eq!(info.created_at, Some(1700000000));
74 }
75}