Skip to content

Commit

Permalink
refactor: add / use 'Client._list_resource' method (#446)
Browse files Browse the repository at this point in the history
* Adjust tests for 'Bucket.list_blobs' not to depend on anything but calling 'Client.list_blobs'.
* Provide explicit coverage for 'client._item_to_bucket' and 'client._item_to_hmack_key_metadata' helpers.
* Provide explicit coverage for 'bucket._item_to_notification'.

Toward #38
  • Loading branch information
tseaver committed Jun 8, 2021
1 parent 2232f38 commit 6bd8a20
Show file tree
Hide file tree
Showing 4 changed files with 541 additions and 506 deletions.
12 changes: 2 additions & 10 deletions google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
import base64
import copy
import datetime
import functools
import json
import warnings

import six
from six.moves.urllib.parse import urlsplit

from google.api_core import page_iterator
from google.api_core import datetime_helpers
from google.cloud._helpers import _datetime_to_rfc3339
from google.cloud._helpers import _NOW
Expand Down Expand Up @@ -1392,14 +1390,8 @@ def list_notifications(
"""
client = self._require_client(client)
path = self.path + "/notificationConfigs"
api_request = functools.partial(
client._connection.api_request, timeout=timeout, retry=retry
)
iterator = page_iterator.HTTPIterator(
client=client,
api_request=api_request,
path=path,
item_to_value=_item_to_notification,
iterator = client._list_resource(
path, _item_to_notification, timeout=timeout, retry=retry,
)
iterator.bucket = self
return iterator
Expand Down
64 changes: 38 additions & 26 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 Expand Up @@ -1214,14 +1239,9 @@ def list_blobs(
extra_params["userProject"] = bucket.user_project

path = bucket.path + "/o"
api_request = functools.partial(
self._connection.api_request, timeout=timeout, retry=retry
)
iterator = page_iterator.HTTPIterator(
client=self,
api_request=api_request,
path=path,
item_to_value=_item_to_blob,
iterator = self._list_resource(
path,
_item_to_blob,
page_token=page_token,
max_results=max_results,
extra_params=extra_params,
Expand Down Expand Up @@ -1328,18 +1348,14 @@ def list_buckets(
if fields is not None:
extra_params["fields"] = fields

api_request = functools.partial(
self._connection.api_request, retry=retry, timeout=timeout
)

return page_iterator.HTTPIterator(
client=self,
api_request=api_request,
path="/b",
item_to_value=_item_to_bucket,
return self._list_resource(
"/b",
_item_to_bucket,
page_token=page_token,
max_results=max_results,
extra_params=extra_params,
timeout=timeout,
retry=retry,
)

def create_hmac_key(
Expand Down Expand Up @@ -1476,17 +1492,13 @@ def list_hmac_keys(
if user_project is not None:
extra_params["userProject"] = user_project

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_hmac_key_metadata,
return self._list_resource(
path,
_item_to_hmac_key_metadata,
max_results=max_results,
extra_params=extra_params,
timeout=timeout,
retry=retry,
)

def get_hmac_key_metadata(
Expand Down

0 comments on commit 6bd8a20

Please sign in to comment.