Skip to content

Remove shipped certs #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ twilio-python Changelog

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

[2017-08-18] Version 6.6.0
---------------------------
- Add connection pooling. This is enabled by default and will use one Session for all requests
in Client.
- To disable this, you can turn this off when creating your Twilio client.
```python
from twilio.rest import Client
from twilio.http.http_client import TwilioHttpClient

client = Client(
username,
password,
http_client=TwilioHttpClient(pool_connections=False)
)
```

[2017-08-10] Version 6.5.1
---------------------------
Fixed PyJWT >= 1.5.1 exception
Expand All @@ -17,7 +33,6 @@ Fixed PyJWT >= 1.5.1 exception
- Add `video_codec` enum and `video_codecs` parameter, which can be set to either `VP8` or `H264` during room creation.
- Restrict recordings page size to 100


[2017-07-27] Version 6.5.0
---------------------------
This release adds Beta and Preview products to main artifact.
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/http/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,47 @@ def test_request_with_unicode_response(self):
self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
self.assertEqual(200, response.status_code)
self.assertEqual('testing-unicode: Ω≈ç√, 💩', response.content)


class TestHttpClientSession(unittest.TestCase):

def setUp(self):
self.session_patcher = patch('twilio.http.http_client.Session')
self.session_constructor_mock = self.session_patcher.start()

def tearDown(self):
self.session_patcher.stop()

def _setup_session_response(self, value):
session_mock = Mock(wraps=Session())
request_mock = Mock()

session_mock.prepare_request.return_value = request_mock
session_mock.send.return_value = Response(200, value)
self.session_constructor_mock.return_value = session_mock

def test_session_preserved(self):
self._setup_session_response('response_1')

client = TwilioHttpClient()
response_1 = client.request('GET', 'https://api.twilio.com')

self._setup_session_response('response_2')
response_2 = client.request('GET', 'https://api.twilio.com')

# Used same session, response should be the same
self.assertEqual(response_1.content, 'response_1')
self.assertEqual(response_2.content, 'response_1')

def test_session_not_preserved(self):
self._setup_session_response('response_1')

client = TwilioHttpClient(pool_connections=False)
response_1 = client.request('GET', 'https://api.twilio.com')

self._setup_session_response('response_2')
response_2 = client.request('GET', 'https://api.twilio.com')

# Used different session, responses should be different
self.assertEqual(response_1.content, 'response_1')
self.assertEqual(response_2.content, 'response_2')
3,376 changes: 0 additions & 3,376 deletions twilio/conf/cacert.pem

This file was deleted.

18 changes: 0 additions & 18 deletions twilio/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
import os

from twilio.base.exceptions import TwilioException


def get_cert_file():
""" Get the cert file location or bail """
# XXX - this currently fails test coverage because we don't actually go
# over the network anywhere. Might be good to have a test that stands up a
# local server and authenticates against it.
try:
# Apparently __file__ is not available in all places so wrapping this
# in a try/catch
current_path = os.path.realpath(__file__)
ca_cert_path = os.path.join(current_path, '..', '..', 'conf', 'cacert.pem')
return os.path.abspath(ca_cert_path)
except Exception:
# None means use the default system file
return None


class HttpClient(object):
"""
An abstract class representing an HTTP client.
Expand Down
30 changes: 0 additions & 30 deletions twilio/http/debug.py

This file was deleted.

16 changes: 4 additions & 12 deletions twilio/http/http_client.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from requests import Request, Session

from twilio.http import HttpClient, get_cert_file
from twilio.http import HttpClient
from twilio.http.response import Response


class TwilioHttpClient(HttpClient):
"""
General purpose HTTP Client for interacting with the Twilio API
"""
def __init__(self, connection_pool=True):
if connection_pool:
self.session = Session()
self.session.verify = get_cert_file()
else:
self.session = None
def __init__(self, pool_connections=True):
self.session = Session() if pool_connections else None

def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
allow_redirects=False):
Expand All @@ -33,11 +29,7 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
:return: An http response
:rtype: A :class:`Response <twilio.rest.http.response.Response>` object
"""
session = self.session
if session is None:
session = Session()
session.verify = get_cert_file()

session = self.session or Session()
request = Request(method.upper(), url, params=params, data=data, headers=headers, auth=auth)

prepped_request = session.prepare_request(request)
Expand Down
9 changes: 4 additions & 5 deletions twilio/http/validation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from requests import Request, Session

from twilio.compat import urlparse
from twilio.http import HttpClient, get_cert_file
from twilio.http import HttpClient
from twilio.http.response import Response
from twilio.jwt.validation import ClientValidationJwt

Expand All @@ -15,7 +15,7 @@
class ValidationClient(HttpClient):
__SIGNED_HEADERS = ['authorization', 'host']

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

def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
allow_redirects=False):
Expand All @@ -49,9 +50,7 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
:return: An http response
:rtype: A :class:`Response <twilio.rest.http.response.Response>` object
"""
session = Session()
session.verify = get_cert_file()

session = self.session or Session()
request = Request(method.upper(), url, params=params, data=data, headers=headers, auth=auth)
prepared_request = session.prepare_request(request)

Expand Down