Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you use the Twilio python helper to page through a message list?

I'm using https://github.com/twilio/twilio-python and have read through the documentation including https://www.twilio.com/docs/api/rest/response#response-formats-list-paging-information.

I can't seem to find the method to retrieve the next page from a large list. I'm using the following code to retrieve the initial list but don't know how to retrieve the nextpageuri and then retrieve the next page.

client = TwilioRestClient(twilioAccount, twilioToken)

messages = client.messages.list(
    to="+15162047575",
    # to="+15167217331",
    after=date(2014,5,7),
    PageSize=50)

I'm using Twilio 3.6.4 and 3.5.1 with the latest version of the python helper.

like image 204
mjbeller Avatar asked Nov 23 '25 10:11

mjbeller


1 Answers

I finally found the answer (and I assume it's ok for me to self answer ...)

The Twilio helper library (at least the python helper) does not expose the nextpageuri directly (which explains why I couldn't find a method or property to access nextpageuri).

Although the paging information on the Twilio site describes methods for paging using nextpageuri, the helper library utilizes an iter() method.

So rather than use the code I posted in my question and then loop through 'messages', you can use:

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
for message in client.messages.iter():
    # code to process message

From the python helper documentation (not the Twilio Rest API documentation):

Sometimes you’d like to retrieve all records from a list resource. Instead of manually paging over the resource, the resources.ListResource.iter method returns a generator. After exhausting the current page, the generator will request the next page of results.

Here are some references:

  • https://twilio-python.readthedocs.org/en/latest/usage/basics.html
    (scroll to 'Listing All Resources')
  • https://twilio-python.readthedocs.org/en/latest/api/rest/resources.html#twilio.rest.resources.ListResource.iter
like image 115
mjbeller Avatar answered Nov 25 '25 00:11

mjbeller