Skip to content

Commit 3e246e4

Browse files
committed
Python Orgs Api Changes
1 parent b4c5734 commit 3e246e4

File tree

17 files changed

+1488
-197
lines changed

17 files changed

+1488
-197
lines changed

tests/cluster/test_cluster.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ def setUp(self):
1919
)
2020
self.voice_twiml = VoiceResponse()
2121

22+
23+
def test_token_fetch(self):
24+
token = self.client.preview_iam.token.create(
25+
grant_type = GRANT_TYPE,
26+
client_id = CLIENT_ID,
27+
client_secret = CLIENT_SECRET)
28+
print(f'{token}')
29+
30+
2231
def test_send_text_message(self):
2332
msg = self.client.messages.create(
2433
to=self.to_number, from_=self.from_number, body="hello world"

tests/cluster/test_webhook.py

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,81 +29,81 @@ def process_request(self):
2929
)
3030

3131

32-
# class WebhookTest(unittest.TestCase):
33-
# def setUp(self):
34-
# api_key = os.environ["TWILIO_API_KEY"]
35-
# api_secret = os.environ["TWILIO_API_SECRET"]
36-
# account_sid = os.environ["TWILIO_ACCOUNT_SID"]
37-
# self.client = Client(api_key, api_secret, account_sid)
38-
#
39-
# portNumber = 7777
40-
# self.validation_server = HTTPServer(("", portNumber), RequestHandler)
41-
# self.tunnel = ngrok.connect(portNumber)
42-
# self.flow_sid = ""
43-
# _thread.start_new_thread(self.start_http_server, ())
44-
#
45-
# def start_http_server(self):
46-
# self.validation_server.serve_forever()
47-
#
48-
# def tearDown(self):
49-
# self.client.studio.v2.flows(self.flow_sid).delete()
50-
# ngrok.kill()
51-
# self.validation_server.shutdown()
52-
# self.validation_server.server_close()
53-
#
54-
# def create_studio_flow(self, url, method):
55-
# flow = self.client.studio.v2.flows.create(
56-
# friendly_name="Python Cluster Test Flow",
57-
# status="published",
58-
# definition={
59-
# "description": "Studio Flow",
60-
# "states": [
61-
# {
62-
# "name": "Trigger",
63-
# "type": "trigger",
64-
# "transitions": [
65-
# {
66-
# "next": "httpRequest",
67-
# "event": "incomingRequest",
68-
# },
69-
# ],
70-
# "properties": {},
71-
# },
72-
# {
73-
# "name": "httpRequest",
74-
# "type": "make-http-request",
75-
# "transitions": [],
76-
# "properties": {
77-
# "method": method,
78-
# "content_type": "application/x-www-form-urlencoded;charset=utf-8",
79-
# "url": url,
80-
# },
81-
# },
82-
# ],
83-
# "initial_state": "Trigger",
84-
# "flags": {
85-
# "allow_concurrent_calls": True,
86-
# },
87-
# },
88-
# )
89-
# return flow
90-
#
91-
# def validate(self, method):
92-
# flow = self.create_studio_flow(url=self.tunnel.public_url, method=method)
93-
# self.flow_sid = flow.sid
94-
# time.sleep(5)
95-
# self.client.studio.v2.flows(self.flow_sid).executions.create(
96-
# to="to", from_="from"
97-
# )
98-
#
99-
# def test_get(self):
100-
# time.sleep(5)
101-
# self.validate("GET")
102-
# time.sleep(5)
103-
# self.assertEqual(RequestHandler.is_request_valid, True)
104-
#
105-
# def test_post(self):
106-
# time.sleep(5)
107-
# self.validate("POST")
108-
# time.sleep(5)
109-
# self.assertEqual(RequestHandler.is_request_valid, True)
32+
class WebhookTest(unittest.TestCase):
33+
def setUp(self):
34+
api_key = os.environ["TWILIO_API_KEY"]
35+
api_secret = os.environ["TWILIO_API_SECRET"]
36+
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
37+
self.client = Client(api_key, api_secret, account_sid)
38+
39+
portNumber = 7777
40+
self.validation_server = HTTPServer(("", portNumber), RequestHandler)
41+
self.tunnel = ngrok.connect(portNumber)
42+
self.flow_sid = ""
43+
_thread.start_new_thread(self.start_http_server, ())
44+
45+
def start_http_server(self):
46+
self.validation_server.serve_forever()
47+
48+
def tearDown(self):
49+
self.client.studio.v2.flows(self.flow_sid).delete()
50+
ngrok.kill()
51+
self.validation_server.shutdown()
52+
self.validation_server.server_close()
53+
54+
def create_studio_flow(self, url, method):
55+
flow = self.client.studio.v2.flows.create(
56+
friendly_name="Python Cluster Test Flow",
57+
status="published",
58+
definition={
59+
"description": "Studio Flow",
60+
"states": [
61+
{
62+
"name": "Trigger",
63+
"type": "trigger",
64+
"transitions": [
65+
{
66+
"next": "httpRequest",
67+
"event": "incomingRequest",
68+
},
69+
],
70+
"properties": {},
71+
},
72+
{
73+
"name": "httpRequest",
74+
"type": "make-http-request",
75+
"transitions": [],
76+
"properties": {
77+
"method": method,
78+
"content_type": "application/x-www-form-urlencoded;charset=utf-8",
79+
"url": url,
80+
},
81+
},
82+
],
83+
"initial_state": "Trigger",
84+
"flags": {
85+
"allow_concurrent_calls": True,
86+
},
87+
},
88+
)
89+
return flow
90+
91+
def validate(self, method):
92+
flow = self.create_studio_flow(url=self.tunnel.public_url, method=method)
93+
self.flow_sid = flow.sid
94+
time.sleep(5)
95+
self.client.studio.v2.flows(self.flow_sid).executions.create(
96+
to="to", from_="from"
97+
)
98+
99+
def test_get(self):
100+
time.sleep(5)
101+
self.validate("GET")
102+
time.sleep(5)
103+
self.assertEqual(RequestHandler.is_request_valid, True)
104+
105+
def test_post(self):
106+
time.sleep(5)
107+
self.validate("POST")
108+
time.sleep(5)
109+
self.assertEqual(RequestHandler.is_request_valid, True)

