use crate::error::{ApiError, Result};
use reqwest::Client;
use serde_json::Value;
use url::Url;
pub struct AiClient {
base_url: String,
client: Client,
}
impl AiClient {
pub fn new(base_url: String, client: Client) -> Self {
Self { base_url, client }
}
pub fn with_auth(self, token: &str) -> Self {
self
}
pub async fn post_repo_ai_chat_completions(
&self,
repo: String,
request_data: serde_json::Value,
) -> Result<Value> {
let path = format!("/{}/-/ai/chat/completions", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let mut request = self.client.request(
reqwest::Method::POST,
url
);
request = request.json(&request_data);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
pub async fn post_repo_build_ai_auto_pr(
&self,
repo: String,
request_data: serde_json::Value,
) -> Result<Value> {
let path = format!("/{}/-/build/ai/auto-pr", repo);
let url = Url::parse(&format!("{}{}", self.base_url, path))?;
let mut request = self.client.request(
reqwest::Method::POST,
url
);
request = request.json(&request_data);
let response = request.send().await?;
if response.status().is_success() {
let json: Value = response.json().await?;
Ok(json)
} else {
Err(ApiError::HttpError(response.status().as_u16()))
}
}
}