8000 feat: support http proxy in TwilioHttpClient by thehackercat · Pull Request #500 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

feat: support http proxy in TwilioHttpClient #500

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 2 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: support http proxy in TwilioHttpClient
  • Loading branch information
thehackercat committed Dec 5, 2019
commit dd6e802a5080d7b41708348a847989e09e11f12e
9 changes: 9 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,15 @@ def test_last_response_empty_on_error(self):
self.assertIsNotNone(self.client.last_request)
self.assertIsNone(self.client.last_response)

def test_request_behind_proxy(self):
proxies = {
'http': 'http://proxy.twilio.com',
'https': 'https://proxy.twilio.com',
}
self.client = TwilioHttpClient(proxy=proxies)
self.client.request('doesnt matter', 'doesnt matter')
self.assertEqual(self.client.proxy, self.session_mock.proxies)


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 = self.proxy
request = Request(**kwargs)
self.last_request = TwilioRequest(**kwargs)

Expand Down
0