twilio/base/client_base.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def request(
7070
timeout: Optional[float] = None,
7171
allow_redirects: bool = False,
7272
is_oauth: bool = False,
73-
domain: Optional[str] = None
73+
domain: Optional[str] = None,
7474
) -> Response:
7575
"""
7676
Makes a request to the Twilio API using the configured http client
@@ -87,15 +87,21 @@ def request(
8787
8888
:returns: Response from the Twilio API
8989
"""
90+
91+
print('*****')
9092
if not is_oauth:
9193
auth = self.get_auth(auth)
9294
headers = self.get_headers(method, headers)
9395
uri = self.get_hostname(uri)
9496
if is_oauth:
95-
OauthTokenBase = dynamic_import("twilio.base.oauth_token_base", "OauthTokenBase")
96-
token = OauthTokenBase().get_oauth_token(domain, "v1", self.username, self.password)
97-
headers['Authorization'] = f'Bearer {token}'
98-
headers.get('Authorization')
97+
OauthTokenBase = dynamic_import(
98+
"twilio.base.oauth_token_base", "OauthTokenBase"
99+
)
100+
token = OauthTokenBase().get_oauth_token(
101+
domain, "v1", self.username, self.password
102+
)
103+
headers["Authorization"] = f"Bearer {token}"
104+
headers.get("Authorization")
99105

