8000 feat: support http proxy in TwilioHttpClient (#500) · aditya274/twilio-python@153020a · GitHub
[go: up one dir, main page]

Skip to content

Commit 153020a

Browse files
thehackercatchildish-sambino
authored andcommitted
feat: support http proxy in TwilioHttpClient (twilio#500)
1 parent a9d48f0 commit 153020a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

tests/unit/http/test_http_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ def test_last_response_empty_on_error(self):
134134
self.assertIsNotNone(self.client.last_request)
135135
self.assertIsNone(self.client.last_response)
136136

137+
def test_request_behind_proxy(self):
138+
proxies = {
139+
'http': 'http://proxy.twilio.com',
140+
'https': 'https://proxy.twilio.com',
141+
}
142+
self.client = TwilioHttpClient(proxy=proxies)
143+
self.client.request('doesnt matter', 'doesnt matter')
144+
self.assertEqual(proxies, self.session_mock.proxies)
145+
137146

138147
class TestHttpClientSession(unittest.TestCase):
139148

twilio/http/http_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TwilioHttpClient(HttpClient):
1313
"""
1414
General purpose HTTP Client for interacting with the Twilio API
1515
"""
16-
def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logger=_logger):
16+
def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logger=_logger, proxy=None):
1717
"""
1818
Constructor for the TwilioHttpClient
1919
@@ -22,6 +22,7 @@ def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logg
2222
:param int timeout: Timeout for the requests.
2323
Timeout should never be zero (0) or less.
2424
:param logger
25+
:param dict proxy: Http proxy for the requests session
2526
"""
2627
self.session = Session() if pool_connections else None
2728
self.last_request = None
@@ -32,6 +33,7 @@ def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logg
3233
if timeout is not None and timeout <= 0:
3334
raise ValueError(timeout)
3435
self.timeout = timeout
36+
self.proxy = proxy
3537

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

7577
self.last_response = None
7678
session = self.session or Session()
79+
if self.proxy:
80+
session.proxies = self.proxy
7781
request = Request(**kwargs)
7882
self.last_request = TwilioRequest(**kwargs)
7983

0 commit comments

Comments
 (0)
0