10000 Making iterators accept client instead of connection. · dhermes/google-cloud-python@30c94de · GitHub
[go: up one dir, main page]

Skip to content

Commit 30c94de

Browse files
committed
Making iterators accept client instead of connection.
1 parent e627eaa commit 30c94de

File tree

6 files changed

+61
-31
lines changed

6 files changed

+61
-31
lines changed

gcloud/iterator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def get_items_from_response(self, response):
4848
class Iterator(object):
4949
"""A generic class for iterating through Cloud JSON APIs list responses.
5050
51-
:type connection: :class:`gcloud.connection.Connection`
52-
:param connection: The connection to use to make requests.
51+
:type client: :class:`gcloud.client.Client`
52+
:param client: The client, which owns a connection to make requests.
5353
5454
:type path: string
5555
:param path: The path to query for the list of items.
@@ -61,8 +61,8 @@ class Iterator(object):
6161
PAGE_TOKEN = 'pageToken'
6262
RESERVED_PARAMS = frozenset([PAGE_TOKEN])
6363

64-
def __init__(self, connection, path, extra_params=None):
65-
self.connection = connection
64+
def __init__(self, client, path, extra_params=None):
65+
self.client = client
6666
self.path = path
6767
self.page_number = 0
6868
self.next_page_token = None
@@ -111,7 +111,7 @@ def get_next_page_response(self):
111111
if not self.has_next_page():
112112
raise RuntimeError('No more pages. Try resetting the iterator.')
113113

114-
response = self.connection.api_request(
114+
response = self.client.connection.api_request(
115115
method='GET', path=self.path, query_params=self.get_query_params())
116116

117117
self.page_number += 1

gcloud/storage/bucket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, bucket, extra_params=None, client=None):
5454
self.prefixes = set()
5555
self._current_prefixes = None
5656
super(_BlobIterator, self).__init__(
57-
connection=client.connection, path=bucket.path + '/o',
57+
client=client, path=bucket.path + '/o',
5858
extra_params=extra_params)
5959

6060
def get_items_from_response(self, response):

gcloud/storage/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def list_buckets(self, max_results=None, page_token=None, prefix=None,
170170
if fields is not None:
171171
extra_params['fields'] = fields
172172

173-
result = _BucketIterator(connection=self.connection,
173+
result = _BucketIterator(client=self,
174174
extra_params=extra_params)
175175
# Page token must be handled specially since the base `Iterator`
176176
# class has it as a reserved property.
@@ -186,15 +186,15 @@ class _BucketIterator(Iterator):
186186
helper methods on :class:`gcloud.storage.connection.Connection`
187187
objects.
188188
189-
:type connection: :class:`gcloud.storage.connection.Connection`
190-
:param connection: The connection to use for querying the list of buckets.
189+
:type client: :class:`gcloud.storage.client.Client`
190+
:param client: The client to use for making connections.
191191
192192
:type extra_params: dict or ``NoneType``
193193
:param extra_params: Extra query string parameters for the API call.
194194
"""
195195

