Skip to content

Commit adcc571

Browse files
committed
Adding extra parameters for querying Cloud Storage keys.
1 parent 2b73c7b commit adcc571

4 files changed

Lines changed: 25 additions & 9 deletions

File tree

gcloud/storage/iterator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ class Iterator(object):
3838
:type path: string
3939
:param path: The path to query for the list of items.
4040
"""
41-
def __init__(self, connection, path):
41+
def __init__(self, connection, path, extra_params=None):
4242
self.connection = connection
4343
self.path = path
4444
self.page_number = 0
4545
self.next_page_token = None
46+
self.extra_params = extra_params
4647

4748
def __iter__(self):
4849
"""Iterate through the list of items."""
@@ -69,8 +70,13 @@ def get_query_params(self):
6970
:rtype: dict or None
7071
:returns: A dictionary of query parameters or None if there are none.
7172
"""
73+
result = None
7274
if self.next_page_token:
73-
return {'pageToken': self.next_page_token}
75+
result = {'pageToken': self.next_page_token}
76+
if self.extra_params is not None:
77+
result = result or {}
78+
result.update(self.extra_params)
79+
return result
7480

7581
def get_next_page_response(self):
7682
"""Requests the next page from the path provided.

gcloud/storage/key.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ def upload_from_string(self, data, content_type='text/plain'):
337337
"""
338338
string_buffer = StringIO()
339339
string_buffer.write(data)
340-
self.set_contents_from_file(file_obj=string_buffer, rewind=True,
341-
size=string_buffer.len,
342-
content_type=content_type)
340+
self.upload_from_file(file_obj=string_buffer, rewind=True,
341+
size=string_buffer.len,
342+
content_type=content_type)
343343
return self
344344

345345
# NOTE: Alias for boto-like API.
@@ -452,10 +452,11 @@ class _KeyIterator(Iterator):
452452
:type bucket: :class:`gcloud.storage.bucket.Bucket`
453453
:param bucket: The bucket from which to list keys.
454454
"""
455-
def __init__(self, bucket):
455+
def __init__(self, bucket, extra_params=None):
456456
self.bucket = bucket
457457
super(_KeyIterator, self).__init__(
458-
connection=bucket.connection, path=bucket.path + '/o')
458+
connection=bucket.connection, path=bucket.path + '/o',
459+
extra_params=extra_params)
459460

460461
def get_items_from_response(self, response):
461462
"""Factory method, yields :class:`.storage.key.Key` items from response.

gcloud/storage/test_bucket.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,9 @@ def __init__(self, bucket, name):
334334
self._bucket = bucket
335335
self._name = name
336336

337-
def set_contents_from_filename(self, filename):
337+
def upload_from_filename(self, filename):
338338
_uploaded.append((self._bucket, self._name, filename))
339+
339340
bucket = self._makeOne()
340341
with _Monkey(MUT, Key=_Key):
341342
bucket.upload_file(FILENAME)
@@ -354,8 +355,9 @@ def __init__(self, bucket, name):
354355
self._bucket = bucket
355356
self._name = name
356357

357-
def set_contents_from_filename(self, filename):
358+
def upload_from_filename(self, filename):
358359
_uploaded.append((self._bucket, self._name, filename))
360+
359361
bucket = self._makeOne()
360362
with _Monkey(MUT, Key=_Key):
361363
bucket.upload_file(FILENAME, KEY)

gcloud/storage/test_iterator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ def test_get_query_params_w_token(self):
7575
self.assertEqual(iterator.get_query_params(),
7676
{'pageToken': TOKEN})
7777

78+
def test_get_query_params_extra_params(self):
79+
connection = _Connection()
80+
PATH = '/foo'
81+
extra_params = {'key': 'val'}
82+
iterator = self._makeOne(connection, PATH, extra_params=extra_params)
83+
self.assertEqual(iterator.get_query_params(), extra_params)
84+
7885
def test_get_next_page_response_new_no_token_in_response(self):
7986
PATH = '/foo'
8087
TOKEN = 'token'

0 commit comments

Comments
 (0)