-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from luizalabs/access_log_middleware
create access log middleware
- Loading branch information
Showing
8 changed files
with
178 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.conf import settings | ||
import logging | ||
|
||
from .shortcuts import get_oauth2_app | ||
from .toolkit_settings import API_VERSION, MIDDLEWARE_ACCESS_LOG_FORMAT | ||
|
||
class VersionHeaderMiddleware(object): | ||
try: | ||
from django.utils.deprecation import MiddlewareMixin | ||
except ImportError: | ||
MiddlewareMixin = object | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class VersionHeaderMiddleware(MiddlewareMixin): | ||
""" | ||
Add a X-API-Version header to the response. The version is taken from | ||
TOOLKIT['API_VERSION'] setting. | ||
""" | ||
|
||
def process_response(self, request, response): | ||
response['X-API-Version'] = settings.TOOLKIT['API_VERSION'] | ||
response['X-API-Version'] = API_VERSION | ||
return response | ||
|
||
|
||
class AccessLogMiddleware(MiddlewareMixin): | ||
|
||
LOG_FORMAT = MIDDLEWARE_ACCESS_LOG_FORMAT | ||
UNKNOWN_APP_NAME = 'unknown' | ||
|
||
def process_response(self, request, response): | ||
app = get_oauth2_app(request) | ||
|
||
app_name = getattr(app, 'name', self.UNKNOWN_APP_NAME) | ||
|
||
logger.info( | ||
self.LOG_FORMAT.format(app_name=app_name, | ||
request=request, | ||
response=response) | ||
) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
-e . | ||
bumpversion==0.5.3 | ||
django-oauth-toolkit==0.10.0 | ||
djangorestframework==3.5.2 | ||
djangorestframework==3.5.3 | ||
flake8==3.0.4 | ||
isort==4.2.5 | ||
mixer==5.5.8 | ||
mkdocs==0.15.3 | ||
mkdocs==0.16.0 | ||
mock==2.0.0 | ||
pytest-cov==2.4.0 | ||
pytest-django==3.0.0 | ||
pytest==3.0.3 | ||
pytest==3.0.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters