Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator #66

Merged
merged 2 commits into from
Mar 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion SoftLayer/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def __getitem__(self, name):
name = self._prefix + name
return Service(self, name)

def __call__(self, service, method, *args, **kwargs):
def __call__(self, *args, **kwargs):
""" Perform a SoftLayer API call.

:param service: the name of the SoftLayer API service
Expand All @@ -218,6 +218,12 @@ def __call__(self, service, method, *args, **kwargs):
:param dict **kwargs: response-level arguments (limit, offset, etc.)

"""
if kwargs.get('iter'):
return self.iter_call(*args, **kwargs)
else:
return self.call(*args, **kwargs)

def call(self, service, method, *args, **kwargs):
objectid = kwargs.get('id')
objectmask = kwargs.get('mask')
objectfilter = kwargs.get('filter')
Expand Down Expand Up @@ -263,6 +269,46 @@ def __call__(self, service, method, *args, **kwargs):
http_headers=http_headers, timeout=self.timeout,
verbose=self.verbose)

def iter_call(self, service, method,
chunk=100, limit=None, offset=0, *args, **kwargs):
if chunk <= 0:
raise AttributeError("Chunk size should be greater than zero.")

if limit:
chunk = min(chunk, limit)

result_count = 0
while True:
if limit:
# We've reached the end of the results
if result_count >= limit:
break

# Don't over-fetch past the given limit
if chunk + result_count > limit:
chunk = limit - result_count
results = self.call(service, method,
offset=offset, limit=chunk, *args, **kwargs)

# It looks like we ran out results
if not results:
break

# Apparently this method doesn't return a list.
# Why are you even iterating over this?
if not isinstance(results, list):
yield results
break

for item in results:
yield item
result_count += 1

offset += chunk

if len(results) < chunk:
break

def __format_object_mask(self, objectmask, service):
""" Format new and old style object masks into proper headers.

Expand Down
68 changes: 67 additions & 1 deletion SoftLayer/tests/API/client_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
except ImportError:
import unittest # NOQA

from mock import patch, MagicMock
from mock import patch, MagicMock, call

import SoftLayer
import SoftLayer.API
Expand Down Expand Up @@ -262,3 +262,69 @@ def test_mask_call_invalid_mask(self, make_api_call):
self.assertIn('Malformed Mask', str(e))
else:
self.fail('No exception raised')

@patch('SoftLayer.API.Client.call')
def test_iterate(self, _call):
# chunk=100, no limit
_call.side_effect = [range(100), range(100, 125)]
result = list(self.client['SERVICE'].METHOD(iter=True))

self.assertEquals(range(125), result)
_call.assert_has_calls([
call('SoftLayer_SERVICE', 'METHOD',
limit=100, iter=True, offset=0),
call('SoftLayer_SERVICE', 'METHOD',
limit=100, iter=True, offset=100),
])
_call.reset_mock()

# chunk=100, no limit. Requires one extra request.
_call.side_effect = [range(100), range(100, 200), []]
result = list(self.client['SERVICE'].METHOD(iter=True))
self.assertEquals(range(200), result)
_call.assert_has_calls([
call('SoftLayer_SERVICE', 'METHOD',
limit=100, iter=True, offset=0),
call('SoftLayer_SERVICE', 'METHOD',
limit=100, iter=True, offset=100),
call('SoftLayer_SERVICE', 'METHOD',
limit=100, iter=True, offset=200),
])
_call.reset_mock()

# chunk=25, limit=30
_call.side_effect = [range(0, 25), range(25, 30)]
result = list(
self.client['SERVICE'].METHOD(iter=True, limit=30, chunk=25))
self.assertEquals(range(30), result)
_call.assert_has_calls([
call('SoftLayer_SERVICE', 'METHOD', iter=True, limit=25, offset=0),
call('SoftLayer_SERVICE', 'METHOD', iter=True, limit=5, offset=25),
])
_call.reset_mock()

# A non-list was returned
_call.side_effect = ["test"]
result = list(self.client['SERVICE'].METHOD(iter=True))
self.assertEquals(["test"], result)
_call.assert_has_calls([
call('SoftLayer_SERVICE', 'METHOD',
iter=True, limit=100, offset=0),
])
_call.reset_mock()

# chunk=25, limit=30, offset=12
_call.side_effect = [range(0, 25), range(25, 30)]
result = list(self.client['SERVICE'].METHOD(
iter=True, limit=30, chunk=25, offset=12))
self.assertEquals(range(30), result)
_call.assert_has_calls([
call('SoftLayer_SERVICE', 'METHOD',
iter=True, limit=25, offset=12),
call('SoftLayer_SERVICE', 'METHOD', iter=True, limit=5, offset=37),
])

# Chunk size of 0 is invalid
self.assertRaises(
AttributeError,
lambda: list(self.client['SERVICE'].METHOD(iter=True, chunk=0)))