import { Injectable } from '@angular/core'; import * as globals from '../core/common'; import { Http, RequestOptions, Response, URLSearchParams, ResponseContentType } from '@angular/http'; import { Observable } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class ImportService { constructor(private http: Http) { } //------------------- Web API URL sections --------------------- private getMYOBAuthUrl = globals.serverBase + '/api/Import/RequestMYOBAuthentication'; private getXeroAuthUrl = globals.serverBase + '/api/Import/RequestXEROAuthentication'; private updateOrganisationUrl = globals.serverBase + '/api/Import/UpdateOrganisation'; private getXeroSearchAuthUrl = globals.serverBase + '/api/Import/RequestXEROSearchAuthentication'; private searchXeroUrl = globals.serverBase + '/api/Import/SearchXeroContacts'; private importXeroUrl = globals.serverBase + '/api/Import/ImportXeroContacts'; // MYOB authantication getMYOBAuth(): Observable { return this.http.get(this.getMYOBAuthUrl) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } // Xero authentication getXeroAuth(): Observable { return this.http.get(this.getXeroAuthUrl) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } // Xero search authentication getXeroSearchAuth(): Observable { return this.http.get(this.getXeroSearchAuthUrl) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } // Xero search searchXero(searchText:string,token:string): Observable { let myparams = new URLSearchParams(); myparams.append("searchText", searchText.toString()); myparams.append("token", token.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.searchXeroUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } // Xero search importXero(searchText:string,token:string): Observable { let myparams = new URLSearchParams(); myparams.append("companyName", searchText.toString()); myparams.append("token", token.toString()); let options = new RequestOptions({ params: myparams }); return this.http.get(this.importXeroUrl, options) .pipe(map((response: Response) => response.json()), catchError(this.handleError)) } // Update organisation updateOrganisation(frmData: any) { return this.http.post(this.updateOrganisationUrl, frmData) .pipe(map((response: Response) => response.json()), catchError(this.handleError)); } //The function of handling the error private handleError(error: Response) { console.log("Error in common service"); console.log(error); return Observable.throw(error.json().error || 'Server error'); } }