100106
return self.http_client.request(
101107
method,
@@ -145,10 +151,14 @@ async def request_async(
145151
headers = self.get_headers(method, headers)
146152
uri = self.get_hostname(uri)
147153
if is_oauth:
148-
OauthTokenBase = dynamic_import("twilio.base.oauth_token_base", "OauthTokenBase")
149-
token = OauthTokenBase().get_oauth_token(domain, "v1", self.username, self.password)
150-
headers['Authorization'] = f'Bearer {token}'
151-
headers.get('Authorization')
154+
OauthTokenBase = dynamic_import(
155+
"twilio.base.oauth_token_base", "OauthTokenBase"
156+
)
157+
token = OauthTokenBase().get_oauth_token(
158+
domain, "v1", self.username, self.password
159+
)
160+
headers["Authorization"] = f"Bearer {token}"
161+
headers.get("Authorization")
152162

153163
return await self.http_client.request(
154164
method,
@@ -247,7 +257,9 @@ def __repr__(self) -> str:
247257
"""
248258
return "<Twilio {}>".format(self.account_sid)
249259

260+
250261
def dynamic_import(module_name, class_name):
251262
from importlib import import_module
263+
252264
module = import_module(module_name)
253265
return getattr(module, class_name)

twilio/base/domain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,5 @@ async def request_async(
9494
auth=auth,
9595
timeout=timeout,
9696
allow_redirects=allow_redirects,
97-
is_oauth=is_oauth
97+
is_oauth=is_oauth,
9898
)

twilio/base/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def get_uri(code: int) -> str:
6262
"\n\n{twilio_returned}\n\n{message}\n".format(
6363
red_error=red("HTTP Error"),
6464
request_was=white("Your request was:"),
65-
http_line=teal("%s %s" % (self.method, self.uri)),
65+
http_line=teal("%s %s" % (self.method, self.uri, self.data, self.uri)),
66+
http_line=teal("%s %s" % (self.data, self.headers)),
6667
twilio_returned=white("Twilio returned the following information:"),
6768
message=blue(str(self.msg)),
6869
)

twilio/base/oauth_token_base.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
from twilio.http.token_manager_initializer import TokenManagerInitializer
22

3+
34
# Dynamic import utility function
45
def dynamic_import(module_name, class_name):
56
from importlib import import_module
7+
68
module = import_module(module_name)
79
return getattr(module, class_name)
810

11+
912
class OauthTokenBase:
1013
def get_oauth_token(self, domain: str, version: str, username: str, password: str):
1114
Domain = dynamic_import("twilio.base.domain", "Domain")
1215
Version = dynamic_import("twilio.base.version", "Version")
13-
BearerTokenHTTPClient = dynamic_import("twilio.http.bearer_token_http_client", "BearerTokenHTTPClient")
14-
OrgTokenManager = dynamic_import("twilio.http.orgs_token_manager", "OrgTokenManager")
16+
BearerTokenHTTPClient = dynamic_import(
17+
"twilio.http.bearer_token_http_client", "BearerTokenHTTPClient"
18+
)
19+
OrgTokenManager = dynamic_import(
20+
"twilio.http.orgs_token_manager", "OrgTokenManager"
21+
)
1522
Client = dynamic_import("twilio.rest", "Client")
1623
try:
1724
orgs_token_manager = TokenManagerInitializer.get_token_manager()
18-
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version))
25+
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(
26+
Version(Domain(Client(username, password), domain), version)
27+
)
1928
except Exception:
20-
orgs_token_manager = OrgTokenManager(grant_type='client_credentials',
21-
client_id=username,
22-
client_secret=password)
29+
orgs_token_manager = OrgTokenManager(
30+
grant_type="client_credentials",
31+
client_id=username,
32+
client_secret=password,
33+
)
2334
TokenManagerInitializer().set_token_manager(orgs_token_manager)
24-
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version))
35+
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(
36+
Version(Domain(Client(username, password), domain), version)
37+
)

0 commit comments

Comments
 (0)