1use crate::error::{ApiError, Result};
4use reqwest::Client;
5use serde_json::Value;
6use url::Url;
7
8pub struct AiClient {
10 base_url: String,
11 client: Client,
12}
13
14impl AiClient {
15 pub fn new(base_url: String, client: Client) -> Self {
17 Self { base_url, client }
18 }
19
20 pub fn with_auth(self, token: &str) -> Self {
22 self
24 }
25
26 pub async fn post_repo_ai_chat_completions(
28 &self,
29 repo: String,
30 request_data: serde_json::Value,
31 ) -> Result<Value> {
32 let path = format!("/{}/-/ai/chat/completions", repo);
33 let url = Url::parse(&format!("{}{}", self.base_url, path))?;
34
35
36
37 let mut request = self.client.request(
38 reqwest::Method::POST,
39 url
40 );
41
42
43
44 request = request.json(&request_data);
45
46 let response = request.send().await?;
47
48 if response.status().is_success() {
49 let json: Value = response.json().await?;
50 Ok(json)
51 } else {
52 Err(ApiError::HttpError(response.status().as_u16()))
53 }
54 }
55
56}