X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=src%2Fservices%2FRestService.ts;h=bea559a7fa94cd4fdd9b8804258cd754efa2a7ce;hb=382448fed3f9d4d4cf9ac614e1275208d18b752f;hp=d7ded6f6a773dab2c6933acbd36a38f458ae9aa9;hpb=3b4814aa2d3dec621dadb52f058ba95a3dc3a86a;p=osm%2FNG-UI.git diff --git a/src/services/RestService.ts b/src/services/RestService.ts index d7ded6f..bea559a 100644 --- a/src/services/RestService.ts +++ b/src/services/RestService.ts @@ -20,12 +20,13 @@ * @file Provider for REST Service */ +import { isNullOrUndefined } from 'util'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; import { TranslateService } from '@ngx-translate/core'; import { NotifierService } from 'angular-notifier'; -import { ERRORDATA } from 'CommonModel'; +import { APIURLHEADER, ERRORDATA } from 'CommonModel'; import * as HttpStatus from 'http-status-codes'; import { Observable } from 'rxjs'; @@ -60,7 +61,12 @@ export class RestService { */ public getResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable<{}> { - return this.http.get(apiURL, httpHeaders); + const getRequest: APIURLHEADER = this.getHttpoptions(apiURL); + if (!isNullOrUndefined(httpHeaders)) { + return this.http.get(apiURL, httpHeaders); + } else { + return this.http.get(getRequest.url, getRequest.httpOptions); + } } /** @@ -70,7 +76,12 @@ export class RestService { */ public postResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<{}> { - return this.http.post(apiURLHeader.url, payload, apiURLHeader.httpOptions); + const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); + if (!isNullOrUndefined(apiURLHeader.httpOptions)) { + return this.http.post(apiURLHeader.url, payload, apiURLHeader.httpOptions); + } else { + return this.http.post(getRequest.url, payload, getRequest.httpOptions); + } } /** @@ -80,7 +91,12 @@ export class RestService { */ public patchResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable { - return this.http.patch(apiURLHeader.url, payload, apiURLHeader.httpOptions); + const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); + if (!isNullOrUndefined(apiURLHeader.httpOptions)) { + return this.http.patch(apiURLHeader.url, payload, apiURLHeader.httpOptions); + } else { + return this.http.patch(getRequest.url, payload, getRequest.httpOptions); + } } /** @@ -90,7 +106,12 @@ export class RestService { */ public putResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable { - return this.http.put(apiURLHeader.url, payload, apiURLHeader.httpOptions); + const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); + if (!isNullOrUndefined(apiURLHeader.httpOptions)) { + return this.http.put(apiURLHeader.url, payload, apiURLHeader.httpOptions); + } else { + return this.http.put(getRequest.url, payload, getRequest.httpOptions); + } } /** @@ -99,14 +120,19 @@ export class RestService { */ public deleteResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable { - return this.http.delete(apiURL, httpHeaders); + const getRequest: APIURLHEADER = this.getHttpoptions(apiURL); + if (!isNullOrUndefined(httpHeaders)) { + return this.http.delete(apiURL, httpHeaders); + } else { + return this.http.delete(getRequest.url, getRequest.httpOptions); + } } + /** * Handle Error response based on the status. * @param error The error response reecieved from API call. * @param method The http request method. */ - // tslint:disable-next-line: cyclomatic-complexity public handleError(err: ERRORDATA, method?: string): void { if (err.error.status === HttpStatus.UNAUTHORIZED) { if (method !== 'get') { @@ -141,4 +167,17 @@ export class RestService { err.error.detail : this.translateService.instant('ERROR')); } } + + /** Set headers for get Methods @public */ + private getHttpoptions(apiURL: string): APIURLHEADER { + const apiHeaders: HttpHeaders = new HttpHeaders({ + 'Content-Type': 'application/json; charset=UTF-8', + Accept: 'application/json', + 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' + }); + return { + url: apiURL, + httpOptions: {headers : apiHeaders} + }; + } }