8000 add request logging and hooking · twilio/twilio-python@1e05c9b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e05c9b

Browse files
author
Tyson Holub
committed
add request logging and hooking
1 parent f6af212 commit 1e05c9b

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

twilio/http/http_client.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
from requests import Request, Session
1+
from requests import Request, Session, hooks
22

33
from twilio.http import HttpClient, get_cert_file
44
from twilio.http.response import Response
< 10000 /code>5+
import logging
6+
import json
7+
try:
8+
from urllib.parse import urlencode
9+
except ImportError:
10+
from urllib import urlencode
11+
12+
_logger = logging.getLogger('twilio.http_client')
513

614

715
class TwilioHttpClient(HttpClient):
816
"""
917
General purpose HTTP Client for interacting with the Twilio API
1018
"""
11-
def __init__(self, connection_pool=True):
19+
def __init__(self, connection_pool=True, request_hooks=None):
1220
if connection_pool:
1321
self.session = Session()
1422
self.session.verify = get_cert_file()
15-
else:
23+
else:
1624
self.session = None
17-
25+
26+
self.request_hooks = request_hooks or hooks.default_hooks()
27+
1828
def request(self, method, url, params=None, data=None, headers=None, auth=None, timeout=None,
1929
allow_redirects=False):
2030
"""
@@ -38,7 +48,25 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
3848
session = Session()
3949
session.verify = get_cert_file()
4050

41-
request = Request(method.upper(), url, params=params, data=data, headers=headers, auth=auth)
51+
kwargs = dict(
52+
method=method.upper(),
53+
url=url,
54+
params=params,
55+
data=data,
56+
headers=headers,
57+
auth=auth,
58+
hooks=self.request_hooks
59+
)
60+
61+
if params:
62+
_logger.info('{method} Request: {url}?{query}'.format(query=urlencode(params), **kwargs))
63+
_logger.info('PARAMS: {params}'.format(**kwargs))
64+
else:
65+
_logger.info('{method} Request: {url}'.format(**kwargs))
66+
if data:
67+
_logger.info('PAYLOAD: {data}'.format(**kwargs))
68+
69+
request = Request(**kwargs)
4270

4371
prepped_request = session.prepare_request(request)
4472
response = session.send(
@@ -47,4 +75,12 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
4775
timeout=timeout,
4876
)
4977

78+
if hasattr(response, 'json'):
79+
# strip JSON of possible prettyprint
80+
text = json.dumps(response.json())
81+
else:
82+
text = response.text
83+
84+
_logger.info('{method} Response: {status} {text}'.format(method=method, status=response.status_code, text=text))
85+
5086
return Response(int(response.status_code), response.text)

twilio/rest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Client(object):
1717
""" A client for accessing the Twilio API. """
1818

1919
def __init__(self, username=None, password=None, account_sid=None, region=None,
20-
http_client=None, environment=None):
20+
http_client=None, environment=None, request_hooks=None):
2121
"""
2222
Initializes the Twilio Client
2323
@@ -47,7 +47,7 @@ def __init__(self, username=None, password=None, account_sid=None, region=None,
4747

4848
self.auth = (self.username, self.password)
4949
""" :type : tuple(str, str) """
50-
self.http_client = http_client or TwilioHttpClient()
50+
self.http_client = http_client or TwilioHttpClient(request_hooks=request_hooks)
5151
""" :type : HttpClient """
5252

5353
# Domains

0 commit comments

Comments
 (0)
0