| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 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'; |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 28 | import { APIURLHEADER, ERRORDATA } from 'CommonModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 29 | import * as HttpStatus from 'http-status-codes'; |
| 30 | import { Observable } from 'rxjs'; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 31 | import { isNullOrUndefined } from 'SharedService'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * An Injectable is a class adorned with the @Injectable decorator function. |
| 35 | * @Injectable takes a metadata object that tells Angular how to compile and run module code |
| 36 | */ |
| 37 | @Injectable() |
| 38 | /** Exporting a class @exports RestService */ |
| 39 | export class RestService { |
| 40 | /** convenient way to modify request made by the http service both before they are sent and after they return */ |
| 41 | private http: HttpClient; |
| 42 | /** API URL. Disabled tslint since server doesn't support https protocol */ |
| 43 | private apiURL: string = ''; |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame] | 44 | /** Method to handle null or undefined @private */ |
| 45 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 46 | private isNullOrUndefined = (data: any): boolean => data === null || data === undefined; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 47 | /** Notifier service to popup notification @private */ |
| 48 | private notifierService: NotifierService; |
| 49 | /** Instance for active modal service @public */ |
| 50 | private activeModal: NgbModal; |
| 51 | /** insatnce for translate @private */ |
| 52 | private translateService: TranslateService; |
| 53 | |
| 54 | constructor(http: HttpClient, notifierService: NotifierService, activeModal: NgbModal, translateService: TranslateService) { |
| 55 | this.http = http; |
| 56 | this.notifierService = notifierService; |
| 57 | this.activeModal = activeModal; |
| 58 | this.translateService = translateService; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get a resource from the server which identified by a URI. |
| 63 | * @param apiURL The URL of the resource to be retrieved. |
| 64 | */ |
| 65 | |
| 66 | public getResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable<{}> { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 67 | const getRequest: APIURLHEADER = this.getHttpoptions(apiURL); |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame] | 68 | if (!this.isNullOrUndefined(httpHeaders)) { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 69 | return this.http.get(apiURL, httpHeaders); |
| 70 | } else { |
| 71 | return this.http.get(getRequest.url, getRequest.httpOptions); |
| 72 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Create a new resource on the server. |
| 77 | * @param apiURL The URL of the resource to be created. |
| 78 | * @param payload The request data to be sent to server. |
| 79 | */ |
| 80 | |
| 81 | public postResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<{}> { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 82 | const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame] | 83 | if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 84 | return this.http.post(apiURLHeader.url, payload, apiURLHeader.httpOptions); |
| 85 | } else { |
| 86 | return this.http.post(getRequest.url, payload, getRequest.httpOptions); |
| 87 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Modify the resource on the server. |
| 92 | * @param apiURL The URL of the resource to be created. |
| 93 | * @param payload The request data to be sent to server. |
| 94 | */ |
| 95 | |
| 96 | public patchResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<object> { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 97 | const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame] | 98 | if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 99 | return this.http.patch(apiURLHeader.url, payload, apiURLHeader.httpOptions); |
| 100 | } else { |
| 101 | return this.http.patch(getRequest.url, payload, getRequest.httpOptions); |
| 102 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Replace the resource on the server. |
| 107 | * @param apiName The URL of the resource to be created. |
| 108 | * @param payload The request data to be sent to server. |
| 109 | */ |
| 110 | |
| 111 | public putResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<object> { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 112 | const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame] | 113 | if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 114 | return this.http.put(apiURLHeader.url, payload, apiURLHeader.httpOptions); |
| 115 | } else { |
| 116 | return this.http.put(getRequest.url, payload, getRequest.httpOptions); |
| 117 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Delete a resource identified by a URL. |
| 122 | * @param apiURL The URL of the resource to be deleted. |
| 123 | */ |
| 124 | |
| 125 | public deleteResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable<object> { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 126 | const getRequest: APIURLHEADER = this.getHttpoptions(apiURL); |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame] | 127 | if (!this.isNullOrUndefined(httpHeaders)) { |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 128 | return this.http.delete(apiURL, httpHeaders); |
| 129 | } else { |
| 130 | return this.http.delete(getRequest.url, getRequest.httpOptions); |
| 131 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 132 | } |
| Barath Kumar R | 5d75d51 | 2020-09-02 17:00:07 +0530 | [diff] [blame] | 133 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 134 | /** |
| 135 | * Handle Error response based on the status. |
| 136 | * @param error The error response reecieved from API call. |
| 137 | * @param method The http request method. |
| 138 | */ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 139 | public handleError(err: ERRORDATA, method?: string): void { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 140 | const errStatus = { |
| 141 | 400: this.translateService.instant('HTTPERROR.400'), |
| 142 | 401: this.translateService.instant('HTTPERROR.401'), |
| 143 | 404: this.translateService.instant('HTTPERROR.404'), |
| 144 | 409: this.translateService.instant('HTTPERROR.409'), |
| 145 | 500: this.translateService.instant('HTTPERROR.500'), |
| 146 | 502: this.translateService.instant('HTTPERROR.502'), |
| 147 | 503: this.translateService.instant('HTTPERROR.503'), |
| 148 | 504: this.translateService.instant('HTTPERROR.504') |
| 149 | }; |
| 150 | if (!this.isNullOrUndefined(err)) { |
| 151 | if (err.error.status === HttpStatus.UNAUTHORIZED) { |
| 152 | if (!this.isNullOrUndefined(method)) { |
| 153 | if (method !== 'get') { |
| 154 | if (err.error.detail !== 'Expired Token or Authorization HTTP header' && |
| 155 | err.error.detail !== 'Invalid Token or Authorization HTTP header') { |
| 156 | this.notifierService.notify('error', err.error.detail !== undefined ? |
| 157 | err.error.detail : this.translateService.instant('HTTPERROR.401')); |
| 158 | } |
| 159 | this.activeModal.dismissAll(); |
| 160 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 161 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 162 | } else if (err.error.status === HttpStatus.CONFLICT) { |
| 163 | this.notifierService.notify('error', err.error.detail !== undefined ? |
| 164 | err.error.detail : this.translateService.instant('HTTPERROR.409')); |
| 165 | if (sessionStorage.getItem('usertype') !== 'change_password') { |
| 166 | this.activeModal.dismissAll(); |
| 167 | } |
| 168 | } else if (!isNullOrUndefined(errStatus[err.error.status])) { |
| 169 | this.notifierService.notify('error', err.error.detail !== undefined ? |
| 170 | err.error.detail : errStatus[err.error.status]); |
| SANDHYA.JS | 0fa417a | 2024-03-06 16:19:04 +0530 | [diff] [blame] | 171 | } else { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 172 | this.notifierService.notify('error', err.error.detail !== undefined ? |
| 173 | err.error.detail : this.translateService.instant('ERROR')); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 174 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 175 | } |
| 176 | } |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 177 | |
| 178 | /** Set headers for get Methods @public */ |
| 179 | private getHttpoptions(apiURL: string): APIURLHEADER { |
| 180 | const apiHeaders: HttpHeaders = new HttpHeaders({ |
| 181 | 'Content-Type': 'application/json; charset=UTF-8', |
| 182 | Accept: 'application/json', |
| 183 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 184 | }); |
| 185 | return { |
| 186 | url: apiURL, |
| SANDHYA.JS | 0fa417a | 2024-03-06 16:19:04 +0530 | [diff] [blame] | 187 | httpOptions: { headers: apiHeaders } |
| Barath Kumar R | 218e881 | 2020-09-02 16:43:05 +0530 | [diff] [blame] | 188 | }; |
| 189 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 190 | } |