Skip to content

Commit

Permalink
Update get_bucket to take a Bucket object or string (#7856)
Browse files Browse the repository at this point in the history
* Updated class method get_bucket to take Bucket object as part of Storage Client redesign.
  • Loading branch information
lbristol88 authored and tswast committed May 6, 2019
1 parent a017937 commit 47db1c6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 17 deletions.
55 changes: 40 additions & 15 deletions storage/google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,28 +202,53 @@ def batch(self):
"""
return Batch(client=self)

def get_bucket(self, bucket_name):
"""Get a bucket by name.
def get_bucket(self, bucket_or_name):
"""API call: retrieve a bucket via a GET request.
If the bucket isn't found, this will raise a
:class:`google.cloud.exceptions.NotFound`.
See
https://cloud.google.com/storage/docs/json_api/v1/buckets/get
For example:
Args:
bucket_or_name (Union[ \
:class:`~google.cloud.storage.bucket.Bucket`, \
str, \
]):
The bucket resource to pass or name to create.
.. literalinclude:: snippets.py
:start-after: [START get_bucket]
:end-before: [END get_bucket]
Returns:
google.cloud.storage.bucket.Bucket
The bucket matching the name provided.
This implements "storage.buckets.get".
Raises:
google.cloud.exceptions.NotFound
If the bucket is not found.
:type bucket_name: str
:param bucket_name: The name of the bucket to get.
Examples:
Retrieve a bucket using a string.
.. literalinclude:: snippets.py
:start-after: [START get_bucket]
:end-before: [END get_bucket]
Get a bucket using a resource.
>>> from google.cloud import storage
>>> client = storage.Client()
>>> # Set properties on a plain resource object.
>>> bucket = client.get_bucket("my-bucket-name")
>>> # Time passes. Another program may have modified the bucket
... # in the meantime, so you want to get the latest state.
>>> bucket = client.get_bucket(bucket) # API request.
:rtype: :class:`google.cloud.storage.bucket.Bucket`
:returns: The bucket matching the name provided.
:raises: :class:`google.cloud.exceptions.NotFound`
"""
bucket = Bucket(self, name=bucket_name)

bucket = None
if isinstance(bucket_or_name, Bucket):
bucket = bucket_or_name
else:
bucket = Bucket(self, name=bucket_or_name)

bucket.reload(client=self)
return bucket
Expand Down
66 changes: 64 additions & 2 deletions storage/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def test_batch(self):
self.assertIsInstance(batch, Batch)
self.assertIs(batch._client, client)

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

PROJECT = "PROJECT"
Expand Down Expand Up @@ -290,7 +290,7 @@ def test_get_bucket_miss(self):
method="GET", url=URI, data=mock.ANY, headers=mock.ANY
)

def test_get_bucket_hit(self):
def test_get_bucket_with_string_hit(self):
from google.cloud.storage.bucket import Bucket

PROJECT = "PROJECT"
Expand Down Expand Up @@ -320,6 +320,68 @@ def test_get_bucket_hit(self):
method="GET", url=URI, data=mock.ANY, headers=mock.ANY
)

def test_get_bucket_with_object_miss(self):
from google.cloud.exceptions import NotFound
from google.cloud.storage.bucket import Bucket

project = "PROJECT"
credentials = _make_credentials()
client = self._make_one(project=project, credentials=credentials)

nonesuch = "nonesuch"
bucket_obj = Bucket(client, nonesuch)
URI = "/".join(
[
client._connection.API_BASE_URL,
"storage",
client._connection.API_VERSION,
"b",
"nonesuch?projection=noAcl",
]
)
http = _make_requests_session(
[_make_json_response({}, status=http_client.NOT_FOUND)]
)
client._http_internal = http

with self.assertRaises(NotFound):
client.get_bucket(bucket_obj)

http.request.assert_called_once_with(
method="GET", url=URI, data=mock.ANY, headers=mock.ANY
)

def test_get_bucket_with_object_hit(self):
from google.cloud.storage.bucket import Bucket

project = "PROJECT"
credentials = _make_credentials()
client = self._make_one(project=project, credentials=credentials)

bucket_name = "bucket-name"
bucket_obj = Bucket(client, bucket_name)
URI = "/".join(
[
client._connection.API_BASE_URL,
"storage",
client._connection.API_VERSION,
"b",
"%s?projection=noAcl" % (bucket_name,),
]
)

data = {"name": bucket_name}
http = _make_requests_session([_make_json_response(data)])
client._http_internal = http

bucket = client.get_bucket(bucket_obj)

self.assertIsInstance(bucket, Bucket)
self.assertEqual(bucket.name, bucket_name)
http.request.assert_called_once_with(
method="GET", url=URI, data=mock.ANY, headers=mock.ANY
)

def test_lookup_bucket_miss(self):
PROJECT = "PROJECT"
CREDENTIALS = _make_credentials()
Expand Down

0 comments on commit 47db1c6

Please sign in to comment.