-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Refactor iterator to separate into HTTP/GAX iterators #2617
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
Refactor iterator to separate into HTTP/GAX iterators #2617
Conversation
For this commit, moving some features of HTTPIterator constructor onto base class.
This was HTTP/JSON specific and belongs in the HTTP subclass.
Also updating the _GAXPageIterator mock to allow multiple pages.
Instead, using the page token directly from the page iterator passed in (this may occasionally be strange to a user, e.g. if the token is INITIAL_PAGE).
This somewhat of a cart-before-the-horse change, but is done this way to make the commit easier to understand, before unifying the two approaches via _next_page().
Also moving the token setting behavior from HTTPIterator._get_next_page_response() into _next_page().
Also a lint fix for an unimorted member and a unit test fix adding a page token to allow more paging.
|
This conceptually LGTM, I'll defer @tseaver for implementation review. |
|
@tseaver Can I get an amen? |
tseaver
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch! Commit-by-comit was a hard row to hoe, especially on the last commit, which blew away so much of what had been done before
|
|
||
| class Iterator(object): | ||
| class HTTPIterator(object): | ||
| """A generic class for iterating through Cloud JSON APIs list responses. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
core/unit_tests/test_iterator.py
Outdated
| self.assertIs(iterator.client, client) | ||
| self.assertEqual(iterator.max_results, max_results) | ||
| self.assertEqual(list(iterator._page_iter), []) | ||
| self.assertFalse(iterator._page_increment) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| page = Page(iterator, items, _item_to_topic) | ||
| iterator.next_page_token = page_iter.page_token or None | ||
| iterator.num_results += page.num_items | ||
| yield page |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
core/google/cloud/iterator.py
Outdated
| except StopIteration: | ||
| return None | ||
|
|
||
| def _wrap_gax(self, page_iter): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| response = iterator._get_next_page_response() | ||
| self.assertEqual(response['items'], [{'name': key1}, {'name': key2}]) | ||
| self.assertEqual(iterator.page_number, 1) | ||
| self.assertEqual(iterator.next_page_token, token) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
@tseaver Sorry for the bait and switch at the end. It didn't become clear that a |
…ctor Refactor iterator to separate into HTTP/GAX iterators
Refactor iterator to separate into HTTP/GAX iterators

NOTE: I'm happy to split this PR into tiny PRs if it makes review easy, though the commits are all pretty compact, so it should be reviewable commit-by-commit.