use std::collections::HashMap;
use std::sync::Arc;
use serde_json::Value;
use crate::error::Error;
use crate::transport::HttpTransport;
use crate::types::{StarResponse, StarsInfo};
pub struct StarsClient {
transport: Arc<HttpTransport>,
}
impl StarsClient {
pub fn new(transport: Arc<HttpTransport>) -> Self {
Self { transport }
}
pub async fn star(
&self,
repo_id: &str,
reason: Option<&str>,
reason_public: bool,
) -> Result<StarResponse, Error> {
let mut body: HashMap<String, Value> = HashMap::new();
body.insert("repoId".to_string(), Value::String(repo_id.to_string()));
body.insert(
"reason".to_string(),
reason.map_or(Value::Null, |r| Value::String(r.to_string())),
);
body.insert("reasonPublic".to_string(), Value::Bool(reason_public));
let response: Value = self
.transport
.signed_request(
"POST",
&format!("/v1/repos/{repo_id}/stars/:star"),
"star",
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 unstar(&self, repo_id: &str) -> Result<StarResponse, Error> {
let mut body: HashMap<String, Value> = HashMap::new();
body.insert("repoId".to_string(), Value::String(repo_id.to_string()));
let response: Value = self
.transport
.signed_request(
"POST",
&format!("/v1/repos/{repo_id}/stars/:unstar"),
"unstar",
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<StarsInfo, Error> {
let response: Value = self
.transport
.unsigned_request::<Value>(
"GET",
&format!("/v1/repos/{repo_id}/stars"),
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)
}
}