Skip to content

Commit 50da3a3

Browse files
committed
Preparing to use futures in storage.
Wraps setting/getting of object _properties in custom methods. This will allow centralized detection of a future in a response and will also allow replacing with the value on access if it is ready. Towards googleapis#775
1 parent 028a15d commit 50da3a3

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

gcloud/storage/_helpers.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ def reload(self):
5454
# Pass only '?projection=noAcl' here because 'acl' and related
5555
# are handled via custom endpoints.
5656
query_params = {'projection': 'noAcl'}
57-
self._properties = self.connection.api_request(
57+
api_response = self.connection.api_request(
5858
method='GET', path=self.path, query_params=query_params)
59-
# If the api_request succeeded, we reset changes.
60-
self._changes = set()
59+
self._set_properties(api_response)
6160

6261
def _patch_property(self, name, value):
6362
"""Update field of this object's properties.
@@ -77,6 +76,16 @@ def _patch_property(self, name, value):
7776
self._changes.add(name)
7877
self._properties[name] = value
7978

79+
def _set_properties(self, value):
80+
"""Set the properties for the current object.
81+
82+
:type value: dict
83+
:param value: The properties to be set.
84+
"""
85+
self._properties = value
86+
# If the values are reset, the changes must as well.
87+
self._changes = set()
88+
8089
def patch(self):
8190
"""Sends all changed properties in a PATCH request.
8291
@@ -86,11 +95,10 @@ def patch(self):
8695
# to work properly w/ 'noAcl'.
8796
update_properties = dict((key, self._properties[key])
8897
for key in self._changes)
89-
self._properties = self.connection.api_request(
98+
api_response = self.connection.api_request(
9099
method='PATCH', path=self.path, data=update_properties,
91100
query_params={'projection': 'full'})
92-
# If the api_request succeeded, we reset changes.
93-
self._changes = set()
101+
self._set_properties(api_response)
94102

95103

96104
def _scalar_property(fieldname):

gcloud/storage/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def get_items_from_response(self, response):
227227
for item in response.get('items', []):
228228
name = item.get('name')
229229
bucket = Bucket(name, connection=self.connection)
230-
bucket._properties = item
230+
bucket._set_properties(item)
231231
yield bucket
232232

233233

gcloud/storage/blob.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
386386
if not isinstance(response_content,
387387
six.string_types): # pragma: NO COVER Python3
388388
response_content = response_content.decode('utf-8')
389-
self._properties = json.loads(response_content)
389+
self._set_properties(json.loads(response_content))
390390

391391
def upload_from_filename(self, filename, content_type=None):
392392
"""Upload this blob's contents from the content of a named file.

gcloud/storage/bucket.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def get_items_from_response(self, response):
7575
for item in response.get('items', []):
7676
name = item.get('name')
7777
blob = Blob(name, bucket=self.bucket)
78-
blob._properties = item
78+
blob._set_properties(item)
7979
yield blob
8080

8181

@@ -152,9 +152,10 @@ def create(self, project=None):
152152
'from environment.')
153153

154154
query_params = {'project': project}
155-
self._properties = self.connection.api_request(
155+
api_response = self.connection.api_request(
156156
method='POST', path='/b', query_params=query_params,
157157
data={'name': self.name})
158+
self._set_properties(api_response)
158159

159160
@property
160161
def acl(self):
@@ -220,7 +221,7 @@ def get_blob(self, blob_name):
220221
path=blob.path)
221222
name = response.get('name') # Expect this to be blob_name
222223
blob = Blob(name, bucket=self)
223-
blob._properties = response
224+
blob._set_properties(response)
224225
return blob
225226
except NotFound:
226227
return None
@@ -408,7 +409,7 @@ def copy_blob(self, blob, destination_bucket, new_name=None):
408409
new_blob = Blob(bucket=destination_bucket, name=new_name)
409410
api_path = blob.path + '/copyTo' + new_blob.path
410411
copy_result = self.connection.api_request(method='POST', path=api_path)
411-
new_blob._properties = copy_result
412+
new_blob._set_properties(copy_result)
412413
return new_blob
413414

414415
def upload_file(self, filename, blob_name=None):

gcloud/storage/iterator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_items_from_response(self, response):
2626
items = response.get('items', [])
2727
for item in items:
2828
my_item = MyItemClass(other_arg=True)
29-
my_item._properties = item
29+
my_item._set_properties(item)
3030
yield my_item
3131
3232
You then can use this to get **all** the results from a resource::

0 commit comments

Comments
 (0)