196-
def __init__(self, connection, extra_params=None):
197-
super(_BucketIterator, self).__init__(connection=connection, path='/b',
196+
def __init__(self, client, extra_params=None):
197+
super(_BucketIterator, self).__init__(client=client, path='/b',
198198
extra_params=extra_params)
199199

200200
def get_items_from_response(self, response):
@@ -205,6 +205,6 @@ def get_items_from_response(self, response):
205205
"""
206206
for item in response.get('items', []):
207207
name = item.get('name')
208-
bucket = Bucket(None, name)
208+
bucket = Bucket(self.client, name)
209209
bucket._set_properties(item)
210210
yield bucket

gcloud/storage/test_bucket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_ctor_w_explicit_connection(self):
3232
bucket = _Bucket()
3333
iterator = self._makeOne(bucket, client=client)
3434
self.assertTrue(iterator.bucket is bucket)
35-
self.assertTrue(iterator.connection is connection)
35+
self.assertTrue(iterator.client is client)
3636
self.assertEqual(iterator.path, '%s/o' % bucket.path)
3737
self.assertEqual(iterator.page_number, 0)
3838
self.assertEqual(iterator.next_page_token, None)

gcloud/storage/test_client.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,22 +301,26 @@ def _makeOne(self, *args, **kw):
301301

302302
def test_ctor(self):
303303
connection = object()
304-
iterator = self._makeOne(connection)
304+
client = _Client(connection)
305+
iterator = self._makeOne(client)
305306
self.assertEqual(iterator.path, '/b')
306307
self.assertEqual(iterator.page_number, 0)
307308
self.assertEqual(iterator.next_page_token, None)
309+
self.assertTrue(iterator.client is client)
308310

309311
def test_get_items_from_response_empty(self):
310312
connection = object()
311-
iterator = self._makeOne(connection)
313+
client = _Client(connection)
314+
iterator = self._makeOne(client)
312315
self.assertEqual(list(iterator.get_items_from_response({})), [])
313316

314317
def test_get_items_from_response_non_empty(self):
315318
from gcloud.storage.bucket import Bucket
316319
BLOB_NAME = 'blob-name'
317320
response = {'items': [{'name': BLOB_NAME}]}
318321
connection = object()
319-
iterator = self._makeOne(connection)
322+
client = _Client(connection)
323+
iterator = self._makeOne(client)
320324
buckets = list(iterator.get_items_from_response(response))
321325
self.assertEqual(len(buckets), 1)
322326
bucket = buckets[0]
@@ -349,3 +353,9 @@ def __init__(self, headers, content):
349353
def request(self, **kw):
350354
self._called_with = kw
351355
return self._response, self._content
356+
357+
358+
class _Client(object):
359+
360+
def __init__(self, connection):
361+
self.connection = connection

gcloud/test_iterator.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ def _makeOne(self, *args, **kw):
2626

2727
def test_ctor(self):
2828
connection = _Connection()
29+
client = _Client(connection)
2930
PATH = '/foo'
30-
iterator = self._makeOne(connection, PATH)
31-
self.assertTrue(iterator.connection is connection)
31+
iterator = self._makeOne(client, PATH)
32+
self.assertTrue(iterator.client is client)
3233
self.assertEqual(iterator.path, PATH)
3334
self.assertEqual(iterator.page_number, 0)
3435
self.assertEqual(iterator.next_page_token, None)
@@ -44,7 +45,8 @@ def _get_items(response):
4445
for item in response.get('items', []):
4546
yield ITEMS[item['name']]
4647
connection = _Connection({'items': [{'name': KEY1}, {'name': KEY2}]})
47-
iterator = self._makeOne(connection, PATH)
48+
client = _Client(connection)
49+
iterator = self._makeOne(client, PATH)
4850
iterator.get_items_from_response = _get_items
4951
self.assertEqual(list(iterator), [ITEM1, ITEM2])
5052
kw, = connection._requested
@@ -54,54 +56,61 @@ def _get_items(response):
5456

5557
def test_has_next_page_new(self):
5658
connection = _Connection()
59+
client = _Client(connection)
5760
PATH = '/foo'
58-
iterator = self._makeOne(connection, PATH)
61+
iterator = self._makeOne(client, PATH)
5962
self.assertTrue(iterator.has_next_page())
6063

6164
def test_has_next_page_w_number_no_token(self):
6265
connection = _Connection()
66+
client = _Client(connection)
6367
PATH = '/foo'
64-
iterator = self._makeOne(connection, PATH)
68+
iterator = self._makeOne(client, PATH)
6569
iterator.page_number = 1
6670
self.assertFalse(iterator.has_next_page())
6771

6872
def test_has_next_page_w_number_w_token(self):
6973
connection = _Connection()
74+
client = _Client(connection)
7075
PATH = '/foo'
7176
TOKEN = 'token'
72-
iterator = self._makeOne(connection, PATH)
77+
iterator = self._makeOne(client, PATH)
7378
iterator.page_number = 1
7479
iterator.next_page_token = TOKEN
7580
self.assertTrue(iterator.has_next_page())
7681

7782
def test_get_query_params_no_token(self):
7883
connection = _Connection()
84+
client = _Client(connection)
7985
PATH = '/foo'
80-
iterator = self._makeOne(connection, PATH)
86+
iterator = self._makeOne(client, PATH)
8187
self.assertEqual(iterator.get_query_params(), {})
8288

8389
def test_get_query_params_w_token(self):
8490
connection = _Connection()
91+
client = _Client(connection)
8592
PATH = '/foo'
8693
TOKEN = 'token'
87-
iterator = self._makeOne(connection, PATH)
94+
iterator = self._makeOne(client, PATH)
8895
iterator.next_page_token = TOKEN
8996
self.assertEqual(iterator.get_query_params(),
9097
{'pageToken': TOKEN})
9198

9299
def test_get_query_params_extra_params(self):
93100
connection = _Connection()
101+
client = _Client(connection)
94102
PATH = '/foo'
95103
extra_params = {'key': 'val'}
96-
iterator = self._makeOne(connection, PATH, extra_params=extra_params)
104+
iterator = self._makeOne(client, PATH, extra_params=extra_params)
97105
self.assertEqual(iterator.get_query_params(), extra_params)
98106

99107
def test_get_query_params_w_token_and_extra_params(self):
100108
connection = _Connection()
109+
client = _Client(connection)
101110
PATH = '/foo'
102111
TOKEN = 'token'
103112
extra_params = {'key': 'val'}
104-
iterator = self._makeOne(connection, PATH, extra_params=extra_params)
113+
iterator = self._makeOne(client, PATH, extra_params=extra_params)
105114
iterator.next_page_token = TOKEN
106115

107116
expected_query = extra_params.copy()
@@ -110,9 +119,10 @@ def test_get_query_params_w_token_and_extra_params(self):
110119

111120
def test_get_query_params_w_token_collision(self):
112121
connection = _Connection()
122+
client = _Client(connection)
113123
PATH = '/foo'
114124
extra_params = {'pageToken': 'val'}
115-
self.assertRaises(ValueError, self._makeOne, connection, PATH,
125+
self.assertRaises(ValueError, self._makeOne, client, PATH,
116126
extra_params=extra_params)
117127

118128
def test_get_next_page_response_new_no_token_in_response(self):
@@ -122,7 +132,8 @@ def test_get_next_page_response_new_no_token_in_response(self):
122132
KEY2 = 'key2'
123133
connection = _Connection({'items': [{'name': KEY1}, {'name': KEY2}],
124134
'nextPageToken': TOKEN})
125-
iterator = self._makeOne(connection, PATH)
135+
client = _Client(connection)
136+
iterator = self._makeOne(client, PATH)
126137
response = iterator.get_next_page_response()
127138
self.assertEqual(response['items'], [{'name': KEY1}, {'name': KEY2}])
128139
self.assertEqual(iterator.page_number, 1)
@@ -134,16 +145,18 @@ def test_get_next_page_response_new_no_token_in_response(self):
134145

135146
def test_get_next_page_response_no_token(self):
136147
connection = _Connection()
148+
client = _Client(connection)
137149
PATH = '/foo'
138-
iterator = self._makeOne(connection, PATH)
150+
iterator = self._makeOne(client, PATH)
139151
iterator.page_number = 1
140152
self.assertRaises(RuntimeError, iterator.get_next_page_response)
141153

142154
def test_reset(self):
143155
connection = _Connection()
156+
client = _Client(connection)
144157
PATH = '/foo'
145158
TOKEN = 'token'
146-
iterator = self._makeOne(connection, PATH)
159+
iterator = self._makeOne(client, PATH)
147160
iterator.page_number = 1
148161
iterator.next_page_token = TOKEN
149162
iterator.reset()
@@ -153,7 +166,8 @@ def test_reset(self):
153166
def test_get_items_from_response_raises_NotImplementedError(self):
154167
PATH = '/foo'
155168
connection = _Connection()
156-
iterator = self._makeOne(connection, PATH)
169+
client = _Client(connection)
170+
iterator = self._makeOne(client, PATH)
157171
self.assertRaises(NotImplementedError,
158172
iterator.get_items_from_response, object())
159173

@@ -168,3 +182,9 @@ def api_request(self, **kw):
168182
self._requested.append(kw)
169183
response, self._responses = self._responses[0], self._responses[1:]
170184
return response
185+
186+
187+
class _Client(object):
188+
189+
def __init__(self, connection):
190+
self.connection = connection

0 commit comments

Comments
 (0)
0