added versioning headers to the API

This commit is contained in:
jonaswinkler 2021-02-26 11:13:28 +01:00
parent 6a70b9c4b4
commit 068fdbd3f9
3 changed files with 34 additions and 0 deletions

View File

@ -305,6 +305,19 @@ API versions are specified by submitting an additional HTTP ``Accept`` header wi
If an invalid version is specified, Paperless 1.3.0 will respond with "406 Not Acceptable" and an error message in the body. If an invalid version is specified, Paperless 1.3.0 will respond with "406 Not Acceptable" and an error message in the body.
Earlier versions of Paperless will serve API version 1 regardless of whether a version is specified via the ``Accept`` header. Earlier versions of Paperless will serve API version 1 regardless of whether a version is specified via the ``Accept`` header.
If a client wishes to verify whether it is compatible with any given server, the following procedure should be performed:
1. Perform an *authenticated* request against any API endpoint. If the server is on version 1.3.0 or newer, the server will
add two custom headers to the response:
.. code::
X-Api-Version: 2
X-Version: 1.3.0
2. Determine whether the client is compatible with this server based on the presence/absence of these headers and their values if present.
API Changelog API Changelog
============= =============

View File

@ -0,0 +1,18 @@
from django.conf import settings
from paperless import version
class ApiVersionMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
if request.user.is_authenticated:
versions = settings.REST_FRAMEWORK['ALLOWED_VERSIONS']
response['X-Api-Version'] = versions[len(versions)-1]
response['X-Version'] = ".".join([str(_) for _ in version.__version__])
return response

View File

@ -115,6 +115,8 @@ REST_FRAMEWORK = {
], ],
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning', 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',
'DEFAULT_VERSION': '1', 'DEFAULT_VERSION': '1',
# Make sure these are ordered and that the most recent version appears
# last
'ALLOWED_VERSIONS': ['1', '2'] 'ALLOWED_VERSIONS': ['1', '2']
} }
@ -131,6 +133,7 @@ MIDDLEWARE = [
'django.middleware.locale.LocaleMiddleware', 'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'paperless.middleware.ApiVersionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',