From c1b8636407e1efc80b7f9634e97cfc360950062d Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Tue, 16 Jul 2024 08:16:01 +0100 Subject: [PATCH] Add /me/time_entries --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 7 ++++++- src/toggl/mod.rs | 38 +++++++++++++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 479a35d..c7c07eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1464,6 +1464,7 @@ dependencies = [ "serde_json_path_to_error", "thiserror", "tokio", + "url", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 5e1f31b..31a872b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ base64 = "0.22.1" serde = { version = "1.0.204", features = ["derive"] } serde_json = "1.0.120" serde_json_path_to_error = "0.1.4" +url = "2.5.2" diff --git a/src/main.rs b/src/main.rs index 5e721f1..b6a2277 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::ops::Sub; +use chrono::{TimeDelta, Utc}; use toggl::TogglApi; mod toggl; @@ -10,5 +12,8 @@ async fn main() { sensitive::WORKSPACE_ID, ); - dbg!(api.get_tags().await); + dbg!(api.get_time_user_entries_between( + Utc::now().sub(TimeDelta::days(2)), + Utc::now(), + ).await); } diff --git a/src/toggl/mod.rs b/src/toggl/mod.rs index b845baf..39c254f 100644 --- a/src/toggl/mod.rs +++ b/src/toggl/mod.rs @@ -9,7 +9,8 @@ use governor::clock::DefaultClock; use reqwest::header::{HeaderMap, HeaderValue}; use base64::engine::general_purpose::STANDARD; use base64::Engine; -use reqwest::Response; +use chrono::{DateTime, SecondsFormat, Utc}; +use reqwest::{Response, Url}; use serde::de::DeserializeOwned; struct ReqwestRateLimiter { @@ -72,6 +73,38 @@ impl TogglApi { headers } + /// Fetches all time entries for this user from Toggl that have been modified since the given + /// date. + pub async fn get_time_entries_for_user_modified_since(&self, since: DateTime) -> Result, TogglError> { + let url = format!( + "{base_url}/me/time_entries?since={since}", + base_url = BASE_URL, + since = since.timestamp() + ); + + Self::parse(self.client.get(&url) + .headers(self.headers.clone()) + .send().await?).await + } + + /// Fetches all time entries for this user from Toggl that have a start time between the given + /// start and end times. + pub async fn get_time_user_entries_between(&self, start: DateTime, until: DateTime) -> Result, TogglError> { + let url = format!( + "{base_url}/me/time_entries", + base_url = BASE_URL, + ); + + let mut url = Url::parse(&url).unwrap(); + url.query_pairs_mut() + .append_pair("start_date", &start.to_rfc3339_opts(SecondsFormat::Secs, true)) + .append_pair("end_date", &until.to_rfc3339_opts(SecondsFormat::Secs, true)); + + Self::parse(self.client.get(url) + .headers(self.headers.clone()) + .send().await?).await + } + pub async fn get_current_time_entry(&self) -> Result, TogglError> { let url = format!( "{base_url}/me/time_entries/current", @@ -268,6 +301,9 @@ mod types { #[derive(Debug, thiserror::Error)] pub enum TogglError { + #[error("Toggl returned error: {0}")] + TogglError(serde_json::Value), + #[error("Reqwest error: {0}")] ReqwestError(#[from] reqwest_middleware::Error),