refactor: implement RequestBuilder for main request

This commit is contained in:
Polianin Nikita 2024-06-27 01:50:58 +03:00
parent e36376db3a
commit 8e738d9b3d
9 changed files with 151 additions and 75 deletions

View File

@ -1,9 +1,10 @@
import {catchError, mergeMap, Observable, retryWhen, timer} from "rxjs";
import {catchError, mergeMap, Observable, retryWhen, tap, 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";
export function retryWithInterval<T>(): (source: Observable<T>) => Observable<T> {
return (source: Observable<T>) =>
@ -28,24 +29,23 @@ export function retryWithInterval<T>(): (source: Observable<T>) => Observable<T>
);
}
/*
@Injectable({
providedIn: 'root'
})
*/
export enum AvailableVersion {
v1
}
@Injectable()
export default abstract class ApiService {
export default abstract class ApiService implements SetRequestBuilderAfterBuild {
constructor(private http: HttpClient, private notify: OpenNotifyService, private router: Router) {
}
private urlApi = environment.apiUrl;
protected abstract basePath: string;
protected abstract version: AvailableVersion;
private request: RequestData = RequestBuilder.getStandardRequestData();
public setRequestBuilder(request: RequestData): void {
this.request = request;
}
private static addQuery(endpoint: string, queryParams?: Record<string, string | number | boolean | Array<any> | null> | null): string {
const url = new URL(endpoint);
@ -54,10 +54,9 @@ export default abstract class ApiService {
Object.keys(queryParams).forEach(key => {
const value = queryParams[key];
if (value !== null && value !== undefined) {
if (typeof(value) === typeof(Array)) {
if (typeof (value) === typeof (Array)) {
(value as Array<any>).forEach(x => url.searchParams.append(key, x.toString()));
}
else
} else
url.searchParams.append(key, value.toString());
}
});
@ -72,50 +71,59 @@ export default abstract class ApiService {
return test;
}
public get<Type>(endpoint: string = '', queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
return this.http.get<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), {withCredentials: true}).pipe(
private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put'): Observable<Type> {
const doneEndpoint = ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, this.request.endpoint), this.request.queryParams);
return this.http.request<Type>(method, doneEndpoint, {
withCredentials: true,
headers: this.request.httpHeaders,
body: this.request.data
}).pipe(
tap(_ => this.request = RequestBuilder.getStandardRequestData()),
retryWithInterval<Type>(),
catchError(error => {
this.handleError(error);
if (!this.request.silenceMode)
this.handleError(error);
this.request = RequestBuilder.getStandardRequestData();
throw error;
})
})
);
}
public post<Type>(endpoint: string, data: any, queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
return this.http.post<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), data, {withCredentials: true}).pipe(
retryWithInterval<Type>(),
catchError(error => {
this.handleError(error);
throw error;
})
);
public createRequestBuilder() {
this.request = RequestBuilder.getStandardRequestData();
return new RequestBuilder(this);
}
public put<Type>(endpoint: string, data: any, queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
return this.http.put<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), data, {withCredentials: true}).pipe(
retryWithInterval<Type>(),
catchError(error => {
this.handleError(error);
throw error;
})
);
public get<Type>(endpoint: string = ''): Observable<Type> {
if (endpoint)
this.request.endpoint = endpoint;
return this.makeHttpRequest<Type>('get');
}
public delete<Type>(endpoint: string): Observable<Type> {
return this.http.delete<Type>(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), {withCredentials: true}).pipe(
retryWithInterval<Type>(),
catchError(error => {
this.handleError(error);
throw error;
})
);
public post<Type>(endpoint: string = ''): Observable<Type> {
if (endpoint)
this.request.endpoint = endpoint;
return this.makeHttpRequest<Type>('post');
}
public put<Type>(endpoint: string = ''): Observable<Type> {
if (endpoint)
this.request.endpoint = endpoint;
return this.makeHttpRequest<Type>('put');
}
public delete<Type>(endpoint: string = ''): Observable<Type> {
if (endpoint)
this.request.endpoint = endpoint;
return this.makeHttpRequest<Type>('delete');
}
private handleError(error: HttpErrorResponse): void {
// todo: change to Retry-After condition
if (error.error.toString().includes("setup")) {
this.router.navigate(['/setup/']);
this.router.navigate(['/setup/']).then();
return;
}

View File

@ -5,8 +5,8 @@ import {CampusDetailsResponse} from "@api/v1/campusDetailsResponse";
@Injectable()
export class CampusService extends ApiService {
protected basePath = 'Campus/';
protected version = AvailableVersion.v1;
public readonly basePath = 'Campus/';
public readonly version = AvailableVersion.v1;
public getCampus() {
return this.get<CampusBasicInfoResponse[]>();

View File

@ -4,11 +4,14 @@ import {DisciplineResponse} from "@api/v1/disciplineResponse";
@Injectable()
export class DisciplineService extends ApiService {
protected basePath = 'Discipline/';
protected version = AvailableVersion.v1;
public readonly basePath = 'Discipline/';
public readonly version = AvailableVersion.v1;
public getDisciplines(page: number | null = null, pageSize: number | null = null) {
return this.get<DisciplineResponse[]>('', {page: page, pageSize: pageSize});
return this.createRequestBuilder()
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<DisciplineResponse[]>();
}
public getById(id: number) {

View File

@ -5,11 +5,14 @@ import {FacultyDetailsResponse} from "@api/v1/facultyDetailsResponse";
@Injectable()
export class FacultyService extends ApiService {
protected basePath = 'Faculty/';
protected version = AvailableVersion.v1;
public readonly basePath = 'Faculty/';
public readonly version = AvailableVersion.v1;
public getFaculties(page: number | null = null, pageSize: number | null = null) {
return this.get<FacultyResponse[]>('', {page: page, pageSize: pageSize});
return this.createRequestBuilder()
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<FacultyResponse[]>();
}

View File

@ -5,11 +5,14 @@ import {GroupDetailsResponse} from "@api/v1/groupDetailsResponse";
@Injectable()
export class GroupService extends ApiService {
protected basePath = 'Group/';
protected version = AvailableVersion.v1;
public readonly basePath = 'Group/';
public readonly version = AvailableVersion.v1;
public getGroups(page: number | null = null, pageSize: number | null = null) {
return this.get<GroupResponse[]>('', {page: page, pageSize: pageSize});
return this.createRequestBuilder()
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<GroupResponse[]>();
}
public getById(id: number) {

View File

@ -5,8 +5,8 @@ import {LectureHallDetailsResponse} from "@api/v1/lectureHallDetailsResponse";
@Injectable()
export class LectureHallService extends ApiService {
protected basePath = 'LectureHall/';
protected version = AvailableVersion.v1;
public readonly basePath = 'LectureHall/';
public readonly version = AvailableVersion.v1;
public getLectureHalls() {
return this.get<LectureHallResponse[]>();

View File

@ -4,11 +4,14 @@ import {ProfessorResponse} from "@api/v1/professorResponse";
@Injectable()
export class ProfessorService extends ApiService {
protected basePath = 'Professor/';
protected version = AvailableVersion.v1;
public readonly basePath = 'Professor/';
public readonly version = AvailableVersion.v1;
public getProfessors(page: number | null = null, pageSize: number | null = null) {
return this.get<ProfessorResponse[]>('', {page: page, pageSize: pageSize});
return this.createRequestBuilder()
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<ProfessorResponse[]>();
}
public getById(id: number) {

View File

@ -8,8 +8,8 @@ import {map} from "rxjs";
@Injectable()
export class ScheduleService extends ApiService {
protected basePath = 'Schedule/';
protected version = AvailableVersion.v1;
public readonly basePath = 'Schedule/';
public readonly version = AvailableVersion.v1;
public startTerm() {
return this.get<string>('StartTerm').pipe(map(date => new DateOnly(date)));
@ -20,22 +20,41 @@ export class ScheduleService extends ApiService {
}
public postSchedule(data: ScheduleRequest) {
return this.post<ScheduleResponse[]>('', data);
return this.createRequestBuilder()
.setData(data)
.build<ApiService>()
.post<ScheduleResponse[]>();
}
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.get<ScheduleResponse[]>('GetByGroup/' + id.toString(), {isEven: isEven, disciplines: disciplines, professors: professors, lectureHalls: lectureHalls});
return this.createRequestBuilder()
.setEndpoint('GetByGroup/' + id.toString())
.setQueryParams({isEven: isEven, disciplines: disciplines, professors: professors, lectureHalls: lectureHalls})
.build<ApiService>()
.get<ScheduleResponse[]>();
}
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.get<ScheduleResponse[]>('GetByProfessor/' + id.toString(), {isEven: isEven, disciplines: disciplines, groups: groups, lectureHalls: lectureHalls});
return this.createRequestBuilder()
.setEndpoint('GetByProfessor/' + id.toString())
.setQueryParams({isEven: isEven, disciplines: disciplines, groups: groups, lectureHalls: lectureHalls})
.build<ApiService>()
.get<ScheduleResponse[]>();
}
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.get<ScheduleResponse[]>('GetByLectureHall/' + id.toString(), {isEven: isEven, disciplines: disciplines, groups: groups, professors: professors});
return this.createRequestBuilder()
.setEndpoint('GetByLectureHall/' + id.toString())
.setQueryParams({isEven: isEven, disciplines: disciplines, groups: groups, professors: professors})
.build<ApiService>()
.get<ScheduleResponse[]>();
}
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.get<ScheduleResponse[]>('GetByDiscipline/' + id.toString(), {isEven: isEven, groups: groups, professors: professors, lectureHalls: lectureHalls});
return this.createRequestBuilder()
.setEndpoint('GetByDiscipline/' + id.toString())
.setQueryParams({isEven: isEven, groups: groups, professors: professors, lectureHalls: lectureHalls})
.build<ApiService>()
.get<ScheduleResponse[]>();
}
}

View File

@ -10,51 +10,88 @@ import {DateOnly} from "@model/DateOnly";
@Injectable()
export default class SetupService extends ApiService {
protected basePath = 'Setup/';
public readonly basePath = 'Setup/';
public readonly version = AvailableVersion.v1;
public checkToken(token: string) {
return this.get<boolean>('CheckToken', {token: token});
return this.createRequestBuilder()
.setEndpoint('CheckToken')
.setQueryParams({token: token})
.build<ApiService>()
.get<boolean>();
}
public setPsql(data: DatabaseRequest) {
return this.post<boolean>('SetPsql', data);
return this.createRequestBuilder()
.setEndpoint('SetPsql')
.setData(data)
.build<ApiService>()
.post<boolean>();
}
public setMysql(data: DatabaseRequest) {
return this.post<boolean>('SetMysql', data);
return this.createRequestBuilder()
.setEndpoint('SetMysql')
.setData(data)
.build<ApiService>()
.post<boolean>();
}
public setSqlite(path: string | null = null) {
return this.post<boolean>('SetSqlite', null, {path: path});
return this.createRequestBuilder()
.setEndpoint('SetSqlite')
.setQueryParams({path: path})
.build<ApiService>()
.get<boolean>();
}
public setRedis(data: CacheRequest) {
return this.post<boolean>('SetRedis', data);
return this.createRequestBuilder()
.setEndpoint('SetRedis')
.setData(data)
.build<ApiService>()
.post<boolean>();
}
public setMemcached() {
return this.post<boolean>('SetMemcached', null);
return this.post<boolean>('SetMemcached');
}
public createAdmin(data: CreateUserRequest) {
return this.post<boolean>('CreateAdmin', data);
return this.createRequestBuilder()
.setEndpoint('CreateAdmin')
.setData(data)
.build<ApiService>()
.post<boolean>();
}
public setLogging(data: LoggingRequest | null = null) {
return this.post<boolean>('SetLogging', data);
return this.createRequestBuilder()
.setEndpoint('SetLogging')
.setData(data)
.build<ApiService>()
.post<boolean>();
}
public setEmail(data: EmailRequest | null = null) {
return this.post<boolean>('SetEmail', data);
return this.createRequestBuilder()
.setEndpoint('SetEmail')
.setData(data)
.build<ApiService>()
.post<boolean>();
}
public setSchedule(data: ScheduleConfigurationRequest) {
data.startTerm = new DateOnly(data.startTerm).toString();
return this.post<boolean>('SetSchedule', data);
return this.createRequestBuilder()
.setEndpoint('SetSchedule')
.setData(data)
.build<ApiService>()
.post<boolean>();
}
public submit() {
return this.post<boolean>('Submit', null);
return this.post<boolean>('Submit');
}
}