diff --git a/README.md b/README.md index 4b525ac..5c5dd31 100644 --- a/README.md +++ b/README.md @@ -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/) @@ -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. diff --git a/drf_api_logger/middleware/api_logger_middleware.py b/drf_api_logger/middleware/api_logger_middleware.py index 83a5a6a..6682f9f 100644 --- a/drf_api_logger/middleware/api_logger_middleware.py +++ b/drf_api_logger/middleware/api_logger_middleware.py @@ -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( @@ -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: @@ -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, diff --git a/setup.py b/setup.py index 57bc36c..d36e05e 100644 --- a/setup.py +++ b/setup.py @@ -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.",