Skip to content
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: add / use 'Client._list_resource' method #446

Merged
merged 5 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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