Skip to content

Commit

Permalink
Removed offset adjustment from limit calculation (#23)
Browse files Browse the repository at this point in the history
The number of result items should be capped to the item limit (either
the total number of items reported by the json or to some arbitrary max
value). Subtracting the offset value from that limit would effectively
cap the results early once the offset exceeded the number of results
retrieved.

Ex. Retrieving 60 total items with a 50 item limit and no max cap:
1. item_limit is 60, offset is 0, there are 50 items in the response.
Old effective cap is (60-0) 50, new effective cap is 50. We retrieve
and add all 50 items to the results. Let's increase the offset and
retrieve the next batch.
2. item_limit is 60, offset if 50, there are 10 items in the response.
Old effective cap is (60-50) 10, new effective cap is still 50. With
the old effective cap we simply stop and return the 50 items we had
already retrieved. With the new effective cap we retrieve and add all
10 items to the results, then return with the full 60 results.
  • Loading branch information
alextarrell authored Oct 18, 2022
1 parent 8bb7746 commit 92f0230
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl ApiClient {
// the minimum between the items in the response, and the total number of items requested
let item_limit = usize::min(json.total_number_of_items, max);
for item in json.items {
if result.len() >= item_limit - offset {
if result.len() >= item_limit {
break 'req;
}
result.push(item);
Expand Down

0 comments on commit 92f0230

Please sign in to comment.