8000 add request logging and hooking by tysonholub · Pull Request #385 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

add request logging and hooking #385

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 3 commits into from
Oct 20, 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
34 changes: 29 additions & 5 deletions twilio/http/http_client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
from requests import Request, Session
from requests import Request, Session, hooks

from twilio.http import HttpClient
from twilio.http.response import Response
from twilio.http.request import Request as TwilioRequest
import logging
from twilio.compat import urlencode

_logger = logging.getLogger('twilio.http_client')


class TwilioHttpClient(HttpClient):
"""
General purpose HTTP Client for interacting with the Twilio API
"""
def __init__(self, pool_connections=True):
def __init__(self, pool_connections=True, request_hooks=None):
self.session = Session() if pool_connections else None
self.last_request = None
self.last_response = None
self.request_hooks = request_hooks or hooks.default_hooks()

def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
allow_redirects=False):
Expand All @@ -33,11 +38,28 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
:rtype: A :class:`Response <twilio.rest.http.response.Response>` object
"""

kwargs = {
'method': method.upper(),
'url': url,
'params': params,
'data': data,
'headers': headers,
'auth': auth,
'hooks': self.request_hooks
}

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

self.last_response = None
session = self.session or Session()
request = Request(method.upper(), url, params=params, data=data, headers=headers, auth=auth)
self.last_request = TwilioRequest(method.upper(), url, auth=auth, params=params, data=data,
headers=headers)
request = Request(**kwargs)
self.last_request = TwilioRequest(**kwargs)

prepped_request = session.prepare_request(request)
response = session.send(
Expand All @@ -46,6 +68,8 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
timeout=timeout,
)

_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)

return self.last_response
3 changes: 2 additions & 1 deletion twilio/http/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def __init__(self,
auth=ANY,
params=ANY,
data=ANY,
headers=ANY):
headers=ANY,
**kwargs):
self.method = method.upper()
self.url = url
self.auth = auth
Expand Down
4 changes: 2 additions & 2 deletions twilio/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Client(object):
""" A client for accessing the Twilio API. """

def __init__(self, username=None, password=None, account_sid=None, region=None,
http_client=None, environment=None):
http_client=None, environment=None, request_hooks=None):
"""
Initializes the Twilio Client

Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self, username=None, password=None, account_sid=None, region=None,

self.auth = (self.username, self.password)
""" :type : tuple(str, str) """
self.http_client = http_client or TwilioHttpClient()
self.http_client = http_client or TwilioHttpClient(request_hooks=request_hooks)
""" :type : HttpClient """

# Domains
Expand Down
0