Skip to content

Commit

Permalink
feat: support http proxy in TwilioHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
thehackercat committed Dec 3, 2019
1 parent d3b9b9f commit 4bf9509
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions tests/unit/http/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ def test_last_response_empty_on_error(self):
self.assertIsNotNone(self.client.last_request)
self.assertIsNone(self.client.last_response)

def test_request_with_proxy(self):
self.client.proxy = {
'http': 'http://proxy.twilio.com:8118',
'https': 'https://proxy.twilio.com:8118',
}
self.request_mock.url = 'https://api.twilio.com/'
self.request_mock.headers = {'Host': 'other.twilio.com'}

response = self.client.request(
'doesnt matter', 'doesnt matter', None, None, None, None, None, None)

self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
self.assertEqual(200, response.status_code)


class TestHttpClientSession(unittest.TestCase):

Expand Down
6 changes: 5 additions & 1 deletion twilio/http/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TwilioHttpClient(HttpClient):
"""
General purpose HTTP Client for interacting with the Twilio API
"""
def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logger=_logger):
def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logger=_logger, proxy=None):
"""
Constructor for the TwilioHttpClient
Expand All @@ -22,6 +22,7 @@ def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logg
:param int timeout: Timeout for the requests.
Timeout should never be zero (0) or less.
:param logger
:param dict proxy: Http proxy for the requests session
"""
self.session = Session() if pool_connections else None
self.last_request = None
Expand All @@ -32,6 +33,7 @@ def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logg
if timeout is not None and timeout <= 0:
raise ValueError(timeout)
self.timeout = timeout
self.proxy = proxy

def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
allow_redirects=False):
Expand Down Expand Up @@ -74,6 +76,8 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,

self.last_response = None
session = self.session or Session()
if self.proxy:
session.proxies = proxy
request = Request(**kwargs)
self.last_request = TwilioRequest(**kwargs)

Expand Down

0 comments on commit 4bf9509

Please sign in to comment.