import { Injectable } from '@angular/core'; import { Observable, Subject, throwError } from 'rxjs'; import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http'; import { catchError, retry } from 'rxjs/operators'; import { DatePipe } from '@angular/common'; @Injectable({ providedIn: 'root', }) export class ApiService { // For Development public static BASE_URL = 'https://localhost:44375/api'; public base_url = 'https://localhost:44375/api'; // For WeLink /*public static BASE_URL = '/welinkventures/api'; public base_url = '/welinkventures/api';*/ // For BAT /*public static BASE_URL = '/batDemo/api'; public base_url = '/batDemo/api';*/ // For Demo /*public static BASE_URL = '/celDemo/api'; public base_url = '/celDemo/api';*/ // For LiFung App Test //public base_url = '/lifungtest/api'; //public static BASE_URL = '/lifungtest/api'; // For Synovia Test // public base_url = '/synovia/api'; // public static BASE_URL = '/synovia/api'; // For Synovia Live // public base_url = '/synoviahr/api'; // public static BASE_URL = '/synoviahr/api'; // For BProperty Live /*public base_url = '/api'; public static BASE_URL = '/api';*/ // For Azure /*public static BASE_URL = '/api'; public base_url = '/api';*/ // For H&M Live /*public base_url = '/hnm/api'; public static BASE_URL = '/hnm/api';*/ // For erecruitment /*public base_url = '/erecruitment/api'; public static BASE_URL = '/erecruitment/api';*/ // public mobileAppDownloadLink = this.BASE_URL + 'MobileApp/Download'; public static AuthenticationToken = ''; public selectedMenuName: string = ''; constructor(public httpClient: HttpClient, public datepipe: DatePipe) { } createClientHeader(): { headers: HttpHeaders } { let token = ''; if (ApiService.AuthenticationToken) { token = `Bearer ${ApiService.AuthenticationToken}`; } const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token }) }; return httpOptions; } getApiDefaultData(str: string) { return (str === undefined || str === null || str.trim() === '') ? null : str; } getApiDateString(dDate: Date) { if (dDate === undefined || dDate === null) return dDate; var str: string = this.datepipe.transform(dDate, 'dd-MMM-yyyy'); return str; } getApiDefaultIntData(intData: number) { return (intData === undefined || intData === null) ? undefined : intData; } public httpGet(url: string, params?: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } if (params) { const p = []; for (const k in params) { p.push((k + '=' + params[k]) as any); } url = url + '?' + (p.join('&')); } var endpoint: string; return this.httpClient.get(this.base_url + url, { headers: options, withCredentials: true }); // return this.http.get(`${url}`, { headers: options, withCredentials: true }); } public httpGetByKey(url: string, key: any): Observable { return this.httpClient.get(ApiService.BASE_URL + url + key, this.createClientHeader()) .pipe( catchError(this.handleClientError) ); } public httpPost(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } debugger; if (ApiService.AuthenticationToken) { options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true }); //return this.httpClient.post(ApiService.BASE_URL + url, body, this.createClientHeader()) // .pipe( // catchError(this.handleClientError) // ); } public httpPostLoginWithGoogle(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true }); } //public LoginWithGoogle(credentials: string): Observable { // const header = new HttpHeaders().set('Content-type', 'application/json'); // return this.httpClient.post(this.base_url + "/auth/loginWithGoogle", JSON.stringify(credentials), { headers: header }); //} public httpDelete(url: string, key: any): Observable { return this.httpClient.delete(ApiService.BASE_URL + url + key, this.createClientHeader()) .pipe( catchError(this.handleClientError) ); } public httpPostFile(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true }) .pipe( catchError(this.handleClientError) ); } public httpPostBlob(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { //options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); //options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = new HttpHeaders(); options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true }) .pipe( catchError(this.handleClientError) ); } public httpDownloadFile(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { //options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); //options = options.append('Accept', 'application/json'); } if (ApiService.AuthenticationToken) { options = new HttpHeaders(); options = options.append('Authorization', `Bearer ${ApiService.AuthenticationToken}`); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true, responseType: 'blob' as 'json' }) .pipe( catchError(this.handleClientError) ); } public httpDownloadFileNew(url: string, body: any): Observable { var options: HttpHeaders; if (!options) { options = new HttpHeaders({ 'Content-Type': 'application/json;charset=utf-8' }); options = options.append('Accept', 'application/json'); } return this.httpClient.post(this.base_url + url, body, { headers: options, withCredentials: true, responseType: 'blob' as 'json' }); } public handleClientError(error: HttpErrorResponse) { // return Observable.throw(error.message || 'An error has been occurred. Please try again or contact to system administrator'); return throwError(error.error || 'An error has been occurred. Please try again or contact to system administrator'); } } @Injectable() export class PubSubService { public subjects: Subject[] = []; publish(eventName: string) { this.subjects[eventName] = this.subjects[eventName] || new Subject(); this.subjects[eventName].next(); } on(eventName: string): Observable { this.subjects[eventName] = this.subjects[eventName] || new Subject(); return this.subjects[eventName].asObservable(); } } @Injectable() export class BaseHrefService { }