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

Use generator for get_all_serial #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ response.next # returns a URL to the next group of items in the pagination
response.offset # returns the nubmer of items offset by the current request
response.items # returns a list of items in the current group
response.limit # returns a count of the current group
response.all() # returns a list of all the items in the entire selection. Please note that this method can be time consuming and lead to timeout errors by the Shipwire API.
response.all() # returns a generator of all the items in the entire selection. Please note that this method can be time consuming and lead to timeout errors by the Shipwire API.
```

##### Unsuccessful requests:
Expand Down
9 changes: 5 additions & 4 deletions shipwire/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ def _get_all_serial(self):
# loop over all items with previous and next
next_uri = self.__next__
req = self.response.request
items = self.items

for item in self.items:
yield item

while next_uri:
resp = requests.request(req.method, next_uri, auth=self.shipwire.auth)
list_resp = ListResponse(resp, self.shipwire)
items.extend(list_resp.items)
for item in list_resp.items:
yield item
next_uri = list_resp.__next__

return items


class CreateResponse(ListResponse):
pass
Expand Down
4 changes: 2 additions & 2 deletions shipwire/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_all_calls_until_end(self, mock):
mock.side_effect = [fake_list_response([], next=p) for p in subsequent]

sw_response = responses.ListResponse(initial_response, MagicMock())
sw_response.all_serial()
list(sw_response.all_serial())

self.assertEqual(len(subsequent), mock.call_count)

Expand All @@ -65,4 +65,4 @@ def test_all_returns_all_items(self, mock):
for i, p in enumerate(subsequent)]

sw_response = responses.ListResponse(initial_response, MagicMock())
self.assertEqual([0, 1, 2, 3, 4], sw_response.all_serial())
self.assertEqual([0, 1, 2, 3, 4], list(sw_response.all_serial()))