Skip to content

Commit

Permalink
Add support for passing custom logger into TwilioHttpClient (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
tysonholub authored and childish-sambino committed Oct 29, 2019
1 parent 1e3d778 commit e2d06b1
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions twilio/http/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ class TwilioHttpClient(HttpClient):
"""
General purpose HTTP Client for interacting with the Twilio API
"""
def __init__(self, pool_connections=True, request_hooks=None, timeout=None):
def __init__(self, pool_connections=True, request_hooks=None, timeout=None, logger=_logger):
"""
Constructor for the TwilioHttpClient
:param bool pool_connections
:param request_hooks
:param int timeout: Timeout for the requests.
Timeout should never be zero (0) or less.
:param logger
"""
self.session = Session() if pool_connections else None
self.last_request = None
self.last_response = None
self.logger = logger
self.request_hooks = request_hooks or hooks.default_hooks()

if timeout is not None and timeout <= 0:
Expand Down Expand Up @@ -63,12 +65,12 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
}

if params:
_logger.info('{method} Request: {url}?{query}'.format(query=urlencode(params), **kwargs))
_logger.info('PARAMS: {params}'.format(**kwargs))
self.logger.info('{method} Request: {url}?{query}'.format(query=urlencode(params), **kwargs))
self.logger.info('PARAMS: {params}'.format(**kwargs))
else:
_logger.info('{method} Request: {url}'.format(**kwargs))
self.logger.info('{method} Request: {url}'.format(**kwargs))
if data:
_logger.info('PAYLOAD: {data}'.format(**kwargs))
self.logger.info('PAYLOAD: {data}'.format(**kwargs))

self.last_response = None
session = self.session or Session()
Expand All @@ -82,7 +84,9 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
timeout=timeout if timeout is not None else self.timeout,
)

_logger.info('{method} Response: {status} {text}'.format(method=method, status=response.status_code, text=response.text))
self.logger.info('{method} Response: {status} {text}'.format(
method=method, status=response.status_code, text=response.text)
)

self.last_response = Response(int(response.status_code), response.text)

Expand Down

0 comments on commit e2d06b1

Please sign in to comment.