Permalink
Browse files

Use next_page_uri for iterating over a list resource

  • Loading branch information...
1 parent 82d2e15 commit 6ef25c8fbc958a21c6f3033a7357cf6bbab5d164 @kyleconroy kyleconroy committed Jun 19, 2012
Showing with 66 additions and 15 deletions.
  1. +6 −0 CHANGES
  2. +3 −1 requirements.txt
  3. +38 −1 tests/test_base_resource.py
  4. +1 −1 twilio/__init__.py
  5. +18 −12 twilio/rest/resources.py
View
@@ -3,6 +3,12 @@ twilio-python Changelog
Here you can see the full list of changes between each twilio-python release.
+Version 3.3.8
+-----------
+
+- Use next_page_uri when iterating over a list resource
+
+
Version 3.3.7
-----------
View
@@ -1,7 +1,9 @@
sphinx
-mock
+mock==0.8.0
httplib2
nose
pyjwt
simplejson
unittest2
+coverage
+nosexcover
@@ -6,7 +6,7 @@
import unittest2 as unittest
else:
import unittest
-from mock import Mock
+from mock import Mock, patch
from nose.tools import assert_equals
from nose.tools import raises
from twilio.rest.resources import Resource
@@ -45,9 +45,46 @@ def testListResourceInit(self):
def testKeyValue(self):
self.assertEquals(self.r.key, self.r.name.lower())
+ def testIterNoKey(self):
+ self.r.request = Mock()
+ self.r.request.return_value = Mock(), {}
+
+ with self.assertRaises(StopIteration):
+ self.r.iter().next()
+
+ def testRequest(self):
+ self.r.request = Mock()
+ self.r.request.return_value = Mock(), {self.r.key: [{'sid': 'foo'}]}
+ self.r.iter().next()
+ self.r.request.assert_called_with("GET", "https://api.twilio.com/2010-04-01/Resources", params={})
+
+ def testIterOneItem(self):
+ self.r.request = Mock()
+ self.r.request.return_value = Mock(), {self.r.key: [{'sid': 'foo'}]}
+
+ items = self.r.iter()
+ items.next()
+
+ with self.assertRaises(StopIteration):
+ items.next()
+
+ def testIterNoNextPage(self):
+ self.r.request = Mock()
+ self.r.request.return_value = Mock(), {self.r.key: []}
+
+ with self.assertRaises(StopIteration):
+ self.r.iter().next()
+
def testKeyValue(self):
self.r.key = "Hey"
self.assertEquals(self.r.key, "Hey")
+
+ def testInstanceLoading(self):
+ instance = self.r.load_instance({"sid": "foo"})
+
+ self.assertIsInstance(instance, InstanceResource)
+ self.assertEquals(instance.sid, "foo")
+
def testInstanceLoading(self):
instance = self.r.load_instance({"sid": "foo"})
View
@@ -1,4 +1,4 @@
-__version_info__ = ('3', '3', '7')
+__version_info__ = ('3', '3', '8')
__version__ = '.'.join(__version_info__)
View
@@ -5,7 +5,7 @@
from twilio import TwilioException
from twilio import TwilioRestException
from urllib import urlencode
-from urlparse import urlparse
+from urlparse import urlparse, parse_qs
# import json
try:
@@ -356,18 +356,24 @@ def count(self):
def iter(self, **kwargs):
"""
Return all instance resources using an iterator
- Can only be called on classes which implement list()
-
- TODO Make this use the next_url instead
"""
- p = 0
- try:
- while True:
- for r in self.list(page=p, **kwargs):
- yield r
- p += 1
- except TwilioRestException:
- pass
+ params = transform_params(kwargs)
+
+ while True:
+ resp, page = self.request("GET", self.uri, params=params)
+
+ if self.key not in page:
+ raise StopIteration()
+
+ for ir in page[self.key]:
+ yield self.load_instance(ir)
+
+ if not page.get('next_page_uri', ''):
+ raise StopIteration()
+
+ o = urlparse(page['next_page_uri'])
+ params.update(parse_qs(o.query))
+
def load_instance(self, data):
instance = self.instance(self, data[self.instance.id_key])

0 comments on commit 6ef25c8

Please sign in to comment.