Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

add versioning support to the API

jonaswinkler3d3300ac32b5
diff --git a/src-ui/src/app/app.module.ts b/src-ui/src/app/app.module.ts
index 46d8df9a1..0cc42292b 100644
--- a/src-ui/src/app/app.module.ts
+++ b/src-ui/src/app/app.module.ts
@@ -62,6 +62,7 @@ import { CustomDatePipe } from './pipes/custom-date.pipe';
import { DateComponent } from './components/common/input/date/date.component';
import { ISODateTimeAdapter } from './utils/ngb-iso-date-time-adapter';
import { LocalizedDateParserFormatter } from './utils/ngb-date-parser-formatter';
+import { ApiVersionInterceptor } from './interceptors/api-version.interceptor';
import localeFr from '@angular/common/locales/fr';
import localeNl from '@angular/common/locales/nl';
@@ -144,6 +145,10 @@ registerLocaleData(localeEnGb)
provide: HTTP_INTERCEPTORS,
useClass: CsrfInterceptor,
multi: true
+ },{
+ provide: HTTP_INTERCEPTORS,
+ useClass: ApiVersionInterceptor,
+ multi: true
},
FilterPipe,
DocumentTitlePipe,
diff --git a/src-ui/src/app/interceptors/api-version.interceptor.spec.ts b/src-ui/src/app/interceptors/api-version.interceptor.spec.ts
new file mode 100644
index 000000000..7ad51f687
--- /dev/null
+++ b/src-ui/src/app/interceptors/api-version.interceptor.spec.ts
@@ -0,0 +1,16 @@
+import { TestBed } from '@angular/core/testing';
+
+import { ApiVersionInterceptor } from './api-version.interceptor';
+
+describe('ApiVersionInterceptor', () => {
+ beforeEach(() => TestBed.configureTestingModule({
+ providers: [
+ ApiVersionInterceptor
+ ]
+ }));
+
+ it('should be created', () => {
+ const interceptor: ApiVersionInterceptor = TestBed.inject(ApiVersionInterceptor);
+ expect(interceptor).toBeTruthy();
+ });
+});
diff --git a/src-ui/src/app/interceptors/api-version.interceptor.ts b/src-ui/src/app/interceptors/api-version.interceptor.ts
new file mode 100644
index 000000000..95aa6ef16
--- /dev/null
+++ b/src-ui/src/app/interceptors/api-version.interceptor.ts
@@ -0,0 +1,25 @@
+import { Injectable } from '@angular/core';
+import {
+ HttpRequest,
+ HttpHandler,
+ HttpEvent,
+ HttpInterceptor
+} from '@angular/common/http';
+import { Observable } from 'rxjs';
+import { environment } from 'src/environments/environment';
+
+@Injectable()
+export class ApiVersionInterceptor implements HttpInterceptor {
+
+ constructor() {}
+
+ intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
+ request = request.clone({
+ setHeaders: {
+ 'Accept': `application/json; version=${environment.apiVersion}`
+ }
+ })
+
+ return next.handle(request);
+ }
+}
diff --git a/src-ui/src/app/services/auth.interceptor.ts b/src-ui/src/app/services/auth.interceptor.ts
deleted file mode 100644
index e69de29bb..000000000
diff --git a/src-ui/src/environments/environment.prod.ts b/src-ui/src/environments/environment.prod.ts
index 6c97ac1cb..50cf6d954 100644
--- a/src-ui/src/environments/environment.prod.ts
+++ b/src-ui/src/environments/environment.prod.ts
@@ -1,6 +1,7 @@
export const environment = {
production: true,
apiBaseUrl: "/api/",
+ apiVersion: "1",
appTitle: "Paperless-ng",
version: "1.2.1",
webSocketHost: window.location.host,
diff --git a/src-ui/src/environments/environment.ts b/src-ui/src/environments/environment.ts
index b2c40e261..b0ac843d5 100644
--- a/src-ui/src/environments/environment.ts
+++ b/src-ui/src/environments/environment.ts
@@ -5,6 +5,7 @@
export const environment = {
production: false,
apiBaseUrl: "http://localhost:8000/api/",
+ apiVersion: "1",
appTitle: "Paperless-ng",
version: "DEVELOPMENT",
webSocketHost: "localhost:8000",
diff --git a/src/paperless/settings.py b/src/paperless/settings.py
index 4c860351f..48712954c 100644
--- a/src/paperless/settings.py
+++ b/src/paperless/settings.py
@@ -112,7 +112,10 @@ REST_FRAMEWORK = {
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication'
- ]
+ ],
+ 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',
+ 'DEFAULT_VERSION': 'v1',
+ 'ALLOWED_VERSIONS': ['1', '2']
}
if DEBUG:
@@ -142,7 +145,7 @@ ASGI_APPLICATION = "paperless.asgi.application"
STATIC_URL = os.getenv("PAPERLESS_STATIC_URL", "/static/")
-# what is this used for?
+# TODO: what is this used for?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',