allerta-vvf/frontend/src/app/_services/api-client.service.ts

136 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-12-22 23:06:58 +01:00
import { Injectable } from '@angular/core';
2022-03-11 22:04:47 +01:00
import { HttpClient, HttpParams } from '@angular/common/http';
import { Subject } from "rxjs";
2021-12-22 23:06:58 +01:00
@Injectable({
providedIn: 'root'
})
export class ApiClientService {
2021-12-24 17:54:17 +01:00
private apiRoot = 'api/';
2022-03-12 20:58:49 +01:00
public lastEtag = "";
public isLastSame = false;
2022-03-11 22:04:47 +01:00
public alertsChanged = new Subject<void>();
2022-03-12 20:58:49 +01:00
public availableUsers: undefined | number = undefined;
2021-12-22 23:06:58 +01:00
private _maintenanceMode = false;
private _maintenanceModeInterval: any = undefined;
public maintenanceModeChanged = new Subject<void>();
get maintenanceMode(): boolean {
return this._maintenanceMode;
}
set maintenanceMode(value: boolean) {
if(value && !this._maintenanceMode) {
//Every 5 seconds, check if maintenance mode is still active
this._maintenanceModeInterval = setInterval(() => {
this.get("ping").then(() => {
console.log("Maintenance mode disabled");
this.maintenanceMode = false;
clearInterval(this._maintenanceModeInterval);
}).catch(() => {});
}, 10000);
}
this._maintenanceMode = value;
this.maintenanceModeChanged.next();
}
private _offline = false;
private _offlineInterval: any = undefined;
public offlineChanged = new Subject<void>();
get offline(): boolean {
return this._offline;
}
set offline(value: boolean) {
if(value && !this._offline) {
//Every 5 seconds, check if sill offline
this._offlineInterval = setInterval(() => {
this.get("ping").then(() => {
console.log("Offline mode disabled");
this.offline = false;
clearInterval(this._offlineInterval);
}).catch(() => {});
}, 10000);
}
this._offline = value;
this.offlineChanged.next();
}
constructor(private http: HttpClient) { }
2021-12-22 23:06:58 +01:00
2024-04-11 00:39:06 +02:00
private returnResponseData(body: any): any {
if(body === null || body === undefined) return null;
if(body.data !== undefined) {
return body.data;
}
return body;
}
2021-12-22 23:06:58 +01:00
public apiEndpoint(endpoint: string): string {
if(endpoint.startsWith('http') || endpoint.startsWith('//')) {
2022-01-07 16:41:09 +01:00
return endpoint;
}
2021-12-22 23:06:58 +01:00
return this.apiRoot + endpoint;
}
public get(endpoint: string, data: any = {}, etag: string = "") {
2023-10-24 00:17:37 +02:00
if(etag === null) etag = "";
2021-12-22 23:06:58 +01:00
return new Promise<any>((resolve, reject) => {
2022-01-07 16:41:09 +01:00
this.http.get(this.apiEndpoint(endpoint), {
params: new HttpParams({ fromObject: data }),
observe: 'response',
2023-10-24 00:17:37 +02:00
headers: etag !== "" ? {
'If-None-Match': etag
} : {}
}).subscribe({
next: (v: any) => {
this.lastEtag = v.headers.get("etag");
2023-10-24 00:17:37 +02:00
this.isLastSame = etag === this.lastEtag && etag !== "";
2024-04-11 00:39:06 +02:00
resolve(
this.returnResponseData(v.body)
);
},
error: (e) => reject(e)
2021-12-22 23:06:58 +01:00
});
});
}
public post(endpoint: string, data: any = {}) {
2021-12-22 23:06:58 +01:00
return new Promise<any>((resolve, reject) => {
2023-02-23 00:25:23 +01:00
this.http.post(this.apiEndpoint(endpoint), data).subscribe({
next: (v) => resolve(v),
error: (e) => reject(e)
2021-12-22 23:06:58 +01:00
});
});
}
public put(endpoint: string, data: any = {}) {
2021-12-22 23:06:58 +01:00
return new Promise<any>((resolve, reject) => {
2023-02-23 00:25:23 +01:00
this.http.put(this.apiEndpoint(endpoint), data).subscribe({
next: (v) => resolve(v),
error: (e) => reject(e)
2021-12-22 23:06:58 +01:00
});
});
}
2023-10-22 14:22:12 +02:00
public patch(endpoint: string, data: any = {}) {
return new Promise<any>((resolve, reject) => {
this.http.patch(this.apiEndpoint(endpoint), data).subscribe({
next: (v) => resolve(v),
error: (e) => reject(e)
});
});
}
2021-12-22 23:06:58 +01:00
public delete(endpoint: string) {
return new Promise<any>((resolve, reject) => {
this.http.delete(this.apiEndpoint(endpoint)).subscribe({
next: (v) => resolve(v),
error: (e) => reject(e)
2021-12-22 23:06:58 +01:00
});
});
}
2022-01-04 00:08:13 +01:00
}