You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemmionic5/Mohem/src/app/hmg-common/services/connector/connector.service.ts

260 lines
7.0 KiB
TypeScript

import { Injectable } from '@angular/core';
6 years ago
import {
HttpClient,
HttpHeaders,
HttpErrorResponse
} from '@angular/common/http';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError, retry, tap, timeout } from 'rxjs/operators';
import { CommonService } from '../common/common.service';
// import { Response } from "../models/response";
import { Response } from 'src/app/hmg-common/services/models/response';
@Injectable({
providedIn: 'root'
})
export class ConnectorService {
6 years ago
/*
connection configuration settings
*/
6 years ago
static httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
6 years ago
};
public static retryTimes = 0;
public static timeOut = 120 * 1000;
6 years ago
// public static host = 'https://uat.hmgwebservices.com/';
public static host = 'https://hmgwebservices.com/';
6 years ago
constructor(public httpClient: HttpClient, public cs: CommonService) {}
6 years ago
public post(
service: string,
data: any,
onError: any,
errorLabel?: string
): Observable<any> {
this.cs.startLoading();
return this.httpClient
.post<any>(
ConnectorService.host + service,
data,
ConnectorService.httpOptions
)
.pipe(
timeout(ConnectorService.timeOut),
retry(ConnectorService.retryTimes),
tap(
res => this.handleResponse(res, onError, errorLabel),
error => this.handleError(error, onError, errorLabel)
)
);
}
6 years ago
public postItg(
service: string,
data: any,
onError: any,
errorLabel?: string
): Observable<any> {
this.cs.startLoading();
return this.httpClient
.post<any>(
ConnectorService.host + service,
data,
ConnectorService.httpOptions
)
.pipe(
timeout(ConnectorService.timeOut),
retry(ConnectorService.retryTimes),
tap(
res => this.handleResponseItg(res, onError, errorLabel),
error => this.handleError(error, onError, errorLabel)
)
);
}
// public postNoLoad(service: string, data: any, onError: any): Observable<any> {
// return this.httpClient
// .post<any>(
// ConnectorService.host + service,
// data,
// ConnectorService.httpOptions
// )
// .pipe(
// retry(ConnectorService.retryTimes),
// tap(() => {}, () => {})
// );
// }
public postNoLoad(service: string, data: any, onError: any, errorLabel?: string): Observable<any> {
6 years ago
return this.httpClient
.post<any>(
6 years ago
ConnectorService.host + service,
data,
ConnectorService.httpOptions)
.pipe(
timeout(ConnectorService.timeOut),
retry(ConnectorService.retryTimes),
tap((res) => {
this.handleResponse(res, onError, errorLabel, true);
}, (error) => {
this.handleError(error, onError, errorLabel, true);
}));
6 years ago
}
6 years ago
// absolute url connection
public postToken(
service: string,
data: any,
onError: any,
errorLabel?: string
): Observable<any> {
this.cs.startLoading();
return this.httpClient
.post<any>(service, data, ConnectorService.httpOptions)
.pipe(
timeout(ConnectorService.timeOut),
retry(ConnectorService.retryTimes),
tap(
res => this.handleResponse(res, onError, errorLabel),
error => this.handleError(error, onError, errorLabel)
)
);
}
6 years ago
public get(
service: string,
data: any,
onError: any,
errorLabel?: string
): Observable<any> {
this.cs.startLoading();
return this.httpClient.get(service, ConnectorService.httpOptions).pipe(
timeout(ConnectorService.timeOut),
retry(ConnectorService.retryTimes),
tap(
res => this.handleResponse(res, onError, errorLabel),
error => this.handleError(error, onError, errorLabel)
)
);
}
/*
load local json data and convert it to corresponding model
resourceURL such as 'assets/config.json'
*/
6 years ago
public getLocalResrouce(resourceURL: string): Observable<any> {
return this.httpClient.get(resourceURL);
}
6 years ago
/*
public validResponse(result: Response): boolean {
// error type == 2 means you have error
if (result.MessageStatus === 1) {
return true;
} else if (result.MessageStatus === 2) {
return this.cs.hasData(result['SameClinicApptList']);
}
return false;
}
*/
public handleError(error: any, onError: any, errorLabel: string, isPostNoLoad = false) {
// if (!isPostNoLoad) {
// this.cs.stopLoading();
// }
6 years ago
this.cs.stopLoading();
if (error instanceof TimeoutError) {
this.cs.showConnectionTimeout(onError, errorLabel);
} else {
this.cs.showConnectionErrorDialog(onError, errorLabel);
}
6 years ago
}
public handleResponse(result: Response, onError: any, errorLabel: string, isPostNoLoad = false) {
if (!isPostNoLoad) {
this.cs.stopLoading();
}
6 years ago
if (!this.cs.validResponse(result)) {
if (result.IsAuthenticated === false) {
this.cs.stopLoading();
6 years ago
this.cs.presentAcceptDialog(result.ErrorEndUserMessage, () => {
this.cs.sharedService.clearAll();
this.cs.openLogin();
6 years ago
6 years ago
});
return false;
} else if (result.ErrorType === 2 || result.ErrorType === 4) {
// console.log("error expired");
this.cs.stopLoading();
6 years ago
} else {
this.cs.stopLoading();
6 years ago
this.cs.showErrorMessageDialog(
onError,
errorLabel,
result.ErrorEndUserMessage
);
6 years ago
//add flag if user not auth
6 years ago
}
}
6 years ago
}
6 years ago
public handleResponseItg(result: Response, onError: any, errorLabel: string, isPostNoLoad = false) {
console.log("handleResponseItg");
if (!isPostNoLoad) {
this.cs.stopLoading();
}
if (!this.cs.validResponse(result)) {
if (result.IsAuthenticated === false) {
this.cs.stopLoading();
this.cs.presentAcceptDialog(result.ErrorEndUserMessage, () => {
console.log("presentAcceptDialog");
this.cs.sharedService.clearAll();
6 years ago
this.cs.openLogin();
});
return false;
} else if (result.ErrorType === 2 || result.ErrorType === 4) {
console.log("ErrorType if");
// console.log("error expired");
this.cs.stopLoading();
} else {
console.log("ErrorType else");
this.cs.stopLoading();
// this.cs.showErrorMessageDialog(
// onError,
// errorLabel,
// result.Message
// );
//add flag if user not auth
}
}
}
6 years ago
public getURLText(
url: string,
onError: any,
errorLabel?: string
): Observable<any> {
this.cs.startLoading();
return this.httpClient.get(url).pipe(
timeout(ConnectorService.timeOut),
retry(ConnectorService.retryTimes),
tap(
res => {
this.cs.stopLoading();
if (!res) {
this.cs.showConnectionErrorDialog(onError, errorLabel);
}
},
error => this.handleError(error, onError, errorLabel)
)
);
}
}