Skip to content

Commit

Permalink
refactor: add 'Client._list_resource' method
Browse files Browse the repository at this point in the history
Toward #38.
  • Loading branch information
tseaver committed Jun 8, 2021
1 parent ef764ac commit f30af4d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
25 changes: 25 additions & 0 deletions google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,31 @@ def _get_resource(
_target_object=_target_object,
)

def _list_resource(
self,
path,
item_to_value,
page_token=None,
max_results=None,
extra_params=None,
page_start=page_iterator._do_nothing_page_start,
timeout=_DEFAULT_TIMEOUT,
retry=DEFAULT_RETRY,
):
api_request = functools.partial(
self._connection.api_request, timeout=timeout, retry=retry
)
return page_iterator.HTTPIterator(
client=self,
api_request=api_request,
path=path,
item_to_value=item_to_value,
page_token=page_token,
max_results=max_results,
extra_params=extra_params,
page_start=page_start,
)

def _patch_resource(
self,
path,
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,74 @@ def test__get_resource_hit_w_explicit(self):
_target_object=target,
)

def test__list_resource_w_defaults(self):
import functools
from google.api_core.page_iterator import HTTPIterator
from google.api_core.page_iterator import _do_nothing_page_start

project = "PROJECT"
path = "/path/to/list/resource"
item_to_value = mock.Mock(spec=[])
credentials = _make_credentials()
client = self._make_one(project=project, credentials=credentials)
connection = client._base_connection = _make_connection()

iterator = client._list_resource(path=path, item_to_value=item_to_value,)

self.assertIsInstance(iterator, HTTPIterator)
self.assertIs(iterator.client, client)
self.assertIsInstance(iterator.api_request, functools.partial)
self.assertIs(iterator.api_request.func, connection.api_request)
self.assertEqual(iterator.api_request.args, ())
expected_keywords = {
"timeout": self._get_default_timeout(),
"retry": DEFAULT_RETRY,
}
self.assertEqual(iterator.api_request.keywords, expected_keywords)
self.assertEqual(iterator.path, path)
self.assertEqual(iterator.next_page_token, None)
self.assertEqual(iterator.max_results, None)
self.assertIs(iterator._page_start, _do_nothing_page_start)

def test__list_resource_w_explicit(self):
import functools
from google.api_core.page_iterator import HTTPIterator

project = "PROJECT"
path = "/path/to/list/resource"
item_to_value = mock.Mock(spec=[])
page_token = "PAGE-TOKEN"
max_results = 47
extra_params = {"foo": "Foo"}
page_start = mock.Mock(spec=[])
credentials = _make_credentials()
client = self._make_one(project=project, credentials=credentials)
connection = client._base_connection = _make_connection()

iterator = client._list_resource(
path=path,
item_to_value=item_to_value,
page_token=page_token,
max_results=max_results,
extra_params=extra_params,
page_start=page_start,
)

self.assertIsInstance(iterator, HTTPIterator)
self.assertIs(iterator.client, client)
self.assertIsInstance(iterator.api_request, functools.partial)
self.assertIs(iterator.api_request.func, connection.api_request)
self.assertEqual(iterator.api_request.args, ())
expected_keywords = {
"timeout": self._get_default_timeout(),
"retry": DEFAULT_RETRY,
}
self.assertEqual(iterator.api_request.keywords, expected_keywords)
self.assertEqual(iterator.path, path)
self.assertEqual(iterator.next_page_token, page_token)
self.assertEqual(iterator.max_results, max_results)
self.assertIs(iterator._page_start, page_start)

def test__patch_resource_miss_w_defaults(self):
from google.cloud.exceptions import NotFound

Expand Down

0 comments on commit f30af4d

Please sign in to comment.