Fix Bug 2048:The VCA Status for an NS with both a KNF and a VNF does not provide...
[osm/NG-UI.git] / src / services / RestService.ts
index d7ded6f..e333627 100644 (file)
@@ -25,7 +25,7 @@ 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';
 
@@ -40,6 +40,9 @@ export class RestService {
     private http: HttpClient;
     /** API URL. Disabled tslint since server doesn't support https protocol */
     private apiURL: string = '';
+    /** Method to handle null or undefined @private */
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
+    private isNullOrUndefined = (data: any): boolean => data === null || data === undefined;
     /** Notifier service to popup notification @private */
     private notifierService: NotifierService;
     /** Instance for active modal service @public */
@@ -60,7 +63,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 (!this.isNullOrUndefined(httpHeaders)) {
+            return this.http.get(apiURL, httpHeaders);
+        } else {
+            return this.http.get(getRequest.url, getRequest.httpOptions);
+        }
     }
 
     /**
@@ -70,7 +78,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 (!this.isNullOrUndefined(apiURLHeader.httpOptions)) {
+            return this.http.post(apiURLHeader.url, payload, apiURLHeader.httpOptions);
+        } else {
+            return this.http.post(getRequest.url, payload, getRequest.httpOptions);
+        }
     }
 
     /**
@@ -80,7 +93,12 @@ export class RestService {
      */
 
     public patchResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<object> {
-        return this.http.patch(apiURLHeader.url, payload, apiURLHeader.httpOptions);
+        const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url);
+        if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) {
+            return this.http.patch(apiURLHeader.url, payload, apiURLHeader.httpOptions);
+        } else {
+            return this.http.patch(getRequest.url, payload, getRequest.httpOptions);
+        }
     }
 
     /**
@@ -90,7 +108,12 @@ export class RestService {
      */
 
     public putResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<object> {
-        return this.http.put(apiURLHeader.url, payload, apiURLHeader.httpOptions);
+        const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url);
+        if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) {
+            return this.http.put(apiURLHeader.url, payload, apiURLHeader.httpOptions);
+        } else {
+            return this.http.put(getRequest.url, payload, getRequest.httpOptions);
+        }
     }
 
     /**
@@ -99,14 +122,19 @@ export class RestService {
      */
 
     public deleteResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable<object> {
-        return this.http.delete(apiURL, httpHeaders);
+        const getRequest: APIURLHEADER = this.getHttpoptions(apiURL);
+        if (!this.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') {
@@ -115,8 +143,11 @@ export class RestService {
                     this.notifierService.notify('error', err.error.detail !== undefined ?
                         err.error.detail : this.translateService.instant('HTTPERROR.401'));
                 }
-                this.activeModal.dismissAll();
+            } else {
+                this.notifierService.notify('error', err?.error?.detail !== undefined ?
+                    err?.error?.detail : this.translateService.instant('HTTPERROR.401'));
             }
+            this.activeModal.dismissAll();
         } else if (err.error.status === HttpStatus.BAD_REQUEST) {
             this.notifierService.notify('error', err.error.detail !== undefined ?
                 err.error.detail : this.translateService.instant('HTTPERROR.400'));
@@ -126,6 +157,9 @@ export class RestService {
         } else if (err.error.status === HttpStatus.CONFLICT) {
             this.notifierService.notify('error', err.error.detail !== undefined ?
                 err.error.detail : this.translateService.instant('HTTPERROR.409'));
+            if (sessionStorage.getItem('usertype') !== 'change_password') {
+                this.activeModal.dismissAll();
+            }
             this.activeModal.dismissAll();
         } else if (err.error.status === HttpStatus.INTERNAL_SERVER_ERROR) {
             this.notifierService.notify('error', err.error.detail !== undefined ?
@@ -141,4 +175,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 }
+        };
+    }
 }