Skip to content

Commit

Permalink
refactor: don't filter on the client-side
Browse files Browse the repository at this point in the history
  • Loading branch information
icepuma committed Feb 11, 2024
1 parent cdb081c commit e441980
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
45 changes: 13 additions & 32 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use chrono::DateTime;
use chrono::Duration;
use chrono::Local;
use colored::Colorize;
use itertools::Itertools;
use minreq::Method;
use minreq::Request;
use minreq::Response;
Expand Down Expand Up @@ -183,22 +182,13 @@ impl TogglClient {
include_archived: bool,
workspace_id: u64,
) -> anyhow::Result<Option<Vec<Client>>> {
let clients = self.request(
debug,
Method::Get,
&format!("workspaces/{workspace_id}/clients"),
)?;

match clients {
Some(clients) if include_archived => Ok(Some(clients)),
Some(clients) => Ok(Some(
clients
.into_iter()
.filter(|client| !client.archived)
.collect_vec(),
)),
None => Ok(None),
}
let uri = if include_archived {
format!("workspaces/{workspace_id}/clients?status=both")
} else {
format!("workspaces/{workspace_id}/clients?status=active")
};

self.request(debug, Method::Get, &uri)
}

pub fn get_time_entries(
Expand Down Expand Up @@ -235,22 +225,13 @@ impl TogglClient {
include_archived: bool,
workspace_id: u64,
) -> anyhow::Result<Vec<Project>> {
let projects = self.request::<Vec<Project>>(
debug,
Method::Get,
&format!("workspaces/{workspace_id}/projects"),
)?;

if include_archived {
Ok(projects)
let uri = if include_archived {
format!("workspaces/{workspace_id}/projects")
} else {
Ok(
projects
.into_iter()
.filter(|project| project.status != "archived")
.collect_vec(),
)
}
format!("workspaces/{workspace_id}/projects?active=true")
};

self.request::<Vec<Project>>(debug, Method::Get, &uri)
}

#[allow(clippy::too_many_arguments)]
Expand Down
4 changes: 2 additions & 2 deletions src/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn get_workspace_clients() -> anyhow::Result<()> {
let mut server = mockito::Server::new();

let mock = server
.mock("GET", "/workspaces/12345678/clients")
.mock("GET", "/workspaces/12345678/clients?status=active")
.with_header(
"Authorization",
"Basic Y2I3YmY3ZWZhNmQ2NTIwNDZhYmQyZjdkODRlZTE4YzE6YXBpX3Rva2Vu",
Expand Down Expand Up @@ -221,7 +221,7 @@ fn get_workspace_projects() -> anyhow::Result<()> {
let mut server = mockito::Server::new();

let mock = server
.mock("GET", "/workspaces/12345678/projects")
.mock("GET", "/workspaces/12345678/projects?active=true")
.with_header(
"Authorization",
"Basic Y2I3YmY3ZWZhNmQ2NTIwNDZhYmQyZjdkODRlZTE4YzE6YXBpX3Rva2Vu",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/time_entries_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn test_create_workday_with_pause_2_hours() -> anyhow::Result<()> {
.create();

let projects_mock = server
.mock("GET", "/workspaces/1234567/projects")
.mock("GET", "/workspaces/1234567/projects?active=true")
.with_header(
"Authorization",
"Basic Y2I3YmY3ZWZhNmQ2NTIwNDZhYmQyZjdkODRlZTE4YzE6YXBpX3Rva2Vu",
Expand Down Expand Up @@ -298,7 +298,7 @@ fn test_create_workday_with_pause_7_hours() -> anyhow::Result<()> {
.create();

let projects_mock = server
.mock("GET", "/workspaces/1234567/projects")
.mock("GET", "/workspaces/1234567/projects?active=true")
.with_header(
"Authorization",
"Basic Y2I3YmY3ZWZhNmQ2NTIwNDZhYmQyZjdkODRlZTE4YzE6YXBpX3Rva2Vu",
Expand Down

0 comments on commit e441980

Please sign in to comment.