fix: delete saving states
Parallel use of the same Api Service instance is likely to replace requestData
This commit is contained in:
parent
2871505591
commit
fd5a1cb14f
@ -1,9 +1,5 @@
|
||||
import {HttpHeaders} from "@angular/common/http";
|
||||
|
||||
export interface SetRequestBuilderAfterBuild {
|
||||
setRequestBuilder(request: RequestData): void;
|
||||
}
|
||||
|
||||
export interface RequestData {
|
||||
endpoint: string;
|
||||
queryParams: Record<string, string | number | boolean | Array<any> | null> | null;
|
||||
@ -11,6 +7,7 @@ export interface RequestData {
|
||||
data: any;
|
||||
silenceMode: boolean;
|
||||
withCredentials: boolean;
|
||||
needAuth: boolean;
|
||||
}
|
||||
|
||||
export class RequestBuilder {
|
||||
@ -20,10 +17,8 @@ export class RequestBuilder {
|
||||
private data: any = null;
|
||||
private silenceMode: boolean = false;
|
||||
private withCredentials: boolean = false;
|
||||
private readonly object: any;
|
||||
|
||||
constructor(obj: any) {
|
||||
this.object = obj;
|
||||
constructor() {
|
||||
}
|
||||
|
||||
public setEndpoint(endpoint: string): this {
|
||||
@ -58,16 +53,16 @@ export class RequestBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public build<Type>(): Type {
|
||||
(this.object as SetRequestBuilderAfterBuild).setRequestBuilder({
|
||||
public get build(): RequestData {
|
||||
return {
|
||||
endpoint: this.endpoint,
|
||||
queryParams: this.queryParams,
|
||||
httpHeaders: this.httpHeaders,
|
||||
data: this.data,
|
||||
silenceMode: this.silenceMode,
|
||||
withCredentials: this.withCredentials
|
||||
});
|
||||
return this.object as Type;
|
||||
withCredentials: this.withCredentials,
|
||||
needAuth: false
|
||||
};
|
||||
}
|
||||
|
||||
public getEndpoint(): string {
|
||||
@ -97,16 +92,8 @@ export class RequestBuilder {
|
||||
httpHeaders: new HttpHeaders(),
|
||||
data: null,
|
||||
silenceMode: false,
|
||||
withCredentials: false
|
||||
withCredentials: false,
|
||||
needAuth: false
|
||||
};
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.endpoint = '';
|
||||
this.queryParams = null;
|
||||
this.httpHeaders = new HttpHeaders();
|
||||
this.data = null;
|
||||
this.silenceMode = false;
|
||||
this.withCredentials = false;
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
import {catchError, filter, mergeMap, Observable, retryWhen, switchMap, tap, timer} from "rxjs";
|
||||
import {catchError, filter, mergeMap, Observable, retryWhen, switchMap, timer} from "rxjs";
|
||||
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
||||
import {NotifyColor, OpenNotifyService} from "@service/open-notify.service";
|
||||
import {environment} from "@environment";
|
||||
import {Router} from "@angular/router";
|
||||
import {Injectable} from "@angular/core";
|
||||
import {RequestBuilder, RequestData, SetRequestBuilderAfterBuild} from "@api/RequestBuilder";
|
||||
import {RequestBuilder, RequestData} from "@api/RequestBuilder";
|
||||
import {TokenRefreshService} from "@service/token-refresh.service";
|
||||
import {AuthToken} from "@service/auth.service";
|
||||
|
||||
@ -36,19 +36,16 @@ export enum AvailableVersion {
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export default abstract class ApiService implements SetRequestBuilderAfterBuild {
|
||||
export default abstract class ApiService {
|
||||
constructor(private http: HttpClient, private notify: OpenNotifyService, private router: Router, protected tokenRefreshService: TokenRefreshService) {
|
||||
}
|
||||
|
||||
private apiUrl = environment.apiUrl;
|
||||
|
||||
protected abstract basePath: string;
|
||||
protected abstract version: AvailableVersion;
|
||||
private request: RequestData = RequestBuilder.getStandardRequestData();
|
||||
public static readonly tokenKey = 'auth_token';
|
||||
|
||||
public setRequestBuilder(request: RequestData): void {
|
||||
this.request = request;
|
||||
}
|
||||
public static readonly tokenKey = 'auth_token';
|
||||
|
||||
private static addQuery(endpoint: string, queryParams?: Record<string, string | number | boolean | Array<any> | null> | null): string {
|
||||
const url = new URL(endpoint);
|
||||
@ -72,73 +69,81 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
|
||||
return parts.map(part => part.replace(/(^\/+|\/+$)/g, '')).join('/');
|
||||
}
|
||||
|
||||
protected get combinedUrl() {
|
||||
return ApiService.addQuery(ApiService.combineUrls(this.apiUrl, AvailableVersion[this.version], this.basePath, this.request.endpoint), this.request.queryParams);
|
||||
protected combinedUrl(request: RequestData) {
|
||||
return ApiService.addQuery(ApiService.combineUrls(this.apiUrl, AvailableVersion[this.version], this.basePath, request.endpoint), request.queryParams);
|
||||
}
|
||||
|
||||
private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put'): Observable<Type> {
|
||||
const doneEndpoint = this.combinedUrl;
|
||||
private sendHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put', request: RequestData): Observable<Type> {
|
||||
const doneEndpoint = this.combinedUrl(request);
|
||||
|
||||
return this.tokenRefreshService.getTokenRefreshing$().pipe(
|
||||
filter(isRefreshing => !isRefreshing),
|
||||
switchMap(() =>
|
||||
this.http.request<Type>(method, doneEndpoint, {
|
||||
withCredentials: this.request.withCredentials,
|
||||
headers: this.request.httpHeaders,
|
||||
body: this.request.data
|
||||
}).pipe(
|
||||
tap(_ => this.request = RequestBuilder.getStandardRequestData()),
|
||||
retryWithInterval<Type>(),
|
||||
catchError(error => {
|
||||
if (!this.request.silenceMode)
|
||||
this.handleError(error);
|
||||
return this.http.request<Type>(method, doneEndpoint, {
|
||||
withCredentials: request.withCredentials,
|
||||
headers: request.httpHeaders,
|
||||
body: request.data
|
||||
}).pipe(
|
||||
retryWithInterval<Type>(),
|
||||
catchError(error => {
|
||||
if (!request.silenceMode)
|
||||
this.handleError(error);
|
||||
|
||||
this.request = RequestBuilder.getStandardRequestData();
|
||||
throw error;
|
||||
})
|
||||
)
|
||||
)
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put', request: RequestData): Observable<Type> {
|
||||
|
||||
console.log(request);
|
||||
if (request.needAuth)
|
||||
return this.tokenRefreshService.getTokenRefreshing$().pipe(
|
||||
filter(isRefreshing => !isRefreshing),
|
||||
switchMap(() => {
|
||||
console.log("I'm here!");
|
||||
const token = localStorage.getItem(ApiService.tokenKey);
|
||||
|
||||
if (token) {
|
||||
const authToken = AuthToken.httpHeader((JSON.parse(token) as AuthToken));
|
||||
authToken.keys().forEach(key => request.httpHeaders = request.httpHeaders.append(key, authToken.get(key) ?? ''));
|
||||
}
|
||||
|
||||
return this.sendHttpRequest<Type>(method, request);
|
||||
})
|
||||
);
|
||||
|
||||
return this.sendHttpRequest<Type>(method, request);
|
||||
}
|
||||
|
||||
private getRequest(request: RequestData | string | null): RequestData {
|
||||
if (request === null)
|
||||
return this.createRequestBuilder().build;
|
||||
if (typeof request === 'string')
|
||||
return this.createRequestBuilder().setEndpoint(request as string).build;
|
||||
|
||||
return request as RequestData;
|
||||
}
|
||||
|
||||
public createRequestBuilder() {
|
||||
this.request = RequestBuilder.getStandardRequestData();
|
||||
return new RequestBuilder(this);
|
||||
return new RequestBuilder();
|
||||
}
|
||||
|
||||
public get<Type>(endpoint: string = ''): Observable<Type> {
|
||||
if (endpoint)
|
||||
this.request.endpoint = endpoint;
|
||||
return this.makeHttpRequest<Type>('get');
|
||||
public get<Type>(request: RequestData | string | null = null): Observable<Type> {
|
||||
return this.makeHttpRequest<Type>('get', this.getRequest(request));
|
||||
}
|
||||
|
||||
public post<Type>(endpoint: string = ''): Observable<Type> {
|
||||
if (endpoint)
|
||||
this.request.endpoint = endpoint;
|
||||
return this.makeHttpRequest<Type>('post');
|
||||
public post<Type>(request: RequestData | string | null = null): Observable<Type> {
|
||||
return this.makeHttpRequest<Type>('post', this.getRequest(request));
|
||||
}
|
||||
|
||||
public put<Type>(endpoint: string = ''): Observable<Type> {
|
||||
if (endpoint)
|
||||
this.request.endpoint = endpoint;
|
||||
return this.makeHttpRequest<Type>('put');
|
||||
public put<Type>(request: RequestData | string | null = null): Observable<Type> {
|
||||
return this.makeHttpRequest<Type>('put', this.getRequest(request));
|
||||
}
|
||||
|
||||
public delete<Type>(endpoint: string = ''): Observable<Type> {
|
||||
if (endpoint)
|
||||
this.request.endpoint = endpoint;
|
||||
return this.makeHttpRequest<Type>('delete');
|
||||
public delete<Type>(request: RequestData | string | null = null): Observable<Type> {
|
||||
return this.makeHttpRequest<Type>('delete', this.getRequest(request));
|
||||
}
|
||||
|
||||
public addAuth() {
|
||||
const token = localStorage.getItem(ApiService.tokenKey);
|
||||
|
||||
if (!token)
|
||||
return this;
|
||||
|
||||
const authToken = AuthToken.httpHeader((JSON.parse(token) as AuthToken));
|
||||
authToken.keys().forEach(key => this.request.httpHeaders = this.request.httpHeaders.append(key, authToken.get(key) ?? ''));
|
||||
|
||||
public addAuth(request: RequestData) {
|
||||
request.needAuth = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -12,26 +12,29 @@ export default class AuthApiService extends ApiService {
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public login(login: LoginRequest) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('Login')
|
||||
.setData(login)
|
||||
.build<ApiService>()
|
||||
.post<TokenResponse>()
|
||||
.setWithCredentials()
|
||||
.build;
|
||||
|
||||
return this.post<TokenResponse>(request)
|
||||
.pipe(
|
||||
tap(response => {
|
||||
AuthService.setToken(response, AvailableAuthenticationProvider.Bearer, this.createRequestBuilder().setEndpoint('ReLogin').build<AuthApiService>().combinedUrl);
|
||||
this.tokenRefreshService.startTokenRefresh(response.expiresIn);
|
||||
AuthService.setToken(response, AvailableAuthenticationProvider.Bearer, this.combinedUrl(this.createRequestBuilder().setEndpoint('ReLogin').build));
|
||||
this.tokenRefreshService.setRefreshTokenExpireMs(response.expiresIn);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public logout() {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setWithCredentials()
|
||||
.setEndpoint('Logout')
|
||||
.build<ApiService>()
|
||||
.addAuth()
|
||||
.get()
|
||||
.build;
|
||||
|
||||
return this.addAuth(request)
|
||||
.get(request)
|
||||
.pipe(
|
||||
tap(_ => {
|
||||
localStorage.removeItem(ApiService.tokenKey);
|
||||
@ -40,11 +43,13 @@ export default class AuthApiService extends ApiService {
|
||||
}
|
||||
|
||||
public getRole(isSilence: boolean = true) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setSilenceMode(isSilence)
|
||||
.build<ApiService>()
|
||||
.addAuth()
|
||||
.get<AuthRoles>('GetRole')
|
||||
.setEndpoint('GetRole')
|
||||
.build;
|
||||
|
||||
return this.addAuth(request)
|
||||
.get<AuthRoles>(request)
|
||||
.pipe(
|
||||
catchError(_ => {
|
||||
return of(null);
|
||||
|
@ -8,10 +8,11 @@ export class DisciplineService extends ApiService {
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getDisciplines(page: number | null = null, pageSize: number | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setQueryParams({page: page, pageSize: pageSize})
|
||||
.build<ApiService>()
|
||||
.get<DisciplineResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.get<DisciplineResponse[]>(request);
|
||||
}
|
||||
|
||||
public getById(id: number) {
|
||||
|
@ -9,10 +9,11 @@ export class FacultyService extends ApiService {
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getFaculties(page: number | null = null, pageSize: number | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setQueryParams({page: page, pageSize: pageSize})
|
||||
.build<ApiService>()
|
||||
.get<FacultyResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.get<FacultyResponse[]>(request);
|
||||
}
|
||||
|
||||
public getById(id: number) {
|
||||
|
@ -9,10 +9,11 @@ export class GroupService extends ApiService {
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getGroups(page: number | null = null, pageSize: number | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setQueryParams({page: page, pageSize: pageSize})
|
||||
.build<ApiService>()
|
||||
.get<GroupResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.get<GroupResponse[]>(request);
|
||||
}
|
||||
|
||||
public getById(id: number) {
|
||||
|
@ -8,10 +8,11 @@ export class ProfessorService extends ApiService {
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getProfessors(page: number | null = null, pageSize: number | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setQueryParams({page: page, pageSize: pageSize})
|
||||
.build<ApiService>()
|
||||
.get<ProfessorResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.get<ProfessorResponse[]>(request);
|
||||
}
|
||||
|
||||
public getById(id: number) {
|
||||
|
@ -20,41 +20,46 @@ export class ScheduleService extends ApiService {
|
||||
}
|
||||
|
||||
public postSchedule(data: ScheduleRequest) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<ScheduleResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.post<ScheduleResponse[]>(request);
|
||||
}
|
||||
|
||||
public getByGroup(id: number, isEven: boolean | null = null, disciplines: Array<number> | null = null, professors: Array<number> | null = null, lectureHalls: Array<number> | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('GetByGroup/' + id.toString())
|
||||
.setQueryParams({isEven: isEven, disciplines: disciplines, professors: professors, lectureHalls: lectureHalls})
|
||||
.build<ApiService>()
|
||||
.get<ScheduleResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.get<ScheduleResponse[]>(request);
|
||||
}
|
||||
|
||||
public getByProfessor(id: number, isEven: boolean | null = null, disciplines: Array<number> | null = null, groups: Array<number> | null = null, lectureHalls: Array<number> | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('GetByProfessor/' + id.toString())
|
||||
.setQueryParams({isEven: isEven, disciplines: disciplines, groups: groups, lectureHalls: lectureHalls})
|
||||
.build<ApiService>()
|
||||
.get<ScheduleResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.get<ScheduleResponse[]>(request);
|
||||
}
|
||||
|
||||
public getByLectureHall(id: number, isEven: boolean | null = null, disciplines: Array<number> | null = null, groups: Array<number> | null = null, professors: Array<number> | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('GetByLectureHall/' + id.toString())
|
||||
.setQueryParams({isEven: isEven, disciplines: disciplines, groups: groups, professors: professors})
|
||||
.build<ApiService>()
|
||||
.get<ScheduleResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.get<ScheduleResponse[]>(request);
|
||||
}
|
||||
|
||||
public getByDiscipline(id: number, isEven: boolean | null = null, groups: Array<number> | null = null, professors: Array<number> | null = null, lectureHalls: Array<number> | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('GetByDiscipline/' + id.toString())
|
||||
.setQueryParams({isEven: isEven, groups: groups, professors: professors, lectureHalls: lectureHalls})
|
||||
.build<ApiService>()
|
||||
.get<ScheduleResponse[]>();
|
||||
.build;
|
||||
|
||||
return this.get<ScheduleResponse[]>(request);
|
||||
}
|
||||
}
|
||||
|
@ -14,101 +14,112 @@ export default class SetupService extends ApiService {
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public checkToken(token: string) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('CheckToken')
|
||||
.setQueryParams({token: token})
|
||||
.build<ApiService>()
|
||||
.get<boolean>();
|
||||
.build;
|
||||
|
||||
return this.get<boolean>(request);
|
||||
}
|
||||
|
||||
public setPsql(data: DatabaseRequest) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('SetPsql')
|
||||
.setData(data)
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public setMysql(data: DatabaseRequest) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('SetMysql')
|
||||
.setData(data)
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public setSqlite(path: string | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('SetSqlite')
|
||||
.setQueryParams({path: path})
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.get<boolean>();
|
||||
.build;
|
||||
|
||||
return this.get<boolean>(request);
|
||||
}
|
||||
|
||||
public setRedis(data: CacheRequest) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('SetRedis')
|
||||
.setData(data)
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public setMemcached() {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('SetMemcached')
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public createAdmin(data: CreateUserRequest) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('CreateAdmin')
|
||||
.setData(data)
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public setLogging(data: LoggingRequest | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('SetLogging')
|
||||
.setData(data)
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public setEmail(data: EmailRequest | null = null) {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('SetEmail')
|
||||
.setData(data)
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public setSchedule(data: ScheduleConfigurationRequest) {
|
||||
data.startTerm = new DateOnly(data.startTerm).toString();
|
||||
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('SetSchedule')
|
||||
.setData(data)
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public submit() {
|
||||
return this.createRequestBuilder()
|
||||
let request = this.createRequestBuilder()
|
||||
.setEndpoint('Submit')
|
||||
.setWithCredentials()
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
.build;
|
||||
|
||||
return this.post<boolean>(request);
|
||||
}
|
||||
|
||||
public isConfigured() {
|
||||
|
Loading…
Reference in New Issue
Block a user