Skip to content

Commit 9e1419b

Browse files
committed
Adding extra parameters for querying Cloud Storage keys.
1 parent f33e557 commit 9e1419b

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

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
@@ -298,9 +298,9 @@ def upload_from_string(self, data, content_type='text/plain'):
298298
"""
299299
string_buffer = StringIO()
300300
string_buffer.write(data)
301-
self.set_contents_from_file(file_obj=string_buffer, rewind=True,
302-
size=string_buffer.len,
303-
content_type=content_type)
301+
self.upload_from_file(file_obj=string_buffer, rewind=True,
302+
size=string_buffer.len,
303+
content_type=content_type)
304304
return self
305305

306306
# NOTE: Alias for boto-like API.
@@ -465,10 +465,11 @@ class _KeyIterator(Iterator):
465465
:type bucket: :class:`gcloud.storage.bucket.Bucket`
466466
:param bucket: The bucket from which to list keys.
467467
"""
468-
def __init__(self, bucket):
468+
def __init__(self, bucket, extra_params=None):
469469
self.bucket = bucket
470470
super(_KeyIterator, self).__init__(
471-
connection=bucket.connection, path=bucket.path + '/o')
471+
connection=bucket.connection, path=bucket.path + '/o',
472+
extra_params=extra_params)
472473

473474
def get_items_from_response(self, response):
474475
"""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)