Initial Commit - NG UI
[osm/NG-UI.git] / src / services / RestService.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
4  Licensed under the Apache License, Version 2.0 (the 'License');
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17  */
18
19 /**
20  * @file Provider for REST Service
21  */
22
23 import { HttpClient, HttpHeaders } from '@angular/common/http';
24 import { Injectable } from '@angular/core';
25 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
26 import { TranslateService } from '@ngx-translate/core';
27 import { NotifierService } from 'angular-notifier';
28 import { ERRORDATA } from 'CommonModel';
29 import * as HttpStatus from 'http-status-codes';
30 import { Observable } from 'rxjs';
31
32 /**
33  * An Injectable is a class adorned with the @Injectable decorator function.
34  * @Injectable takes a metadata object that tells Angular how to compile and run module code
35  */
36 @Injectable()
37 /** Exporting a class @exports RestService */
38 export class RestService {
39     /** convenient way to modify request made by the http service both before they are sent and after they return */
40     private http: HttpClient;
41     /** API URL. Disabled tslint since server doesn't support https protocol */
42     private apiURL: string = '';
43     /** Notifier service to popup notification @private */
44     private notifierService: NotifierService;
45     /** Instance for active modal service @public */
46     private activeModal: NgbModal;
47     /** insatnce for translate @private */
48     private translateService: TranslateService;
49
50     constructor(http: HttpClient, notifierService: NotifierService, activeModal: NgbModal, translateService: TranslateService) {
51         this.http = http;
52         this.notifierService = notifierService;
53         this.activeModal = activeModal;
54         this.translateService = translateService;
55     }
56
57     /**
58      * Get a resource from the server which identified by a URI.
59      * @param apiURL The URL of the resource to be retrieved.
60      */
61
62     public getResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable<{}> {
63         return this.http.get(apiURL, httpHeaders);
64     }
65
66     /**
67      * Create a new resource on the server.
68      * @param apiURL The URL of the resource to be created.
69      * @param payload The request data to be sent to server.
70      */
71
72     public postResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<{}> {
73         return this.http.post(apiURLHeader.url, payload, apiURLHeader.httpOptions);
74     }
75
76     /**
77      * Modify the resource on the server.
78      * @param apiURL The URL of the resource to be created.
79      * @param payload The request data to be sent to server.
80      */
81
82     public patchResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<object> {
83         return this.http.patch(apiURLHeader.url, payload, apiURLHeader.httpOptions);
84     }
85
86     /**
87      * Replace the resource on the server.
88      * @param apiName The URL of the resource to be created.
89      * @param payload The request data to be sent to server.
90      */
91
92     public putResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<object> {
93         return this.http.put(apiURLHeader.url, payload, apiURLHeader.httpOptions);
94     }
95
96     /**
97      * Delete a resource identified by a URL.
98      * @param apiURL The URL of the resource to be deleted.
99      */
100
101     public deleteResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable<object> {
102         return this.http.delete(apiURL, httpHeaders);
103     }
104     /**
105      * Handle Error response based on the status.
106      * @param error The error response reecieved from API call.
107      * @param method The http request method.
108      */
109     // tslint:disable-next-line: cyclomatic-complexity
110     public handleError(err: ERRORDATA, method?: string): void {
111         if (err.error.status === HttpStatus.UNAUTHORIZED) {
112             if (method !== 'get') {
113                 if (err.error.detail !== 'Expired Token or Authorization HTTP header' &&
114                     err.error.detail !== 'Invalid Token or Authorization HTTP header') {
115                     this.notifierService.notify('error', err.error.detail !== undefined ?
116                         err.error.detail : this.translateService.instant('HTTPERROR.401'));
117                 }
118                 this.activeModal.dismissAll();
119             }
120         } else if (err.error.status === HttpStatus.BAD_REQUEST) {
121             this.notifierService.notify('error', err.error.detail !== undefined ?
122                 err.error.detail : this.translateService.instant('HTTPERROR.400'));
123         } else if (err.error.status === HttpStatus.NOT_FOUND) {
124             this.notifierService.notify('error', err.error.detail !== undefined ?
125                 err.error.detail : this.translateService.instant('HTTPERROR.404'));
126         } else if (err.error.status === HttpStatus.CONFLICT) {
127             this.notifierService.notify('error', err.error.detail !== undefined ?
128                 err.error.detail : this.translateService.instant('HTTPERROR.409'));
129             this.activeModal.dismissAll();
130         } else if (err.error.status === HttpStatus.INTERNAL_SERVER_ERROR) {
131             this.notifierService.notify('error', err.error.detail !== undefined ?
132                 err.error.detail : this.translateService.instant('HTTPERROR.500'));
133         } else if (err.error.status === HttpStatus.BAD_GATEWAY) {
134             this.notifierService.notify('error', this.translateService.instant('HTTPERROR.502'));
135         } else if (err.error.status === HttpStatus.SERVICE_UNAVAILABLE) {
136             this.notifierService.notify('error', this.translateService.instant('HTTPERROR.503'));
137         } else if (err.error.status === HttpStatus.GATEWAY_TIMEOUT) {
138             this.notifierService.notify('error', this.translateService.instant('HTTPERROR.504'));
139         } else {
140             this.notifierService.notify('error', err.error.detail !== undefined ?
141                 err.error.detail : this.translateService.instant('ERROR'));
142         }
143     }
144 }