Skip to content

Commit

Permalink
Implementation hiding in Iterator class.
Browse files Browse the repository at this point in the history
Making JSON API call helpers non-public. This reduces the
interface to methods relevant to iterating.
  • Loading branch information
dhermes committed Oct 12, 2016
1 parent c92961d commit d9824c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
9 changes: 5 additions & 4 deletions core/google/cloud/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def _update_page(self):
if self.page is not None and self.page.remaining > 0:
return
if self.has_next_page():
response = self.get_next_page_response()
response = self._get_next_page_response()
self._page = self.PAGE_CLASS(self, response)
else:
raise StopIteration
Expand Down Expand Up @@ -247,7 +247,7 @@ def has_next_page(self):

return self.next_page_token is not None

def get_query_params(self):
def _get_query_params(self):
"""Getter for query parameters for the next request.
:rtype: dict
Expand All @@ -261,14 +261,15 @@ def get_query_params(self):
result.update(self.extra_params)
return result

def get_next_page_response(self):
def _get_next_page_response(self):
"""Requests the next page from the path provided.
:rtype: dict
:returns: The parsed JSON response of the next page's contents.
"""
response = self.client.connection.api_request(
method='GET', path=self.path, query_params=self.get_query_params())
method='GET', path=self.path,
query_params=self._get_query_params())

self.page_number += 1
self.next_page_token = response.get('nextPageToken')
Expand Down
24 changes: 12 additions & 12 deletions core/unit_tests/test_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,24 +206,24 @@ def test_has_next_page_w_max_results_done(self):
iterator.num_results = iterator.max_results
self.assertFalse(iterator.has_next_page())

def test_get_query_params_no_token(self):
def test__get_query_params_no_token(self):
connection = _Connection()
client = _Client(connection)
path = '/foo'
iterator = self._makeOne(client, path=path)
self.assertEqual(iterator.get_query_params(), {})
self.assertEqual(iterator._get_query_params(), {})

def test_get_query_params_w_token(self):
def test__get_query_params_w_token(self):
connection = _Connection()
client = _Client(connection)
path = '/foo'
token = 'token'
iterator = self._makeOne(client, path=path)
iterator.next_page_token = token
self.assertEqual(iterator.get_query_params(),
self.assertEqual(iterator._get_query_params(),
{'pageToken': token})

def test_get_query_params_w_max_results(self):
def test__get_query_params_w_max_results(self):
connection = _Connection()
client = _Client(connection)
path = '/foo'
Expand All @@ -232,18 +232,18 @@ def test_get_query_params_w_max_results(self):
max_results=max_results)
iterator.num_results = 1
local_max = max_results - iterator.num_results
self.assertEqual(iterator.get_query_params(),
self.assertEqual(iterator._get_query_params(),
{'maxResults': local_max})

def test_get_query_params_extra_params(self):
def test__get_query_params_extra_params(self):
connection = _Connection()
client = _Client(connection)
path = '/foo'
extra_params = {'key': 'val'}
iterator = self._makeOne(client, path=path, extra_params=extra_params)
self.assertEqual(iterator.get_query_params(), extra_params)
self.assertEqual(iterator._get_query_params(), extra_params)

def test_get_query_params_w_token_and_extra_params(self):
def test__get_query_params_w_token_and_extra_params(self):
connection = _Connection()
client = _Client(connection)
path = '/foo'
Expand All @@ -254,9 +254,9 @@ def test_get_query_params_w_token_and_extra_params(self):

expected_query = extra_params.copy()
expected_query.update({'pageToken': token})
self.assertEqual(iterator.get_query_params(), expected_query)
self.assertEqual(iterator._get_query_params(), expected_query)

def test_get_next_page_response_new_no_token_in_response(self):
def test__get_next_page_response_new_no_token_in_response(self):
path = '/foo'
token = 'token'
key1 = 'key1'
Expand All @@ -265,7 +265,7 @@ def test_get_next_page_response_new_no_token_in_response(self):
'nextPageToken': token})
client = _Client(connection)
iterator = self._makeOne(client, path=path)
response = iterator.get_next_page_response()
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)
Expand Down

0 comments on commit d9824c6

Please sign in to comment.