Skip to content

Commit d86f0aa

Browse files
API with or without Host feature added
1 parent beea3cf commit d86f0aa

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# DRF API Logger
2-
![version](https://img.shields.io/badge/version-0.0.6-blue.svg)
2+
![version](https://img.shields.io/badge/version-0.0.7-blue.svg)
33
[![PyPi Downloads](http://pepy.tech/badge/drf-api-logger)](http://pepy.tech/project/drf-api-logger)
44
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)
55
[![GitHub issues](https://img.shields.io/github/issues/Naereen/StrapDown.js.svg)](https://GitHub.com/vishalanandl177/DRF-API-Logger/issues/)
@@ -152,3 +152,31 @@ DRF_API_LOGGER_SKIP_URL_NAME = ['url_name1', 'url_name2']
152152
```
153153

154154
Note: It does not log Django Admin Panel API calls.
155+
156+
### API with or without Host
157+
You can specify endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
158+
```python
159+
DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE' # Default to ABSOLUTE if not specified
160+
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI
161+
```
162+
Considering we are accessing the following URL: http://127.0.0.1:8000/api/v1/?page=123
163+
DRF_API_LOGGER_PATH_TYPE possible values are:
164+
1. ABSOLUTE (Default) :
165+
166+
Function used ```request.build_absolute_uri()```
167+
168+
Output: ```http://127.0.0.1:8000/api/v1/?page=123```
169+
170+
2. FULL_PATH
171+
172+
Function used ```request.get_full_path()```
173+
174+
Output: ```/api/v1/?page=123```
175+
176+
3. RAW_URI
177+
178+
Function used ```request.get_raw_uri()```
179+
180+
Output: ```http://127.0.0.1:8000/api/v1/?page=123```
181+
182+
Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return insecure URI.

drf_api_logger/middleware/api_logger_middleware.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ def __init__(self, get_response):
2828
if hasattr(settings, 'DRF_API_LOGGER_SIGNAL'):
2929
self.DRF_API_LOGGER_SIGNAL = settings.DRF_API_LOGGER_SIGNAL
3030

31+
self.DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE'
32+
if hasattr(settings, 'DRF_API_LOGGER_PATH_TYPE'):
33+
if settings.DRF_API_LOGGER_PATH_TYPE in ['ABSOLUTE', 'RAW_URI', 'FULL_PATH']:
34+
self.DRF_API_LOGGER_PATH_TYPE = settings.DRF_API_LOGGER_PATH_TYPE
35+
3136
self.DRF_API_LOGGER_SKIP_URL_NAME = []
3237
if hasattr(settings, 'DRF_API_LOGGER_SKIP_URL_NAME'):
3338
if type(settings.DRF_API_LOGGER_SKIP_URL_NAME) is tuple or type(
@@ -60,7 +65,6 @@ def __call__(self, request):
6065
if namespace in self.DRF_API_LOGGER_SKIP_NAMESPACE:
6166
return self.get_response(request)
6267

63-
path = bleach.clean(request.get_full_path())
6468
start_time = time.time()
6569
request_data = ''
6670
try:
@@ -87,11 +91,16 @@ def __call__(self, request):
8791
response_body = json.loads(response.content)
8892
else:
8993
response_body = '** Not JSON **'
90-
protocol = 'http'
91-
if request.is_secure():
92-
protocol = 'https'
93-
host = request.META['HTTP_HOST']
94-
api = '%s://%s%s' % (protocol, host, path,)
94+
95+
if self.DRF_API_LOGGER_PATH_TYPE == 'ABSOLUTE':
96+
api = request.build_absolute_uri()
97+
elif self.DRF_API_LOGGER_PATH_TYPE == 'FULL_PATH':
98+
api = request.get_full_path()
99+
elif self.DRF_API_LOGGER_PATH_TYPE == 'RAW_URI':
100+
api = request.get_raw_uri()
101+
else:
102+
api = request.build_absolute_uri()
103+
95104
data = dict(
96105
api=api,
97106
headers=headers,

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def get_long_desc():
99

1010
setuptools.setup(
1111
name="drf_api_logger",
12-
version="0.0.6",
12+
version="0.0.7",
1313
author="Vishal Anand",
1414
author_email="[email protected]",
1515
description="An API Logger for your Django Rest Framework project.",

0 commit comments

Comments
 (0)