Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add API logging to client #123

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 9 commits into from
Aug 6, 2020
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
23 changes: 22 additions & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,25 @@ HTTP request to delete elements in a source.
```python
response = client.api_keys._(api_keys_id).delete()
# print(response) as shown above
```
```

## LOGGING
Logging namespace `python_http_client.client` is available on API Client.

Example to stdout:

```
>>> import logging
>>> import sys
>>> logger = logging.getLogger('python_http_client.client')
>>> logger.setLevel(logging.DEBUG)
>>> handler = logging.StreamHandler(sys.stdout)
>>> handler.setLevel(logging.DEBUG)
>>> formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
>>> handler.setFormatter(formatter)
>>> logger.addHandler(handler)
>>> client.templates.get()
2019-03-08 17:21:25,329 - python_http_client.client - DEBUG - GET Request: https://api.sendgrid.com/v3/templates
2019-03-08 17:21:25,329 - python_http_client.client - DEBUG - HEADERS: {'Authorization': 'Bearer redacted_token', 'Accept': 'application/json', 'User-agent': 'sendgrid/5.6.0;python'}
2019-03-08 17:21:25,696 - python_http_client.client - DEBUG - GET Response: 200 {"templates":[]}
```
27 changes: 25 additions & 2 deletions python_http_client/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""HTTP Client library"""
import json

import logging
from .exceptions import handle_error

try:
Expand All @@ -14,6 +14,8 @@
from urllib2 import HTTPError
from urllib import urlencode

_logger = logging.getLogger(__name__)


class Response(object):
"""Holds the response from an API call."""
Expand Down Expand Up @@ -173,6 +175,10 @@ def _make_request(self, opener, request, timeout=None):
try:
return opener.open(request, timeout=timeout)
except HTTPError as err:
_logger.debug('{method} Response: {status} {body}'.format(
method=request.get_method(),
status=exc.status_code,
body=exc.body))
exc = handle_error(err)
exc.__cause__ = None
raise exc
Expand Down Expand Up @@ -258,9 +264,26 @@ def http_request(
)
request.get_method = lambda: method

return Response(
_logger.debug('{method} Request: {url}'.format(
method=method,
url=request.get_full_url()))
if request.data:
_logger.debug('PAYLOAD: {data}'.format(
data=request.data))
_logger.debug('HEADERS: {headers}'.format(
headers=request.headers))

response = Response(
self._make_request(opener, request, timeout=timeout)
)

_logger.debug('{method} Response: {status} {body}'.format(
method=method,
status=response.status_code,
body=response.body))

return response

return http_request
else:
# Add a segment to the URL
Expand Down