blob: ea1a5c356d892dc066ad38701cb2d2d8aff0b790 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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
23import { HttpClient, HttpHeaders } from '@angular/common/http';
24import { Injectable } from '@angular/core';
25import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
26import { TranslateService } from '@ngx-translate/core';
27import { NotifierService } from 'angular-notifier';
Barath Kumar R218e8812020-09-02 16:43:05 +053028import { APIURLHEADER, ERRORDATA } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053029import * as HttpStatus from 'http-status-codes';
30import { Observable } from 'rxjs';
SANDHYA.JS26570112024-07-05 21:35:46 +053031import { isNullOrUndefined } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053032
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 */
39export 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.JSc84f1122024-06-04 21:50:03 +053044 /** 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.m3b4814a2020-05-01 19:48:54 +053047 /** 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 R218e8812020-09-02 16:43:05 +053067 const getRequest: APIURLHEADER = this.getHttpoptions(apiURL);
SANDHYA.JSc84f1122024-06-04 21:50:03 +053068 if (!this.isNullOrUndefined(httpHeaders)) {
Barath Kumar R218e8812020-09-02 16:43:05 +053069 return this.http.get(apiURL, httpHeaders);
70 } else {
71 return this.http.get(getRequest.url, getRequest.httpOptions);
72 }
kumaran.m3b4814a2020-05-01 19:48:54 +053073 }
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 R218e8812020-09-02 16:43:05 +053082 const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url);
SANDHYA.JSc84f1122024-06-04 21:50:03 +053083 if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) {
Barath Kumar R218e8812020-09-02 16:43:05 +053084 return this.http.post(apiURLHeader.url, payload, apiURLHeader.httpOptions);
85 } else {
86 return this.http.post(getRequest.url, payload, getRequest.httpOptions);
87 }
kumaran.m3b4814a2020-05-01 19:48:54 +053088 }
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 R218e8812020-09-02 16:43:05 +053097 const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url);
SANDHYA.JSc84f1122024-06-04 21:50:03 +053098 if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) {
Barath Kumar R218e8812020-09-02 16:43:05 +053099 return this.http.patch(apiURLHeader.url, payload, apiURLHeader.httpOptions);
100 } else {
101 return this.http.patch(getRequest.url, payload, getRequest.httpOptions);
102 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530103 }
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 R218e8812020-09-02 16:43:05 +0530112 const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url);
SANDHYA.JSc84f1122024-06-04 21:50:03 +0530113 if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) {
Barath Kumar R218e8812020-09-02 16:43:05 +0530114 return this.http.put(apiURLHeader.url, payload, apiURLHeader.httpOptions);
115 } else {
116 return this.http.put(getRequest.url, payload, getRequest.httpOptions);
117 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530118 }
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 R218e8812020-09-02 16:43:05 +0530126 const getRequest: APIURLHEADER = this.getHttpoptions(apiURL);
SANDHYA.JSc84f1122024-06-04 21:50:03 +0530127 if (!this.isNullOrUndefined(httpHeaders)) {
Barath Kumar R218e8812020-09-02 16:43:05 +0530128 return this.http.delete(apiURL, httpHeaders);
129 } else {
130 return this.http.delete(getRequest.url, getRequest.httpOptions);
131 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530132 }
Barath Kumar R5d75d512020-09-02 17:00:07 +0530133
kumaran.m3b4814a2020-05-01 19:48:54 +0530134 /**
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.m3b4814a2020-05-01 19:48:54 +0530139 public handleError(err: ERRORDATA, method?: string): void {
SANDHYA.JS26570112024-07-05 21:35:46 +0530140 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.m3b4814a2020-05-01 19:48:54 +0530161 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530162 } 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.JS0fa417a2024-03-06 16:19:04 +0530171 } else {
SANDHYA.JS26570112024-07-05 21:35:46 +0530172 this.notifierService.notify('error', err.error.detail !== undefined ?
173 err.error.detail : this.translateService.instant('ERROR'));
kumaran.m3b4814a2020-05-01 19:48:54 +0530174 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530175 }
176 }
Barath Kumar R218e8812020-09-02 16:43:05 +0530177
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.JS0fa417a2024-03-06 16:19:04 +0530187 httpOptions: { headers: apiHeaders }
Barath Kumar R218e8812020-09-02 16:43:05 +0530188 };
189 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530190}