use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::{client::WakapiClient, error::WakapiError};
pub struct CommitParams {
url: CommitParamsUrl,
params: CommitParamsParams,
}
struct CommitParamsUrl {
hash: String,
project: String,
}
#[derive(Serialize, Default)]
struct CommitParamsParams {
branch: Option<String>,
}
impl CommitParams {
pub fn new(project: &str, hash: &str) -> CommitParams {
CommitParams {
url: CommitParamsUrl {
hash: hash.to_string(),
project: project.to_string(),
},
params: CommitParamsParams { branch: None },
}
}
pub fn branch(mut self, branch: &str) -> CommitParams {
self.params.branch = Some(branch.to_string());
self
}
pub fn no_branch(mut self) -> CommitParams {
self.params.branch = None;
self
}
}
#[derive(Deserialize, Debug)]
pub struct Commit {
pub branch: String,
pub commit: CommitDetails,
pub project: CommitProjectDetails,
pub status: String,
}
#[derive(Deserialize, Debug)]
pub struct CommitDetails {
pub author_avatar_url: Option<String>,
pub author_date: Option<DateTime<Utc>>,
pub author_email: Option<String>,
pub author_html_url: Option<String>,
pub author_name: Option<String>,
pub author_url: Option<String>,
pub author_username: Option<String>,
pub branch: String,
pub committer_avatar_url: Option<String>,
pub committer_date: Option<DateTime<Utc>>,
pub committer_email: Option<String>,
pub committer_html_url: Option<String>,
pub committer_name: Option<String>,
pub committer_url: Option<String>,
pub committer_username: Option<String>,
pub created_at: Option<DateTime<Utc>>,
pub hash: String,
pub html_url: String,
pub human_readable_total: String,
pub human_readable_total_with_seconds: String,
pub id: String,
pub message: Option<String>,
pub r#ref: String,
pub total_seconds: f64,
pub truncated_hash: String,
pub url: String,
}
#[derive(Deserialize, Debug)]
pub struct CommitProjectDetails {
pub id: String,
pub name: String,
pub privacy: Option<String>,
pub repository: RepositoryDetails,
}
#[derive(Deserialize, Debug)]
pub struct RepositoryDetails {
pub default_branch: Option<String>,
pub description: Option<String>,
pub fork_count: Option<usize>,
pub full_name: String,
pub homepage: Option<String>,
pub html_url: Option<String>,
pub id: String,
pub is_fork: bool,
pub is_private: bool,
pub last_synced_at: Option<DateTime<Utc>>,
pub name: String,
pub provider: String,
pub star_count: Option<usize>,
pub url: String,
pub watch_count: Option<usize>,
}
impl Commit {
#[cfg(feature = "blocking")]
pub fn fetch(client: &WakapiClient, params: CommitParams) -> Result<Self, WakapiError> {
let url = client.build_url(
format!(
"/api/v1/users/:user/projects/{}/commits/{}",
¶ms.url.project, ¶ms.url.hash
)
.as_str(),
Some(serde_url_params::to_string(¶ms.params)?),
);
let response = reqwest::blocking::Client::new()
.get(&url)
.header("Authorization", client.get_auth_header())
.send()?;
if response.status().is_success() {
let body = response.json::<Commit>()?;
Ok(body)
} else {
let error = response.json::<crate::error::ErrorMessage>()?;
Err(WakapiError::ResponseError(error))
}
}
#[cfg(not(feature = "blocking"))]
pub async fn fetch(client: &WakapiClient, params: CommitParams) -> Result<Self, WakapiError> {
let url = client.build_url(
format!(
"/api/v1/users/:user/projects/{}/commits/{}",
¶ms.url.project, ¶ms.url.hash
)
.as_str(),
Some(serde_url_params::to_string(¶ms.params)?),
);
let response = reqwest::Client::new()
.get(&url)
.header("Authorization", client.get_auth_header())
.send()
.await?;
if response.status().is_success() {
let body = response.json::<Commit>().await?;
Ok(body)
} else {
let error = response.json::<crate::error::ErrorMessage>().await?;
Err(WakapiError::ResponseError(error))
}
}
}