ekangularbasetest3
Version:
Authentication service for usermanagement(oidc client)
114 lines (86 loc) • 3.78 kB
text/typescript
import { Component, Inject, OnInit, Injectable } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';
import { Headers } from '@angular/http';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
// import { Hero } from './hero';
import { HttpErrorHandler, HandleError } from '../auth/http-error-handler.service';
import { AuthService } from '../auth/auth.service';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { RequireInteractiveControl } from '../interface/Interactive Component/RequireInteractiveControl';
export class BaseService<T extends RequireInteractiveControl> {
// config: Config;
public handleError: HandleError;
public mode: string;
public baseu: string;
public generalhttp: HttpClient;
constructor(
public authService: AuthService,
public httpErrorHandler: HttpErrorHandler,
public http: HttpClient,
public baseUrl: string
, public objectname: string, public subrouteurl: string = "") {
this.handleError = httpErrorHandler.createHandleError('BaseService');
this.baseu = baseUrl;
this.generalhttp = http;
}
httpHeaders = new HttpHeaders()
.set('Content-Type', 'application/json')
.set('Authorization', this.authService.getAuthorizationHeaderValue());
GetHeaders(): HttpHeaders {
var h = this.httpHeaders.append('OC', this.authService.getSelectedOc());
return h;
}
optionss = {
headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': this.authService.getAuthorizationHeaderValue() }),
params: new HttpParams()
};
Get(): Observable<T[]> {
if (this.subrouteurl == "") {
return this.generalhttp.get<T[]>(this.baseu + 'api/' + this.objectname, this.optionss)
.pipe(
catchError(this.handleError('GetBaseService', []))
);
}
else {
return this.generalhttp.get<T[]>(this.baseu + 'api/' + this.subrouteurl + this.objectname, this.optionss)
.pipe(
catchError(this.handleError('GetBaseService', []))
);
}
}
GetById(id: any): Observable<T> {
if (this.subrouteurl == "") {
return this.generalhttp.get<T>(this.baseu + 'api/' + this.objectname + '/' + id, this.optionss)
.pipe(
catchError(this.handleError('GetBaseService', null))
);
}
else {
return this.generalhttp.get<T>(this.baseu + 'api/' + this.subrouteurl + this.objectname + '/' + id, this.optionss)
.pipe(
catchError(this.handleError('GetBaseService', null))
);
}
}
Create(object: T): Observable<T> {
// console.log(object);
return this.generalhttp.post<T>(this.baseu + 'api/' + this.objectname,
object,
this.optionss).pipe(
catchError(this.handleError('CreateBaseService', null))
);
}
Update(object: T) {
return this.generalhttp.put<T>(this.baseu + 'api/' + this.objectname + "/" + object.id, object, this.optionss).pipe(
catchError(this.handleError('UpdateBaseService', null))
);
}
Delete(object: T) {
return this.generalhttp.delete(this.baseu + 'api/' + this.objectname + "/" + object.id, this.optionss).pipe(
catchError(this.handleError('DeleteBaseService', []))
);
}
}