use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::{WakapiClient, WakapiError};
use super::commit::RepositoryDetails;
#[derive(Serialize, Default)]
pub struct ProjectsParams {
q: Option<String>,
page: Option<usize>,
}
impl ProjectsParams {
pub fn new() -> ProjectsParams {
ProjectsParams {
q: None,
page: None,
}
}
pub fn q(mut self, q: &str) -> ProjectsParams {
self.q = Some(q.to_string());
self
}
pub fn page(mut self, page: usize) -> ProjectsParams {
self.page = Some(page);
self
}
}
#[derive(Deserialize, Debug)]
pub struct Projects {
pub data: Vec<ProjectsData>,
pub total: usize,
pub total_pages: usize,
pub page: usize,
pub prev_page: Option<usize>,
pub next_page: Option<usize>,
}
#[derive(Deserialize, Debug)]
pub struct ProjectsData {
pub id: String,
pub name: String,
pub repository: Option<RepositoryDetails>,
pub badge: Option<ProjectBadge>,
pub color: Option<String>,
pub clients: Vec<ProjectClients>,
pub has_public_url: bool,
pub human_readable_last_heartbeat_at: String,
pub last_heartbeat_at: DateTime<Utc>,
pub human_readable_first_heartbeat_at: Option<String>,
pub first_heartbeat_at: Option<String>,
pub url: String,
pub urlencoded_name: String,
pub created_at: DateTime<Utc>,
}
#[derive(Deserialize, Debug)]
pub struct ProjectBadge {
pub color: String,
pub id: String,
pub left_text: String,
pub link: String,
pub project_id: String,
pub snippets: Vec<BadgeSnippet>,
pub title: String,
pub url: String,
}
#[derive(Deserialize, Debug)]
pub struct BadgeSnippet {
pub content: String,
pub name: String,
}
#[derive(Deserialize, Debug)]
pub struct ProjectClients {
pub id: String,
pub name: String,
pub rate: f64,
pub timeout: usize,
}
impl Projects {
#[cfg(feature = "blocking")]
pub fn fetch(client: &WakapiClient, params: ProjectsParams) -> Result<Self, WakapiError> {
let url = client.build_url(
"/api/v1/users/current/projects",
Some(serde_url_params::to_string(¶ms)?),
);
let response = reqwest::blocking::Client::new()
.get(&url)
.header("Authorization", client.get_auth_header())
.send()?;
if response.status().is_success() {
let body = response.json::<Projects>()?;
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: ProjectsParams) -> Result<Self, WakapiError> {
let url = client.build_url(
"/api/v1/users/current/projects",
Some(serde_url_params::to_string(¶ms)?),
);
let response = reqwest::Client::new()
.get(&url)
.header("Authorization", client.get_auth_header())
.send()
.await?;
if response.status().is_success() {
let body = response.json::<Projects>().await?;
Ok(body)
} else {
let error = response.json::<crate::error::ErrorMessage>().await?;
Err(WakapiError::ResponseError(error))
}
}
}