3

I'm querying available phone numbers like so:

from twilio.rest import Client

client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
available = client.available_phone_numbers("US").local.list()

I would like to loop through all of the available phone numbers as dictionary objects but as far as I can tell Twilio's Python SDK doesn't have anything like that.

I would have to do the following:

results = [{
    "address_requirements": phone.address_requirements,
    "beta": phone.beta,
    "capabilities": phone.capabilities,
    "MMS": phone.MMS,
    "SMS": phone.SMS,
    "fax": phone.fax,
    "voice": phone.voice,
    "friendly_name": phone.friendly_name,
    "iso_country": phone.iso_country,
    "lata": phone.lata,
    "locality": phone.locality,
    "longitude": phone.longitude,
    "phone_number": phone.phone_number,
    "postal_code": phone.postal_code,
    "rate_center": phone.rate_center,
    "region": phone.region
} for phone in available]

This seems silly to have to do since the API returns the exact format I want. I'm tempted to ditch the SDK and make the calls directly.

chrislondon
  • 12,487
  • 5
  • 26
  • 65

1 Answers1

2

After much search and digging into the source code I was able to find a simple way to access the data I want. It uses a private attribute which you shouldn't use but if they don't provide what you need you have to do what you have to do.

I can achieve the result I want with this though I still do have to run an extra unnecessary loop:

from twilio.rest import Client

client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
available = client.available_phone_numbers("US").local.list()
results = [phone._properties for phone in available]
chrislondon
  • 12,487
  • 5
  • 26
  • 65