Skip to content

Commit

Permalink
Preparing to use futures in storage.
Browse files Browse the repository at this point in the history
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 #775
  • Loading branch information
dhermes committed Apr 10, 2015
1 parent 028a15d commit 50da3a3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
20 changes: 14 additions & 6 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ def reload(self):
# Pass only '?projection=noAcl' here because 'acl' and related
# are handled via custom endpoints.
query_params = {'projection': 'noAcl'}
self._properties = self.connection.api_request(
api_response = self.connection.api_request(
method='GET', path=self.path, query_params=query_params)
# If the api_request succeeded, we reset changes.
self._changes = set()
self._set_properties(api_response)

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

def _set_properties(self, value):
"""Set the properties for the current object.
:type value: dict
:param value: The properties to be set.
"""
self._properties = value
# If the values are reset, the changes must as well.
self._changes = set()

def patch(self):
"""Sends all changed properties in a PATCH request.
Expand All @@ -86,11 +95,10 @@ def patch(self):
# to work properly w/ 'noAcl'.
update_properties = dict((key, self._properties[key])
for key in self._changes)
self._properties = self.connection.api_request(
api_response = self.connection.api_request(
method='PATCH', path=self.path, data=update_properties,
query_params={'projection': 'full'})
# If the api_request succeeded, we reset changes.
self._changes = set()
self._set_properties(api_response)


def _scalar_property(fieldname):
Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def get_items_from_response(self, response):
for item in response.get('items', []):
name = item.get('name')
bucket = Bucket(name, connection=self.connection)
bucket._properties = item
bucket._set_properties(item)
yield bucket


Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
if not isinstance(response_content,
six.string_types): # pragma: NO COVER Python3
response_content = response_content.decode('utf-8')
self._properties = json.loads(response_content)
self._set_properties(json.loads(response_content))

def upload_from_filename(self, filename, content_type=None):
"""Upload this blob's contents from the content of a named file.
Expand Down
9 changes: 5 additions & 4 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_items_from_response(self, response):
for item in response.get('items', []):
name = item.get('name')
blob = Blob(name, bucket=self.bucket)
blob._properties = item
blob._set_properties(item)
yield blob


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

query_params = {'project': project}
self._properties = self.connection.api_request(
api_response = self.connection.api_request(
method='POST', path='/b', query_params=query_params,
data={'name': self.name})
self._set_properties(api_response)

@property
def acl(self):
Expand Down Expand Up @@ -220,7 +221,7 @@ def get_blob(self, blob_name):
path=blob.path)
name = response.get('name') # Expect this to be blob_name
blob = Blob(name, bucket=self)
blob._properties = response
blob._set_properties(response)
return blob
except NotFound:
return None
Expand Down Expand Up @@ -408,7 +409,7 @@ def copy_blob(self, blob, destination_bucket, new_name=None):
new_blob = Blob(bucket=destination_bucket, name=new_name)
api_path = blob.path + '/copyTo' + new_blob.path
copy_result = self.connection.api_request(method='POST', path=api_path)
new_blob._properties = copy_result
new_blob._set_properties(copy_result)
return new_blob

def upload_file(self, filename, blob_name=None):
Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_items_from_response(self, response):
items = response.get('items', [])
for item in items:
my_item = MyItemClass(other_arg=True)
my_item._properties = item
my_item._set_properties(item)
yield my_item
You then can use this to get **all** the results from a resource::
Expand Down

0 comments on commit 50da3a3

Please sign in to comment.