Skip to content

Commit 9ff5c10

Browse files
committed
Fixes bug that occurs when using iter=True with arguments
1 parent ef030ec commit 9ff5c10

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

SoftLayer/API.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,21 @@ def call(self, service, method, *args, **kwargs):
188188

189189
__call__ = call
190190

191-
def iter_call(self, service, method,
192-
chunk=100, limit=None, offset=0, *args, **kwargs):
191+
def iter_call(self, service, method, *args, **kwargs):
193192
"""A generator that deals with paginating through results.
194193
195194
:param service: the name of the SoftLayer API service
196195
:param method: the method to call on the service
197-
:param integer chunk: result size for each API call
196+
:param integer chunk: result size for each API call (defaults to 100)
198197
:param \\*args: same optional arguments that ``Service.call`` takes
199198
:param \\*\\*kwargs: same optional keyword arguments that
200199
``Service.call`` takes
201200
202201
"""
202+
chunk = kwargs.pop('chunk', 100)
203+
limit = kwargs.pop('limit', None)
204+
offset = kwargs.pop('offset', 0)
205+
203206
if chunk <= 0:
204207
raise AttributeError("Chunk size should be greater than zero.")
205208

@@ -217,6 +220,7 @@ def iter_call(self, service, method,
217220
# Don't over-fetch past the given limit
218221
if chunk + result_count > limit:
219222
chunk = limit - result_count
223+
220224
results = self.call(service, method,
221225
offset=offset, limit=chunk, *args, **kwargs)
222226

SoftLayer/tests/api_tests.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,13 @@ def test_iterate(self, _iter_call):
127127

128128
@mock.patch('SoftLayer.API.Client.iter_call')
129129
def test_service_iter_call(self, _iter_call):
130-
self.client['SERVICE'].iter_call('METHOD')
131-
_iter_call.assert_called_with('SERVICE', 'METHOD')
130+
self.client['SERVICE'].iter_call('METHOD', 'ARG')
131+
_iter_call.assert_called_with('SERVICE', 'METHOD', 'ARG')
132+
133+
@mock.patch('SoftLayer.API.Client.iter_call')
134+
def test_service_iter_call(self, _iter_call):
135+
self.client['SERVICE'].iter_call('METHOD', 'ARG', chunk=2)
136+
_iter_call.assert_called_with('SERVICE', 'METHOD', 'ARG', chunk=2)
132137

133138
@mock.patch('SoftLayer.API.Client.call')
134139
def test_iter_call(self, _call):
@@ -176,12 +181,17 @@ def test_iter_call(self, _call):
176181

177182
# chunk=25, limit=30, offset=12
178183
_call.side_effect = [list(range(0, 25)), list(range(25, 30))]
179-
result = list(self.client.iter_call(
180-
'SERVICE', 'METHOD', iter=True, limit=30, chunk=25, offset=12))
184+
result = list(self.client.iter_call('SERVICE', 'METHOD', 'ARG',
185+
iter=True,
186+
limit=30,
187+
chunk=25,
188+
offset=12))
181189
self.assertEqual(list(range(30)), result)
182190
_call.assert_has_calls([
183-
mock.call('SERVICE', 'METHOD', iter=False, limit=25, offset=12),
184-
mock.call('SERVICE', 'METHOD', iter=False, limit=5, offset=37),
191+
mock.call('SERVICE', 'METHOD', 'ARG',
192+
iter=False, limit=25, offset=12),
193+
mock.call('SERVICE', 'METHOD', 'ARG',
194+
iter=False, limit=5, offset=37),
185195
])
186196

187197
# Chunk size of 0 is invalid

0 commit comments

Comments
 (0)