10000 Add Pricing client and resources · tuhina2020/twilio-python@602bd2c · GitHub
[go: up one dir, main page]

Skip to content

Commit 602bd2c

Browse files
committed
Add Pricing client and resources
1 parent 658f366 commit 602bd2c

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed

twilio/rest/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
from .base import set_twilio_proxy
22
from .client import TwilioRestClient
33
from .lookups import TwilioLookupsClient
4+
from .pricing import TwilioPricingClient
45
from .task_router import TwilioTaskRouterClient
6+
7+
_hush_pyflakes = [set_twilio_proxy, TwilioRestClient, TwilioLookupsClient,
8+
TwilioPricingClient, TwilioTaskRouterClient]

twilio/rest/pricing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from twilio.rest.base import TwilioClient
2+
from twilio.rest.resources import UNSET_TIMEOUT
3+
from twilio.rest.resources.pricing import (
4+
PhoneNumbers,
5+
Voice,
6+
)
7+
8+
9+
class TwilioPricingClient(TwilioClient):
10+
"""
11+
A client for accessing the Twilio Pricing API.
12+
13+
:param str account: Your Account SID from `your dashboard
14+
<https://twilio.com/user/account>`_
15+
:param str token: Your Auth Token from `your dashboard
16+
<https://twilio.com/user_account>`_
17+
:param float timeout: The socket connect and read timeout for requests
18+
to Twilio
19+
"""
20+
21+
def __init__(self, account=None, token=None,
22+
base="https://pricing.twilio.com", version="v1",
23+
timeout=UNSET_TIMEOUT):
24+
super(TwilioPricingClient, self).__init__(account, token, base,
25+
version, timeout)
26+
27+
uri_base = "{}/{}".format(base, version)
28+
29+
self.voice = Voice(uri_base, self.auth, self.timeout)
30+
self.phone_numbers = PhoneNumbers(uri_base, self.auth, self.timeout)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from .voice import (
2+
Voice,
3+
VoiceCountry,
4+
VoiceCountries,
5+
VoiceNumber,
6+
VoiceNumbers,
7+
)
8+
9+
from .phone_numbers import (
10+
PhoneNumberCountries,
11+
PhoneNumberCountry,
12+
PhoneNumbers,
13+
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from .. import InstanceResource, ListResource
2+
3+
4+
class PhoneNumbers(object):
5+
"""Holds references to the Number pricing resources."""
6+
7+
name = "Number"
8+
key = "Number"
9+
10+
def __init__(self, base_uri, auth, timeout):
11+
self.uri = "%s/PhoneNumbers" % base_uri
12+
self.countries = PhoneNumberCountries(self.uri, auth, timeout)
13+
14+
15+
class PhoneNumberCountry(InstanceResource):
16+
"""Pricing information for Twilio Phone Numbers in a specific country.
17+
18+
.. attribute:: country
19+
20+
The full name of the country.
21+
22+
.. attribute:: iso_country
23+
24+
The country's 2-character ISO code.
25+
26+
.. attribute:: price_unit
27+
28+
The currency in which prices are measured, in ISO 4127 format
29+
(e.g. 'usd', 'eur', 'jpy').
30+
31+
.. attribute:: phone_number_prices
32+
33+
A list of dicts containing pricing information as follows:
34+
- type: "local", "mobile", "national", or "toll_free"
35+
- base_price: the base price per month for this Twilio number type
36+
- current_price: the current price per month (including discounts)
37+
for this Twilio number type
38+
"""
39+
40+
id_key = "iso_country"
41+
42+
43+
class PhoneNumberCountries(ListResource):
44+
45+
instance = PhoneNumberCountry
46+
key = "countries"
47+
name = "Countries"
48+
49+
def get(self, iso_country):
50+
"""Retrieve pricing information for Twilio Number in the specified
51+
country.
52+
53+
:param iso_country: The two-letter ISO code for the country
54+
"""
55+
return self.get_instance(iso_country)
56+
57+
def list(self):
58+
"""Retrieve the list of countries in which Twilio Number is available."""
59+
60+
resp, page = self.request("GET", self.uri)
61+
62+
return [self.load_instance(i) for i in page[self.key]]
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
from .. import InstanceResource, ListResource
2+
3+
4+
class Voice(object):
5+
"""Holds references to the Voice pricing resources."""
6+
7+
name = "Voice"
8+
key = "voice"
9+
10+
def __init__(self, base_uri, auth, timeout):
11+
self.uri = "%s/Voice" % base_uri
12+
self.countries = VoiceCountries(self.uri, auth, timeout)
13+
self.numbers = VoiceNumbers(self.uri, auth, timeout)
14+
15+
16+
class VoiceCountry(InstanceResource):
17+
"""Pricing information for Twilio Voice services in a specific country.
18+
19+
.. attribute:: country
20+
21+
The full name of the country.
22+
23+
.. attribute:: iso_country
24+
25+
The country's 2-character ISO code.
26+
27+
.. attribute:: price_unit
28+
29+
The currency in which prices are measured, in ISO 4127 format
30+
(e.g. 'usd', 'eur', 'jpy').
31+
32+
.. attribute:: outbound_prefix_prices
33+
34+
A list of dicts containing pricing information as follows:
35+
- prefix_list: a list of number prefixes in the requested country
36+
that have the same pricing
37+
- friendly_name: a descriptive name for this prefix set
38+
- call_base_price: the base price per minute for calls to numbers
39+
matching any of these prefixes
40+
- call_current_price: the current price per minute (including
41+
volume discounts, etc.) for your account to make calls to
42+
numbers matching these prefixes
43+
44+
.. attribute:: inbound_call_prices
45+
46+
A list of dicts containing pricing information for inbound calls:
47+
- number_type: 'local', 'mobile', 'national', or 'toll_free'
48+
- call_base_price: the base price per minute to receive a call
49+
to this number type
50+
- call_current_price: the current price per minute (including
51+
volume discounts, etc.) for your account to receive a call
52+
to this number type
53+
"""
54+
55+
id_key = "iso_country"
56+
57+
58+
class VoiceCountries(ListResource):
59+
60+
instance = VoiceCountry
61+
key = "countries"
62+
name = "Countries"
63+
64+
def get(self, iso_country):
65+
"""Retrieve pricing information for Twilio Voice in the specified
66+
country.
67+
68+
:param iso_country: The two-letter ISO code for the country
69+
"""
70+
return self.get_instance(iso_country)
71+
72+
def list(self):
73+
"""Retrieve the list of countries in which Twilio Voice is available."""
74+
75+
resp, page = self.request("GET", self.uri)
76+
77+
return [self.load_instance(i) for i in page[self.key]]
78+
79+
80+
class VoiceNumber(InstanceResource):
81+
"""Pricing information for Twilio Voice services to and from a given
82+
phone number.
83+
84+
.. attribute:: phone_number
85+
86+
The E.164-formatted phone number this pricing information applies to
87+
88+
.. attribute:: country
89+
90+
The name of the country this phone number belongs to
91+
92+
.. attribute:: iso_country
93+
94+
The two-character ISO code for the country
95+
96+
.. attribute:: outbound_call_price
97+
98+
A dict containing pricing information for outbound calls to this number:
99+
- base_price: the base price per minute for a call to this number
100+
- current_price: the current price per minute (including discounts,
101+
etc.) for a call to this number
102+
103+
.. attribute:: inbound_call_price
104+
105+
A dict containing pricing information for inbound call to this number,
106+
or null if this number is not Twilio-hosted.
107+
108+
- number_type: "local", "mobile", "national", or "toll_free"
109+
- call_base_price: the base price per minute to receive a call to
110+
this number
111+
- call_current_price: the current price per minute (including
112+
discounts, etc.) to receive a call to this number
113+
"""
114+
115+
id_key = "phone_number"
116+
117+
118+
class VoiceNumbers(ListResource):
119+
120+
instance = VoiceNumber
121+
key = "numbers"
122+
name = "Numbers"
123+
124+
def get(self, phone_number):
125+
""" Retrieve pricing information for a specific phone number.
126+
:param phone_number: the E.164-formatted number to retrieve info for
127+
:return: a :class:`VoiceNumber` instance
128+
"""
129+
130+
return self.get_instance(phone_number)

0 commit comments

Comments
 (0)
0