use crate::error::{ApiError, Result};
use reqwest::Client;
use serde_json::Value;
use url::Url;
pub struct RepoContributorClient {
base_url: String,
client: Client,
}
impl RepoContributorClient {
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 get_repo_contributor_trend(
&self,
repo: String,
limit: Option<i64>,
exclude_external_users: Option<bool>,
) -> Result<Value> {
let path = format!("/{}/-/contributor/trend", repo);
let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
if let Some(value) = limit {
url.query_pairs_mut().append_pair("limit", &value.to_string());
}
if let Some(value) = exclude_external_users {
url.query_pairs_mut().append_pair("exclude_external_users", &value.to_string());
}
let request = self.client.request(
reqwest::Method::GET,
url
);
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()))
}
}
}