-
Notifications
You must be signed in to change notification settings - Fork 445
Implement Pager for auto-paging requests #90
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d4fe1c3
Initial Draft of Paging Getter
t8y8 e2fbec6
Address feedback on PR, add tests, and clean up some comments.
t8y8 e59860e
Fix tests, pep8
t8y8 6d15d32
Wow, Monday
t8y8 080634b
Update Samples and feedback
t8y8 738e490
final comment tweaks
t8y8 55d102c
final bit
t8y8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from . import RequestOptions | ||
|
|
||
|
|
||
| class Pager(object): | ||
| """ | ||
| Generator that takes an endpoint with `.get` and lazily loads items from Server. | ||
| Supports all `RequestOptions` including starting on any page. | ||
| """ | ||
|
|
||
| def __init__(self, endpoint, request_opts=None): | ||
| self._endpoint = endpoint.get | ||
| self._options = request_opts | ||
|
|
||
| # If we have options we could be starting on any page, backfill the count | ||
| if self._options: | ||
| self._count = ((self._options.pagenumber - 1) * self._options.pagesize) | ||
| else: | ||
| self._count = 0 | ||
|
|
||
| def __iter__(self): | ||
| # Fetch the first page | ||
| current_item_list, last_pagination_item = self._endpoint(self._options) | ||
|
|
||
| # Get the rest on demand as a generator | ||
| while self._count < last_pagination_item.total_available: | ||
| if len(current_item_list) == 0: | ||
| current_item_list, last_pagination_item = self._load_next_page(last_pagination_item) | ||
|
|
||
| try: | ||
| yield current_item_list.pop(0) | ||
| self._count += 1 | ||
|
|
||
| except IndexError: | ||
| # The total count on Server changed while fetching exit gracefully | ||
| raise StopIteration | ||
|
|
||
| def _load_next_page(self, last_pagination_item): | ||
| next_page = last_pagination_item.page_number + 1 | ||
| opts = RequestOptions(pagenumber=next_page, pagesize=last_pagination_item.page_size) | ||
| if self._options is not None: | ||
| opts.sort, opts.filter = self._options.sort, self._options.filter | ||
| current_item_list, last_pagination_item = self._endpoint(opts) | ||
| return current_item_list, last_pagination_item |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version='1.0' encoding='UTF-8'?> | ||
| <tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd"> | ||
| <pagination pageNumber="1" pageSize="1" totalAvailable="3" /> | ||
| <workbooks> | ||
| <workbook id="6d13b0ca-043d-4d42-8c9d-3f3313ea3a00" name="Page1Workbook" contentUrl="Page1Workbook" showTabs="false" size="1" createdAt="2016-08-03T20:34:04Z" updatedAt="2016-08-04T17:56:41Z"> | ||
| <project id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" name="default" /> | ||
| <owner id="5de011f8-5aa9-4d5b-b991-f462c8dd6bb7" /> | ||
| <tags /> | ||
| </workbook> | ||
| </workbooks> | ||
| </tsResponse> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version='1.0' encoding='UTF-8'?> | ||
| <tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd"> | ||
| <pagination pageNumber="2" pageSize="1" totalAvailable="3" /> | ||
| <workbooks> | ||
| <workbook id="3cc6cd06-89ce-4fdc-b935-5294135d6d42" name="Page2Workbook" contentUrl="Page2Workbook" showTabs="false" size="26" createdAt="2016-07-26T20:34:56Z" updatedAt="2016-07-26T20:35:05Z"> | ||
| <project id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" name="default" /> | ||
| <owner id="5de011f8-5aa9-4d5b-b991-f462c8dd6bb7" /> | ||
| <tags> | ||
| <tag label="Safari" /> | ||
| <tag label="Sample" /> | ||
| </tags> | ||
| </workbook> | ||
| </workbooks> | ||
| </tsResponse> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?xml version='1.0' encoding='UTF-8'?> | ||
| <tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd"> | ||
| <pagination pageNumber="3" pageSize="1" totalAvailable="3" /> | ||
| <workbooks> | ||
| <workbook id="0413f2d6-387d-4fb6-9823-370d67b1276f" name="Page3Workbook" contentUrl="Page3Workbook" showTabs="false" size="26" createdAt="2016-07-26T20:34:56Z" updatedAt="2016-07-26T20:35:05Z"> | ||
| <project id="63ba565c-f352-41f4-87f5-ba4573fc7c2c" name="default" /> | ||
| <owner id="299fe064-51a5-4e11-93a1-76116239f082" /> | ||
| </workbook> | ||
| </workbooks> | ||
| </tsResponse> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import unittest | ||
| import os | ||
| import requests_mock | ||
| import tableauserverclient as TSC | ||
|
|
||
| TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), 'assets') | ||
|
|
||
| GET_XML_PAGE1 = os.path.join(TEST_ASSET_DIR, 'workbook_get_page_1.xml') | ||
| GET_XML_PAGE2 = os.path.join(TEST_ASSET_DIR, 'workbook_get_page_2.xml') | ||
| GET_XML_PAGE3 = os.path.join(TEST_ASSET_DIR, 'workbook_get_page_3.xml') | ||
|
|
||
|
|
||
| class PagerTests(unittest.TestCase): | ||
| def setUp(self): | ||
| self.server = TSC.Server('http://test') | ||
|
|
||
| # Fake sign in | ||
| self.server._site_id = 'dad65087-b08b-4603-af4e-2887b8aafc67' | ||
| self.server._auth_token = 'j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM' | ||
|
|
||
| self.baseurl = self.server.workbooks.baseurl | ||
|
|
||
| def test_pager_with_no_options(self): | ||
| with open(GET_XML_PAGE1, 'rb') as f: | ||
| page_1 = f.read().decode('utf-8') | ||
| with open(GET_XML_PAGE2, 'rb') as f: | ||
| page_2 = f.read().decode('utf-8') | ||
| with open(GET_XML_PAGE3, 'rb') as f: | ||
| page_3 = f.read().decode('utf-8') | ||
| with requests_mock.mock() as m: | ||
| # Register Pager with default request options | ||
| m.get(self.baseurl, text=page_1) | ||
|
|
||
| # Register Pager with some pages | ||
| m.get(self.baseurl + "?pageNumber=1&pageSize=1", text=page_1) | ||
| m.get(self.baseurl + "?pageNumber=2&pageSize=1", text=page_2) | ||
| m.get(self.baseurl + "?pageNumber=3&pageSize=1", text=page_3) | ||
|
|
||
| # No options should get all 3 | ||
| workbooks = list(TSC.Pager(self.server.workbooks)) | ||
| self.assertTrue(len(workbooks) == 3) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick self.assertEquals(len(workbooks), 3) |
||
|
|
||
| # Let's check that workbook items aren't duplicates | ||
| wb1, wb2, wb3 = workbooks | ||
| self.assertEqual(wb1.name, 'Page1Workbook') | ||
| self.assertEqual(wb2.name, 'Page2Workbook') | ||
| self.assertEqual(wb3.name, 'Page3Workbook') | ||
|
|
||
| def test_pager_with_options(self): | ||
| with open(GET_XML_PAGE1, 'rb') as f: | ||
| page_1 = f.read().decode('utf-8') | ||
| with open(GET_XML_PAGE2, 'rb') as f: | ||
| page_2 = f.read().decode('utf-8') | ||
| with open(GET_XML_PAGE3, 'rb') as f: | ||
| page_3 = f.read().decode('utf-8') | ||
| with requests_mock.mock() as m: | ||
| # Register Pager with some pages | ||
| m.get(self.baseurl + "?pageNumber=1&pageSize=1", text=page_1) | ||
| m.get(self.baseurl + "?pageNumber=2&pageSize=1", text=page_2) | ||
| m.get(self.baseurl + "?pageNumber=3&pageSize=1", text=page_3) | ||
| m.get(self.baseurl + "?pageNumber=1&pageSize=3", text=page_1) | ||
|
|
||
| # Starting on page 2 should get 2 out of 3 | ||
| opts = TSC.RequestOptions(2, 1) | ||
| workbooks = list(TSC.Pager(self.server.workbooks, opts)) | ||
| self.assertTrue(len(workbooks) == 2) | ||
|
|
||
| # Check that the workbooks are the 2 we think they should be | ||
| wb2, wb3 = workbooks | ||
| self.assertEqual(wb2.name, 'Page2Workbook') | ||
| self.assertEqual(wb3.name, 'Page3Workbook') | ||
|
|
||
| # Starting on 1 with pagesize of 3 should get all 3 | ||
| opts = TSC.RequestOptions(1, 3) | ||
| workbooks = list(TSC.Pager(self.server.workbooks, opts)) | ||
| self.assertTrue(len(workbooks) == 3) | ||
| wb1, wb2, wb3 = workbooks | ||
| self.assertEqual(wb1.name, 'Page1Workbook') | ||
| self.assertEqual(wb2.name, 'Page2Workbook') | ||
| self.assertEqual(wb3.name, 'Page3Workbook') | ||
|
|
||
| # Starting on 3 with pagesize of 1 should get the last item | ||
| opts = TSC.RequestOptions(3, 1) | ||
| workbooks = list(TSC.Pager(self.server.workbooks, opts)) | ||
| self.assertTrue(len(workbooks) == 1) | ||
| # Should have the last workbook | ||
| wb3 = workbooks.pop() | ||
| self.assertEqual(wb3.name, 'Page3Workbook') | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why are you registering these if we are not supposed to call them?
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.
This is part of the requests-mock syntax. You register a url that will be called via requests, and the result you'd like returned when this is called (text= arguments), and then requests-mock does magic and returns the text when you make a requests call of the appropriate method to the appropriate url.
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.
Hahahaha. :) I know what it does. My comment was that we don't call these urls in this test so why do we need to register them. :)
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.
My bad ... it does get called (except maybe the first one). OOps