Skip to content

Commit

Permalink
API with or without Host feature added
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalanandl177 committed Nov 12, 2020
1 parent beea3cf commit d86f0aa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DRF API Logger
![version](https://img.shields.io/badge/version-0.0.6-blue.svg)
![version](https://img.shields.io/badge/version-0.0.7-blue.svg)
[![PyPi Downloads](http://pepy.tech/badge/drf-api-logger)](http://pepy.tech/project/drf-api-logger)
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)
[![GitHub issues](https://img.shields.io/github/issues/Naereen/StrapDown.js.svg)](https://github.com/vishalanandl177/DRF-API-Logger/issues/)
Expand Down Expand Up @@ -152,3 +152,31 @@ DRF_API_LOGGER_SKIP_URL_NAME = ['url_name1', 'url_name2']
```

Note: It does not log Django Admin Panel API calls.

### API with or without Host
You can specify endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
```python
DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE' # Default to ABSOLUTE if not specified
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI
```
Considering we are accessing the following URL: http://127.0.0.1:8000/api/v1/?page=123
DRF_API_LOGGER_PATH_TYPE possible values are:
1. ABSOLUTE (Default) :

Function used ```request.build_absolute_uri()```

Output: ```http://127.0.0.1:8000/api/v1/?page=123```

2. FULL_PATH

Function used ```request.get_full_path()```

Output: ```/api/v1/?page=123```

3. RAW_URI

Function used ```request.get_raw_uri()```

Output: ```http://127.0.0.1:8000/api/v1/?page=123```

Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return insecure URI.
21 changes: 15 additions & 6 deletions drf_api_logger/middleware/api_logger_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def __init__(self, get_response):
if hasattr(settings, 'DRF_API_LOGGER_SIGNAL'):
self.DRF_API_LOGGER_SIGNAL = settings.DRF_API_LOGGER_SIGNAL

self.DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE'
if hasattr(settings, 'DRF_API_LOGGER_PATH_TYPE'):
if settings.DRF_API_LOGGER_PATH_TYPE in ['ABSOLUTE', 'RAW_URI', 'FULL_PATH']:
self.DRF_API_LOGGER_PATH_TYPE = settings.DRF_API_LOGGER_PATH_TYPE

self.DRF_API_LOGGER_SKIP_URL_NAME = []
if hasattr(settings, 'DRF_API_LOGGER_SKIP_URL_NAME'):
if type(settings.DRF_API_LOGGER_SKIP_URL_NAME) is tuple or type(
Expand Down Expand Up @@ -60,7 +65,6 @@ def __call__(self, request):
if namespace in self.DRF_API_LOGGER_SKIP_NAMESPACE:
return self.get_response(request)

path = bleach.clean(request.get_full_path())
start_time = time.time()
request_data = ''
try:
Expand All @@ -87,11 +91,16 @@ def __call__(self, request):
response_body = json.loads(response.content)
else:
response_body = '** Not JSON **'
protocol = 'http'
if request.is_secure():
protocol = 'https'
host = request.META['HTTP_HOST']
api = '%s://%s%s' % (protocol, host, path,)

if self.DRF_API_LOGGER_PATH_TYPE == 'ABSOLUTE':
api = request.build_absolute_uri()
elif self.DRF_API_LOGGER_PATH_TYPE == 'FULL_PATH':
api = request.get_full_path()
elif self.DRF_API_LOGGER_PATH_TYPE == 'RAW_URI':
api = request.get_raw_uri()
else:
api = request.build_absolute_uri()

data = dict(
api=api,
headers=headers,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def get_long_desc():

setuptools.setup(
name="drf_api_logger",
version="0.0.6",
version="0.0.7",
author="Vishal Anand",
author_email="vishalanandl177@gmail.com",
description="An API Logger for your Django Rest Framework project.",
Expand Down

0 comments on commit d86f0aa

Please sign in to comment.