8000 Remove shipped certs (#376) · twilio/twilio-python@d75a66b · GitHub
[go: up one dir, main page]

Skip to content

Commit d75a66b

Browse files
jingmingdougblack
authored andcommitted
Remove shipped certs (#376)
* Remove shipped certs * Add tests
1 parent da7a6b2 commit d75a66b

File tree

7 files changed

+68
-3442
lines changed

7 files changed

+68
-3442
lines changed
8000

CHANGES.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ twilio-python Changelog
33

44
Here you can see the full list of changes between each twilio-python release.
55

6+
[2017-08-18] Version 6.6.0
7+
---------------------------
8+
- Add connection pooling. This is enabled by default and will use one Session for all requests
9+
in Client.
10+
- To disable this, you can turn this off when creating your Twilio client.
11+
```python
12+
from twilio.rest import Client
13+
from twilio.http.http_client import TwilioHttpClient
14+
15+
client = Client(
16+
username,
17+
password,
18+
http_client=TwilioHttpClient(pool_connections=False)
19+
)
20+
```
21+
622
[2017-08-10] Version 6.5.1
723
---------------------------
824
Fixed PyJWT >= 1.5.1 exception
@@ -17,7 +33,6 @@ Fixed PyJWT >= 1.5.1 exception
1733
- Add `video_codec` enum and `video_codecs` parameter, which can be set to either `VP8` or `H264` during room creation.
1834
- Restrict recordings page size to 100
1935

20-
2136
[2017-07-27] Version 6.5.0
2237
---------------------------
2338
This release adds Beta and Preview products to main artifact.

tests/unit/http/test_http_client.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,47 @@ def test_request_with_unicode_response(self):
5050
self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
5151
self.assertEqual(200, response.status_code)
5252
self.assertEqual('testing-unicode: Ω≈ç√, 💩', response.content)
53+
54+
55+
class TestHttpClientSession(unittest.TestCase):
56+
57+
def setUp(self):
58+
self.session_patcher = patch('twilio.http.http_client.Session')
59+
self.session_constructor_mock = self.session_patcher.start()
60+
61+
def tearDown(self):
62+
self.session_patcher.stop()
63+
64+
def _setup_session_response(self, value):
65+
session_mock = Mock(wraps=Session())
66+
request_mock = Mock()
67+
68+
session_mock.prepare_request.return_value = request_mock
69+
session_mock.send.return_value = Response(200, value)
70+
self.session_constructor_mock.return_value = session_mock
71+
72+
def test_session_preserved(self):
73+
self._setup_session_response('response_1')
74+
75+
client = TwilioHttpClient()
76+
response_1 = client.request('GET', 'https://api.twilio.com')
77+
78+
self._setup_session_response('response_2')
79+
response_2 = client.request('GET', 'https://api.twilio.com')
80+
81+
# Used same session, response should be the same
82+
self.assertEqual(response_1.content, 'response_1')
83+
self.assertEqual(response_2.content, 'response_1')
84+
85+
def test_session_not_preserved(self):
86+
self._setup_session_response('response_1')
87+
88+
client = TwilioHttpClient(pool_connections=False)
89+
response_1 = client.request('GET', 'https://api.twilio.com')
90+
91+
self._setup_session_response('response_2')
92+
response_2 = client.request('GET', 'https://api.twilio.com')
93+
94+
# Used different session, responses should be different
95+
self.assertEqual(response_1.content, 'response_1')
96+
self.assertEqual(response_2.content, 'response_2')

twilio/conf/cacert.pem

Lines changed: 0 additions & 3376 deletions
This file was deleted.

twilio/http/__init__.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
1-
import os
2-
31
from twilio.base.exceptions import TwilioException
42

53

6-
def get_cert_file():
7-
""" Get the cert file location or bail """
8-
# XXX - this currently fails test coverage because we don't actually go
9-
# over the network anywhere. Might be good to have a test that stands up a
10-
# local server and authenticates against it.
11-
try:
12-
# Apparently __file__ is not available in all places so wrapping this
13-
# in a try/catch
14-
current_path = os.path.realpath(__file__)
15-
ca_cert_path = os.path.join(current_path, '..', '..', 'conf', 'cacert.pem')
16-
return os.path.abspath(ca_cert_path)
17-
except Exception:
18-
# None means use the default system file
19-
return None
20-
21-
224
class HttpClient(object):
235
"""
246
An abstract class representing an HTTP client.

twilio/http/debug.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

twilio/http/http_client.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
from requests import Request, Session
22

3-
from twilio.http import HttpClient, get_cert_file
3+
from twilio.http import HttpClient
44
from twilio.http.response import Response
55

66

77
class TwilioHttpClient(HttpClient):
88
"""
99
General purpose HTTP Client for interacting with the Twilio API
1010
"""
11-
def __init__(self, connection_pool=True):
12-
if connection_pool:
13-
self.session = Session()
14-
self.session.verify = get_cert_file()
15-
else:
16-
self.session = None
11+
def __init__(self, pool_connections=True):
12+
self.session = Session() if pool_connections else None
1713

1814
def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
1915
allow_redirects=False):
@@ -33,11 +29,7 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
3329
:return: An http response
3430
:rtype: A :class:`Response <twilio.rest.http.response.Response>` object
3531
"""
36-
session = self.session
37-
if session is None:
38-
session = Session()
39-
session.verify = get_cert_file()
40-
32+
session = self.session or Session()
4133
request = Request(method.upper(), url, params=params, data=data, headers=headers, auth=auth)
4234

4335
prepped_request = session.prepare_request(request)

twilio/http/validation_client.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from requests import Request, Session
44

55
from twilio.compat import urlparse
6-
from twilio.http import HttpClient, get_cert_file
6+
from twilio.http import HttpClient
77
from twilio.http.response import Response
88
from twilio.jwt.validation import ClientValidationJwt
99

@@ -15,7 +15,7 @@
1515
class ValidationClient(HttpClient):
1616
__SIGNED_HEADERS = ['authorization', 'host']
1717

18-
def __init__(self, account_sid, api_key_sid, credential_sid, private_key):
18+
def __init__(self, account_sid, api_key_sid, credential_sid, private_key, pool_connections=True):
1919
"""
2020
Build a ValidationClient which signs requests with private_key and allows Twilio to
2121
validate request has not been tampered with.
@@ -30,6 +30,7 @@ def __init__(self, account_sid, api_key_sid, credential_sid, private_key):
3030
self.credential_sid = credential_sid
3131
self.api_key_sid = api_key_sid
3232
self.private_key = private_key
33+
self.session = Session() if pool_connections else None
3334

3435
def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
3536
allow_redirects=False):
@@ -49,9 +50,7 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
4950
:return: An http response
5051
:rtype: A :class:`Response <twilio.rest.http.response.Response>` object
5152
"""
52-
session = Session()
53-
session.verify = get_cert_file()
54-
53+
session = self.session or Session()
5554
request = Request(method.upper(), url, params=params, data=data, headers=headers, auth=auth)
5655
prepared_request = session.prepare_request(request)
5756

0 commit comments

Comments
 (0)
0