use std::collections::HashMap;
use std::sync::Arc;
use serde_json::Value;
use crate::error::Error;
use crate::transport::HttpTransport;
use crate::types::Repository;
pub struct ReposClient {
transport: Arc<HttpTransport>,
}
impl ReposClient {
pub fn new(transport: Arc<HttpTransport>) -> Self {
Self { transport }
}
pub async fn create(
&self,
name: &str,
description: Option<&str>,
visibility: Option<&str>,
) -> Result<Repository, Error> {
let mut body: HashMap<String, Value> = HashMap::new();
body.insert("name".to_string(), Value::String(name.to_string()));
body.insert(
"description".to_string(),
description.map_or(Value::Null, |d| Value::String(d.to_string())),
);
body.insert(
"visibility".to_string(),
Value::String(visibility.unwrap_or("public").to_string()),
);
let response: Value = self
.transport
.signed_request("POST", "/v1/repos", "repo_create", body)
.await?;
let data = response
.get("data")
.ok_or_else(|| Error::Http("Missing data in response".to_string()))?;
serde_json::from_value(data.clone()).map_err(Error::from)
}
pub async fn get(&self, repo_id: &str) -> Result<Repository, Error> {
let response: Value = self
.transport
.unsigned_request::<Value>("GET", &format!("/v1/repos/{repo_id}"), None, None::<&()>)
.await?;
let data = response
.get("data")
.ok_or_else(|| Error::Http("Missing data in response".to_string()))?;
serde_json::from_value(data.clone()).map_err(Error::from)
}
pub async fn list(&self) -> Result<Vec<Repository>, Error> {
let response: Value = self
.transport
.signed_request("GET", "/v1/repos", "repo_list", HashMap::new())
.await?;
let data = response
.get("data")
.ok_or_else(|| Error::Http("Missing data in response".to_string()))?;
let repos = data
.get("repos")
.ok_or_else(|| Error::Http("Missing repos in response".to_string()))?;
serde_json::from_value(repos.clone()).map_err(Error::from)
}
}