use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::{client::WakapiClient, error::WakapiError};
#[derive(Serialize, Default)]
pub struct AllTimeSinceTodayParams {
project: Option<String>,
}
impl AllTimeSinceTodayParams {
pub fn new() -> AllTimeSinceTodayParams {
AllTimeSinceTodayParams { project: None }
}
pub fn project(mut self, project: &str) -> AllTimeSinceTodayParams {
self.project = Some(project.to_string());
self
}
pub fn no_project(mut self) -> AllTimeSinceTodayParams {
self.project = None;
self
}
}
#[derive(Deserialize, Debug)]
pub struct AllTimeSinceTodayBody {
pub data: AllTimeSinceTodayData,
}
#[derive(Deserialize, Debug)]
pub struct AllTimeSinceTodayData {
pub daily_average: f64,
pub decimal: String,
pub digital: String,
pub is_up_to_date: bool,
pub percent_calculated: u8,
pub range: AllTimeSinceTodayRange,
pub text: String,
pub timeout: usize,
pub total_seconds: f64,
}
#[derive(Deserialize, Debug)]
pub struct AllTimeSinceTodayRange {
pub end: DateTime<Utc>,
pub end_date: String,
pub end_text: String,
pub start: DateTime<Utc>,
pub start_date: String,
pub start_text: String,
pub timezone: String,
}
#[derive(Debug)]
pub enum AllTimeSinceToday {
Success(AllTimeSinceTodayBody),
WillRefresh(AllTimeSinceTodayBody),
}
impl AllTimeSinceToday {
#[cfg(feature = "blocking")]
pub fn fetch(
client: &WakapiClient,
params: AllTimeSinceTodayParams,
) -> Result<AllTimeSinceToday, WakapiError> {
let url_params = serde_url_params::to_string(¶ms)?;
let url = client.build_url("/api/v1/users/:user/all_time_since_today", Some(url_params));
let response = reqwest::blocking::Client::new()
.get(&url)
.header("Authorization", client.get_auth_header())
.send()?;
match response.status() {
reqwest::StatusCode::OK => {
let data = response.json::<AllTimeSinceTodayBody>()?;
Ok(AllTimeSinceToday::Success(data))
}
reqwest::StatusCode::ACCEPTED => {
let data = response.json::<AllTimeSinceTodayBody>()?;
Ok(AllTimeSinceToday::WillRefresh(data))
}
_ => Err(WakapiError::RequestError(
response.error_for_status().unwrap_err(),
)),
}
}
#[cfg(not(feature = "blocking"))]
pub async fn fetch(
client: &WakapiClient,
params: AllTimeSinceTodayParams,
) -> Result<AllTimeSinceToday, WakapiError> {
let url_params = serde_url_params::to_string(¶ms)?;
let url = client.build_url("/api/v1/users/:user/all_time_since_today", Some(url_params));
let response = reqwest::Client::new()
.get(&url)
.header("Authorization", client.get_auth_header())
.send()
.await?;
match response.status() {
reqwest::StatusCode::OK => {
let data = response.json::<AllTimeSinceTodayBody>().await?;
Ok(AllTimeSinceToday::Success(data))
}
reqwest::StatusCode::ACCEPTED => {
let data = response.json::<AllTimeSinceTodayBody>().await?;
Ok(AllTimeSinceToday::WillRefresh(data))
}
_ => Err(WakapiError::RequestError(
response.error_for_status().unwrap_err(),
)),
}
}
}