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 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
add request logging and hooking
  • Loading branch information
Tyson Holub committed Oct 17, 2017
commit 7e80f7034ba4e4db21ebf970c4cd84ba9bd90777
35 changes: 32 additions & 3 deletions twilio/http/http_client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
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
import json
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should import things via the compat.py module which handles version specific imports like this for you.


_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,9 +42,27 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
:rtype: A :class:`Response <twilio.rest.http.response.Response>` object
"""

kwargs = dict(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Let's just use a dict literal here.

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)
request = Request(**kwargs)
self.last_request = TwilioRequest(method.upper(), url, auth=auth, params=params, data=data,
headers=headers)

Expand All @@ -46,6 +73,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
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