8000 Split up Twilio REST client objects · zhangjingcat/twilio-python@62c649f · GitHub
[go: up one dir, main page]

Skip to content

Commit 62c649f

Browse files
committed
Split up Twilio REST client objects
1 parent 08b57cc commit 62c649f

File tree

7 files changed

+336
-326
lines changed

7 files changed

+336
-326
lines changed

tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def setUp(self):
1515
self.client = TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN")
1616
self.task_router_client = TwilioTaskRouterClient("ACCOUNT_SID", "AUTH_TOKEN")
1717

18-
@patch("twilio.rest.make_request")
18+
@patch("twilio.rest.base.make_request")
1919
def test_request(self, mock):
2020
self.client.request("2010-04-01", method="GET")
2121
mock.assert_called_with("GET", "https://api.twilio.com/2010-04-01",

tests/test_credentials.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from nose.tools import assert_equal, assert_raises
22
from mock import patch
33

4-
from twilio.rest.exceptions import TwilioException
5-
from twilio.rest import TwilioRestClient, find_credentials
4+
from twilio.exceptions import TwilioException
5+
from twilio.rest import TwilioRestClient
6+
from twilio.rest.base import find_credentials
67

78

89
def test_creds_not_found():
@@ -20,7 +21,7 @@ def test_find_creds():
2021
assert_equal(find_credentials(env), ('AC123', '456'))
2122

2223

23-
@patch("twilio.rest.find_credentials")
24+
@patch("twilio.rest.base.find_credentials")
2425
def test_creds_error(creds):
2526
creds.return_value = (None, None)
2627
assert_raises(TwilioException, TwilioRestClient)

twilio/rest/__init__.py

Lines changed: 3 additions & 321 deletions
Original file line numberDiff line numberDiff line change
@@ -1,321 +1,3 @@
1-
import logging
2-
import os
3-
import platform
4-
5-
from ..exceptions import TwilioException
6-
from .. import __version__ as LIBRARY_VERSION
7-
from .resources import (
8-
make_request,
9-
Accounts,
10-
Activities,
11-
Events,
12-
Applications,
13-
AuthorizedConnectApps,
14-
CallerIds,
15-
Calls,
16-
Conferences,
17-
ConnectApps,
18-
Connection,
19-
DependentPhoneNumbers,
20-
MediaList,
21-
Members,
22-
Messages,
23-
Notifications,
24-
Participants,
25-
PhoneNumbers,
26-
Queues,
27-
Recordings,
28-
Sandboxes,
29-
Sip,
30-
Sms,
31-
Tokens,
32-
Transcriptions,
33-
UNSET_TIMEOUT,
34-
Usage,
35-
CallFeedbackFactory,
36-
CallFeedback,
37-
Reservations,
38-
TaskQueues,
39-
Tasks,
40-
Workers,
41-
Workflows,
42-
Workspaces,
43-
)
44-
45-
46-
def find_credentials(environ=None):
47-
"""
48-
Look in the current environment for Twilio credentials
49-
50-
:param environ: the environment to check
51-
"""
52-
environment = environ or os.environ
53-
try:
54-
account = environment["TWILIO_ACCOUNT_SID"]
55-
token = environment["TWILIO_AUTH_TOKEN"]
56-
return account, token
57-
except KeyError:
58-
return None, None
59-
60-
61-
def set_twilio_proxy(proxy_url, proxy_port):
62-
Connection.set_proxy_info(proxy_url, proxy_port)
63-
64-
65-
class TwilioClient(object):
66-
def __init__(self, account=None, token=None, base="https://api.twilio.com",
67-
version="2010-04-01",
68-
timeout=UNSET_TIMEOUT):
69-
"""
70-
Create a Twilio API client.
71-
"""
72-
73-
# Get account credentials
74-
if not account or not token:
75-
account, token = find_credentials()
76-
if not account or not token:
77-
raise TwilioException("""
78-
Twilio could not find your account credentials. Pass them into the
79-
TwilioRestClient constructor like this:
80-
81-
client = TwilioRestClient(account='AC38135355602040856210245275870',
82-
token='2flnf5tdp7so0lmfdu3d')
83-
84-
Or, add your credentials to your shell environment. From the terminal, run
85-
86-
echo "export TWILIO_ACCOUNT_SID=AC3813535560204085626521" >> ~/.bashrc
87-
echo "export TWILIO_AUTH_TOKEN=2flnf5tdp7so0lmfdu3d7wod" >> ~/.bashrc
88-
89-
and be sure to replace the values for the Account SID and auth token with the
90-
values from your Twilio Account at https://www.twilio.com/user/account.
91-
""")
92-
self.base = base
93-
self.auth = (account, token)
94-
self.timeout = timeout
95-
self.account_uri = "{0}/{1}/Accounts/{2}".format(base,
96-
version, account)
97-
98-
def request(self, path, method=None, vars=None):
99-
"""sends a request and gets a response from the Twilio REST API
100-
101-
.. deprecated:: 3.0
102-
103-
:param path: the URL (relative to the endpoint URL, after the /v1
104-
:param url: the HTTP method to use, defaults to POST
105-
:param vars: for POST or PUT, a dict of data to send
106-
107-
:returns: Twilio response in XML or raises an exception on error
108-
:raises: a :exc:`ValueError` if the path is invalid
109-
:raises: a :exc:`NotImplementedError` if the method is unknown
110-
111-
This method is only included for backwards compatability reasons.
112-
It will be removed in a future version
113-
"""
114-
logging.warning(":meth:`TwilioRestClient.request` is deprecated and "
115-
"will be removed in a future version")
116-
117-
vars = vars or {}
118-
params = None
119-
data = None
120-
121-
if not path or len(path) < 1:
122-
raise ValueError('Invalid path parameter')
123-
if method and method not in ['GET', 'POST', 'DELETE', 'PUT']:
124-
raise NotImplementedError(
125-
'HTTP %s method not implemented' % method)
126-
127-
if path[0] == '/':
128-
uri = self.base + path
129-
else:
130-
uri = self.base + '/' + path
131-
132-
if method == "GET":
133-
params = vars
134-
elif method == "POST" or method == "PUT":
135-
data = vars
136-
137-
user_agent = "twilio-python %s (python-%s)" % (
138-
LIBRARY_VERSION,
139-
platform.python_version(),
140-
)
141-
142-
headers = {
143-
"User-Agent": user_agent,
144-
"Accept-Charset": "utf-8",
145-
}
146-
147-
resp = make_request(method, uri, auth=self.auth, data=data,
148-
params=params, headers=headers)
149-
150-
return resp.content
151-
152-
153-
class TwilioRestClient(TwilioClient):
154-
"""
155-
A client for accessing the Twilio REST API
156-
157-
:param str account: Your Account SID from `your dashboard
158-
<https://twilio.com/user/account>`_
159-
:param str token: Your Auth Token from `your dashboard
160-
<https://twilio.com/user/account>`_
161-
:param float timeout: The socket and read timeout for requests to Twilio
162-
"""
163-
164-
def __init__(self, account=None, token=None, base="https://api.twilio.com",
165-
version="2010-04-01", timeout=UNSET_TIMEOUT):
166-
"""
167-
Create a Twilio REST API client.
168-
"""
169-
super(TwilioRestClient, self).__init__(account, token, base, version,
170-
timeout)
171-
172-
version_uri = "%s/%s" % (base, version)
173-
174-
self.accounts = Accounts(version_uri, self.auth, timeout)
175-
self.applications = Applications(self.account_uri, self.auth, timeout)
176-
self.authorized_connect_apps = AuthorizedConnectApps(
177-
self.account_uri,
178-
self.auth,
179-
timeout
180-
)
181-
self.calls = Calls(self.account_uri, self.auth, timeout)
182-
self.caller_ids = CallerIds(self.account_uri, self.auth, timeout)
183-
self.connect_apps = ConnectApps(self.account_uri, self.auth, timeout)
184-
self.notifications = Notifications(self.account_uri, self.auth,
185-
timeout)
186-
self.recordings = Recordings(self.account_uri, self.auth, timeout)
187-
self.transcriptions = Transcriptions(self.account_uri, self.auth,
188-
timeout)
189-
self.sms = Sms(self.account_uri, self.auth, timeout)
190-
self.phone_numbers = PhoneNumbers(self.account_uri, self.auth, timeout)
191-
self.conferences = Conferences(self.account_uri, self.auth, timeout)
192-
self.queues = Queues(self.account_uri, self.auth, timeout)
193-
self.sandboxes = Sandboxes(self.account_uri, self.auth, timeout)
194-
self.usage = Usage(self.account_uri, self.auth, timeout)
195-
self.messages = Messages(self.account_uri, self.auth, timeout)
196-
self.media = MediaList(self.account_uri, self.auth, timeout)
197-
self.sip = Sip(self.account_uri, self.auth, timeout)
198-
self.tokens = Tokens(self.account_uri, self.auth, timeout)
199-
200-
def participants(self, conference_sid):
201-
"""
202-
Return a :class:`~twilio.rest.resources.Participants` instance for the
203-
:class:`~twilio.rest.resources.Conference` with given conference_sid
204-
"""
205-
base_uri = "%s/Conferences/%s" % (self.account_uri, conference_sid)
206-
return Participants(base_uri, self.auth, self.timeout)
207-
208-
def members(self, queue_sid):
209-
"""
210-
Return a :class:`Members <twilio.rest.resources.Members>` instance for
211-
the :class:`Queue <twilio.rest.resources.Queue>` with the
212-
given queue_sid
213-
"""
214-
base_uri = "%s/Queues/%s" % (self.account_uri, queue_sid)
215-
return Members(base_uri, self.auth, self.timeout)
216-
217-
def feedback(self, call_sid):
218-
"""
219-
Return a :class:`CallFeedback <twilio.rest.resources.CallFeedback>`
220-
instance for the :class:`Call <twilio.rest.resources.calls.Call>`
221-
with the given call_sid
222-
"""
223-
base_uri = "%s/Calls/%s/Feedback" % (self.account_uri, call_sid)
224-
call_feedback_list = CallFeedbackFactory(
225-
base_uri,
226-
self.auth,
227-
self.timeout
228-
)
229-
return CallFeedback(call_feedback_list)
230-
231-
def dependent_phone_numbers(self, address_sid):
232-
"""
233-
Return a :class:`DependentPhoneNumbers
234-
<twilio.rest.resources.DependentPhoneNumbers>` instance for the
235-
:class:`Address <twilio.rest.resources.Address>` with the given
236-
address_sid
237-
"""
238-
base_uri = "%s/Addresses/%s" % (self.account_uri, address_sid)
239-
return DependentPhoneNumbers(base_uri, self.auth, self.timeout)
240-
241-
242-
class TwilioTaskRouterClient(TwilioClient):
243-
"""
244-
A client for accessing the Twilio TaskRouter API
245-
246-
:param str account: Your Account SID from `your dashboard
247-
<https://twilio.com/user/account>`_
248-
:param str token: Your Auth Token from `your dashboard
249-
<https://twilio.com/user/account>`_
250-
:param float timeout: The socket and read timeout for requests to Twilio
251-
"""
252-
253-
def __init__(self, account=None, token=None,
254-
base="https://taskrouter.twilio.com", version="v1",
255-
timeout=UNSET_TIMEOUT):
256-
"""
257-
Create a Twilio REST API client.
258-
"""
259-
super(TwilioTaskRouterClient, self).__init__(account, token, base,
260-
version, timeout)
261-
self.base_uri = "{0}/{1}".format(base, version)
262-
self.workspace_uri = "{0}/Workspaces".format(self.base_uri)
263-
264-
self.workspaces = Workspaces(self.base_uri, self.auth, timeout)
265-
266-
def activities(self, workspace_sid):
267-
"""
268-
Return a :class:`Activities` instance for the :class:`Activity`
269-
with the given workspace_sid
270-
"""
271-
base_uri = "{0}/{1}".format(self.workspace_uri, workspace_sid)
272-
return Activities(base_uri, self.auth, self.timeout)
273-
274-
def events(self, workspace_sid):
275-
"""
276-
Return a :class:`Events` instance for the :class:`Event` with the given
277-
workspace_sid
278-
"""
279-
base_uri = "{0}/{1}".format(self.workspace_uri, workspace_sid)
280-
return Events(base_uri, self.auth, self.timeout)
281-
282-
def reservations(self, workspace_sid, task_sid):
283-
"""
284-
Return a :class:`Reservations` instance for the :class:`Reservation`
285-
with the given workspace_sid ans task_sid
286-
"""
287-
base_uri = "{0}/{1}/Tasks/{2}".format(self.workspace_uri,
288-
workspace_sid, task_sid)
289-
return Reservations(base_uri, self.auth, self.timeout)
290-
291-
def task_queues(self, workspace_sid):
292-
"""
293-
Return a :class:`TaskQueues` instance for the :class:`TaskQueue` with
294-
the given workspace_sid
295-
"""
296-
base_uri = "{0}/{1}".format(self.workspace_uri, workspace_sid)
297-
return TaskQueues(base_uri, self.auth, self.timeout)
298-
299-
def tasks(self, workspace_sid):
300-
"""
301-
Return a :class:`Tasks` instance for the :class:`Task` with the given
302-
workspace_sid
303-
"""
304-
base_uri = "{0}/{1}".format(self.workspace_uri, workspace_sid)
305-
return Tasks(base_uri, self.auth, self.timeout)
306-
307-
def workers(self, workspace_sid):
308-
"""
309-
Return a :class:`Workers` instance for the :class:`Worker` with the
310-
given workspace_sid
311-
"""
312-
base_uri = "{0}/{1}".format(self.workspace_uri, workspace_sid)
313-
return Workers(base_uri, self.auth, self.timeout)
314-
315-
def workflows(self, workspace_sid):
316-
"""
317-
Return a :class:`Workflows` instance for the :class:`Workflow` with the
318-
given workspace_sid
319-
"""
320-
base_uri = "{0}/{1}".format(self.workspace_uri, workspace_sid)
321-
return Workflows(base_uri, self.auth, self.timeout)
1+
from .base import set_twilio_proxy
2+
from .client import TwilioRestClient
3+
from .task_router import TwilioTaskRouterClient

0 commit comments

Comments
 (0)
0