Skip to content

Commit

Permalink
Refactor request options into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided committed Jul 8, 2023
1 parent 7acb6f3 commit 7d7fa61
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
.build()?;

let summary = client
.summaries(
"2023-01-01",
"2023-01-08",
None,
None,
None,
None,
None,
None,
)
.summaries("2023-01-01", "2023-01-08", SummariesOptions::default())
.await?;
println!("{summary:?}");

Expand Down
19 changes: 6 additions & 13 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::error::Error;
use waka::WakaTimeClientBuilder;
use waka::{AllTimesSinceTodayOptions, CommitOptions, SummariesOptions, WakaTimeClientBuilder};

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -11,27 +11,20 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
.build()?;

let summary = client
.summaries(
"2023-01-01",
"2023-01-08",
None,
None,
None,
None,
None,
None,
)
.summaries("2023-01-01", "2023-01-08", SummariesOptions::default())
.await?;
println!("{summary:?}");

let all_time_since_today = client.all_time_since_today(None).await?;
let all_time_since_today = client
.all_time_since_today(AllTimesSinceTodayOptions::default())
.await?;
println!("{all_time_since_today:?}");

let commit = client
.commit(
"cartoonify",
"a9cb579b28b39880474c76471c3f337fb6bb9752",
None,
CommitOptions::default(),
)
.await?;
println!("{commit:?}");
Expand Down
67 changes: 37 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! This is a work in progress and the API may change over time.
//!
//! ```no_run
//! use waka::WakaTimeClientBuilder;
//! use waka::{SummariesOptions, WakaTimeClientBuilder};
//!
//! # async fn test() -> Result<(), Box<dyn std::error::Error>> {
//! let api_key = std::env::var("WAKATIME_API_KEY")?;
Expand All @@ -14,16 +14,8 @@
//! .build()?;
//!
//! let summary = client
//! .summaries(
//! "2023-01-01",
//! "2023-01-08",
//! None,
//! None,
//! None,
//! None,
//! None,
//! None,
//! ).await?;
//! .summaries("2023-01-01", "2023-01-08", SummariesOptions::default())
//! .await?;
//! # Ok(())
//! # }
//! ```
Expand Down Expand Up @@ -83,11 +75,11 @@ pub struct WakaTimeClient {
impl WakaTimeClient {
/// ## Documentation
/// * [All Time Since Today](https://wakatime.com/developers#all_time_since_today)
pub async fn all_time_since_today(
pub async fn all_time_since_today<'a>(
&self,
project: Option<&str>,
options: AllTimesSinceTodayOptions<'a>,
) -> Result<model::all_times_since_today::AllTimeSinceToday, ApiError> {
let qs = QueryString::new().with_opt_value("project", project);
let qs = QueryString::new().with_opt_value("project", options.project);
let url = format!(
"{BASE_URL}users/{user}/all_time_since_today{qs}",
user = self.user
Expand All @@ -102,13 +94,13 @@ impl WakaTimeClient {

/// ## Documentation
/// * [Commits](https://wakatime.com/developers#commits)
pub async fn commit(
pub async fn commit<'a>(
&self,
project: &str,
hash: &str,
branch: Option<&str>,
options: CommitOptions<'a>,
) -> Result<model::commit::CommitResponse, ApiError> {
let qs = QueryString::new().with_opt_value("branch", branch);
let qs = QueryString::new().with_opt_value("branch", options.branch);
let url = format!(
"{BASE_URL}users/{user}/projects/{project}/commits/{hash}{qs}",
user = self.user
Expand All @@ -119,28 +111,23 @@ impl WakaTimeClient {

/// ## Documentation
/// * [Summaries](https://wakatime.com/developers#summaries)
pub async fn summaries(
pub async fn summaries<'a>(
&self,
start: &str,
end: &str,
project: Option<&str>,
branches: Option<&str>,
timeout: Option<u32>,
writes_only: Option<bool>,
timezone: Option<&str>,
range: Option<&str>,
options: SummariesOptions<'a>,
) -> Result<model::summaries::Summaries, ApiError> {
let timeout = timeout.map(|v| v.to_string());
let writes_only = writes_only.map(|v| v.to_string());
let timeout = options.timeout.map(|v| v.to_string());
let writes_only = options.writes_only.map(|v| v.to_string());
let qs = QueryString::new()
.with_value("start", start)
.with_value("end", end)
.with_opt_value("project", project)
.with_opt_value("branches", branches)
.with_opt_value("project", options.project)
.with_opt_value("branches", options.branches)
.with_opt_value("timeout", timeout.as_deref())
.with_opt_value("writes_only", writes_only.as_deref())
.with_opt_value("timezone", timezone)
.with_opt_value("range", range);
.with_opt_value("timezone", options.timezone)
.with_opt_value("range", options.range);
let url = format!("{BASE_URL}users/{user}/summaries{qs}", user = self.user);
let response = self.client.get(url).send().await?;
Self::deserialize_as(response, |r| r).await
Expand Down Expand Up @@ -176,3 +163,23 @@ pub struct ErrorsResponse {
pub struct DataWrapper<T> {
data: T,
}

#[derive(Debug, Default, Clone)]
pub struct AllTimesSinceTodayOptions<'a> {
pub project: Option<&'a str>,
}

#[derive(Debug, Default, Clone)]
pub struct CommitOptions<'a> {
pub branch: Option<&'a str>,
}

#[derive(Debug, Default, Clone)]
pub struct SummariesOptions<'a> {
pub project: Option<&'a str>,
pub branches: Option<&'a str>,
pub timeout: Option<u32>,
pub writes_only: Option<bool>,
pub timezone: Option<&'a str>,
pub range: Option<&'a str>,
}
2 changes: 1 addition & 1 deletion src/model/all_times_since_today.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct AllTimeSinceToday {
/// `true` if the stats are up to date; when `false`, a 202 response code is returned and stats will be refreshed soon.
pub is_up_to_date: bool,
/// A number between 0 and 100 where 100 means the stats are up to date including Today’s time.
pub percent_calculated: u8,
pub percent_calculated: Option<u8>,
pub range: AllTimeSinceTodayRange,
/// Total time logged since account created as human readable string.
pub text: String,
Expand Down

0 comments on commit 7d7fa61

Please sign in to comment.