|
| 1 | +from twilio.rest.resources import NextGenInstanceResource, NextGenListResource, transform_params |
| 2 | + |
| 3 | + |
| 4 | +class PhoneNumber(NextGenInstanceResource): |
| 5 | + """ |
| 6 | + Represents information about a phone number. |
| 7 | +
|
| 8 | + .. attribute:: phone_number |
| 9 | +
|
| 10 | + The phone number in normalized E.164 format, e.g. "+14158675309" |
| 11 | +
|
| 12 | + .. attribute:: national_format |
| 13 | +
|
| 14 | + The phone number in localized format, e.g. "(415) 867-5309" |
| 15 | +
|
| 16 | + .. attribute:: country_code |
| 17 | +
|
| 18 | + The ISO 3166-1 two-letter code for this phone number's country, e.g. |
| 19 | + "US" for United States |
| 20 | +
|
| 21 | + .. attribute:: carrier |
| 22 | +
|
| 23 | + A dictionary of information about the carrier responsible for this |
| 24 | + number, if requested. |
| 25 | +
|
| 26 | + Contains the following: |
| 27 | + - mobile_country_code: the country code of the mobile carrier. |
| 28 | + Only populated if the number is a mobile number. |
| 29 | + - mobile_network_code: the network code of the mobile carrier. |
| 30 | + Only populated if the number is a mobile number. |
| 31 | + - name: the name of the carrier. |
| 32 | + - type: the type of the number ("mobile", "landline", or "voip") |
| 33 | + - error_code: the error code of the carrier info request, if one |
| 34 | + occurred |
| 35 | + """ |
| 36 | + id_key = "phone_number" |
| 37 | + |
| 38 | + |
| 39 | +class PhoneNumbers(NextGenListResource): |
| 40 | + name = "PhoneNumbers" |
| 41 | + instance = PhoneNumber |
| 42 | + |
| 43 | + def get(self, number, include_carrier_info=False, country_code=None): |
| 44 | + """Look up a phone number. |
| 45 | +
|
| 46 | + :param str number: The phone number to query. |
| 47 | + :param bool include_carrier_info: Whether to do a carrier lookup on |
| 48 | + the phone number. See twilio.com for the latest pricing. |
| 49 | + :param str country_code: If the number is provided in a local format |
| 50 | + rather than E.164, specify the two-letter code of the country to parse |
| 51 | + the number for. |
| 52 | + """ |
| 53 | + |
| 54 | + params = {} |
| 55 | + if country_code is not None: |
| 56 | + params['country_code'] = country_code |
| 57 | + |
| 58 | + if include_carrier_info: |
| 59 | + params['type'] = 'carrier' |
| 60 | + |
| 61 | + params = transform_params(params) |
| 62 | + uri = "%s/%s" % (self.uri, number) |
| 63 | + _, item = self.request("GET", uri, params=params) |
| 64 | + |
| 65 | + return self.load_instance(item) |
0 commit comments