8000 Add Lookups client and PhoneNumbers resource · saroshfarhan/twilio-python@285c5fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 285c5fd

Browse files
committed
Add Lookups client and PhoneNumbers resource
1 parent 66ba418 commit 285c5fd

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

twilio/rest/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .base import set_twilio_proxy
22
from .client import TwilioRestClient
3+
from .lookups import TwilioLookupsClient
34
from .task_router import TwilioTaskRouterClient

twilio/rest/lookups.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from twilio.rest.base import TwilioClient
2+
from twilio.rest.resources import UNSET_TIMEOUT
3+
from twilio.rest.resources.lookups.phone_numbers import PhoneNumbers
4+
5+
6+
class TwilioLookupsClient(TwilioClient):
7+
"""
8+
A client for accessing the Twilio Lookups API.
9+
10+
The Twilio Lookups API provides information about phone numbers,
11+
including non-Twilio numbers. For more information, see the
12+
`Lookups API documentation <https://www.twilio.com/docs/XXX>`_.
13+
14+
:param str account: Your Account Sid from `your dashboard
15+
<https://www.twilio.com/user/account>`_
16+
:param str token: Your Auth Token from `your dashboard
17+
<https://www.twilio.com/user/account>`_
18+
:param float timeout: The socket and read timeout for requests to Twilio
19+
"""
20+
21+
def __init__(self, account=None, token=None,
22+
base="https://lookups.twilio.com", version="v1",
23+
timeout=UNSET_TIMEOUT):
24+
25+
super(TwilioLookupsClient, self).__init__(account, token, base,
26+
version, timeout)
27+
28+
self.version_uri = "%s/%s" % (base, version)
29+
self.phone_numbers = PhoneNumbers(self.version_uri, self.auth, timeout)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)
0