Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix offset calculation for list items #18

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tdl"
description = "A command line tool for downloading files from the TIDAL API"
version = "0.2.4"
version = "0.2.5"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
20 changes: 11 additions & 9 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,35 +122,37 @@ impl ApiClient {
&self,
url: &str,
opts: Option<Vec<(String, String)>>,
max: Option<u32>,
max: Option<usize>,
) -> Result<Vec<T>, Error>
where
T: DeserializeOwned + 'a,
{
let mut limit = 50;
let mut offset = 0;
let max = max.unwrap_or(u32::MAX);
let mut params = vec![
("limit".to_string(), limit.to_string()),
("offset".to_string(), offset.to_string()),
];
let max = max.unwrap_or(usize::MAX);
let mut params = vec![("limit".to_string(), limit.to_string())];
if let Some(opt) = opts {
params.extend(opt);
}
};

let mut result: Vec<T> = Vec::new();
'req: loop {
params.push(("offset".to_string(), offset.to_string()));
let json = self.get::<ItemResponse<T>>(url, Some(&params)).await?;
limit = json.limit;
// the minimum between the items in the response, and the total number of items requested
let item_limit = u32::min(json.total_number_of_items, max);
let item_limit = usize::min(json.total_number_of_items, max);
for item in json.items {
if result.len() as u32 >= item_limit {
if result.len() >= item_limit {
break 'req;
}
result.push(item);
}
offset += limit;
params.pop();
if offset >= json.total_number_of_items {
break 'req;
}
}
Ok(result)
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Default for DeviceAuthRequest {
pub struct ItemResponse<T> {
pub limit: usize,
pub offset: usize,
pub total_number_of_items: u32,
pub total_number_of_items: usize,
pub items: Vec<T>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl SearchClient {
&self,
url: &str,
query: &str,
max: Option<u32>,
max: Option<usize>,
) -> Result<Table, Error>
where
T: DeserializeOwned + 'a + Tabled,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async fn consume_channel(channel: ReceiveChannel, concurrency: usize) {
async fn search(matches: &ArgMatches) {
let client = login().await;
if let Some(query) = matches.get_one::<String>("query") {
let max = matches.get_one::<u32>("max").cloned();
let max = matches.get_one::<usize>("max").cloned();
let result = match matches.get_one::<String>("filter") {
Some(filter) => match filter.as_str() {
"artist" => {
Expand Down