Add Projects
This commit is contained in:
+66
-1
@@ -9,6 +9,8 @@ use governor::clock::DefaultClock;
|
||||
use reqwest::header::{HeaderMap, HeaderValue};
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use base64::Engine;
|
||||
use reqwest::Response;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
struct ReqwestRateLimiter {
|
||||
rate_limiter: governor::RateLimiter<NotKeyed, InMemoryState, DefaultClock>,
|
||||
@@ -66,6 +68,7 @@ impl TogglApi {
|
||||
let mut value = HeaderValue::from_str(&format!("Basic {}", toggl_auth)).unwrap();
|
||||
value.set_sensitive(true);
|
||||
headers.insert("Authorization", value);
|
||||
headers.insert("Content-Type", HeaderValue::from_static("application/json"));
|
||||
headers
|
||||
}
|
||||
|
||||
@@ -81,10 +84,35 @@ impl TogglApi {
|
||||
.await?
|
||||
.json().await?)
|
||||
}
|
||||
|
||||
pub async fn get_projects(&self) -> Result<Vec<types::Project>, TogglError> {
|
||||
let url = format!(
|
||||
"{base_url}/workspaces/{workspace_id}/projects",
|
||||
base_url = BASE_URL,
|
||||
workspace_id = self.workspace_id
|
||||
);
|
||||
|
||||
Self::parse(self.client.get(&url)
|
||||
.headers(self.headers.clone())
|
||||
.send().await?).await
|
||||
}
|
||||
|
||||
async fn parse<T: DeserializeOwned>(response: Response) -> Result<T, TogglError> {
|
||||
let data = response.text().await?;
|
||||
let result = serde_json_path_to_error::from_str(&data);
|
||||
|
||||
let result = result
|
||||
.map_err(|error| TogglError::JsonWithDataError {
|
||||
data,
|
||||
error,
|
||||
})?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
mod types {
|
||||
use chrono::{DateTime, Utc};
|
||||
use chrono::{DateTime, NaiveDate, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
@@ -141,6 +169,37 @@ mod types {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Project {
|
||||
id: i64,
|
||||
workspace_id: i64,
|
||||
client_id: Option<i64>,
|
||||
|
||||
name: String,
|
||||
color: String,
|
||||
status: Status,
|
||||
active: bool,
|
||||
|
||||
at: DateTime<Utc>,
|
||||
start_date: NaiveDate,
|
||||
created_at: DateTime<Utc>,
|
||||
server_deleted_at: Option<DateTime<Utc>>,
|
||||
|
||||
actual_hours: Option<i64>,
|
||||
actual_seconds: Option<i64>,
|
||||
can_track_time: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Status {
|
||||
Upcoming,
|
||||
Active,
|
||||
Archived,
|
||||
Ended,
|
||||
Deleted,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
@@ -150,6 +209,12 @@ pub enum TogglError {
|
||||
|
||||
#[error("Json error: {0}")]
|
||||
JsonError(#[from] serde_json::Error),
|
||||
|
||||
#[error("Failed to parse JSON data: {data}, error: {error}")]
|
||||
JsonWithDataError {
|
||||
data: String,
|
||||
error: serde_json_path_to_error::Error,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for TogglError {
|
||||
|
||||
Reference in New Issue
Block a user