|
|
@ -1,12 +1,8 @@ |
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
import { Http, Response, RequestOptionsArgs, RequestMethod, Headers } from '@angular/http'; |
|
|
|
import { HttpClient, HttpHeaders, HttpErrorResponse } from "@angular/common/http"; |
|
|
|
|
|
|
|
import 'rxjs/Rx'; |
|
|
|
import { Observable } from 'rxjs/Observable'; |
|
|
|
import 'rxjs/add/observable/throw'; |
|
|
|
import { Observer } from 'rxjs/Observer'; |
|
|
|
import 'rxjs/add/operator/map'; |
|
|
|
import 'rxjs/add/operator/catch'; |
|
|
|
import { Observable, throwError } from 'rxjs'; |
|
|
|
import { retry, map, catchError } from 'rxjs/operators'; |
|
|
|
|
|
|
|
import { SecurityService } from './security.service'; |
|
|
|
import { Guid } from '../../../guid'; |
|
|
@ -15,20 +11,24 @@ import { Guid } from '../../../guid'; |
|
|
|
// is pending to do for the SPA app
|
|
|
|
@Injectable() |
|
|
|
export class DataService { |
|
|
|
constructor(private http: Http, private securityService: SecurityService) { } |
|
|
|
constructor(private http: HttpClient, private securityService: SecurityService) { } |
|
|
|
|
|
|
|
get(url: string, params?: any): Observable<Response> { |
|
|
|
let options: RequestOptionsArgs = {}; |
|
|
|
let options = {}; |
|
|
|
|
|
|
|
if (this.securityService) { |
|
|
|
options.headers = new Headers(); |
|
|
|
options.headers.append('Authorization', 'Bearer ' + this.securityService.GetToken()); |
|
|
|
options["headers"] = new HttpHeaders(); |
|
|
|
options["headers"].append('Authorization', 'Bearer ' + this.securityService.GetToken()); |
|
|
|
} |
|
|
|
|
|
|
|
return this.http.get(url, options).map( |
|
|
|
(res: Response) => { |
|
|
|
return res; |
|
|
|
}).catch(this.handleError); |
|
|
|
return this.http.get(url, options) |
|
|
|
.pipe( |
|
|
|
// retry(3), // retry a failed request up to 3 times
|
|
|
|
map((res: Response) => { |
|
|
|
return res; |
|
|
|
}), |
|
|
|
catchError(this.handleError) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
postWithId(url: string, data: any, params?: any): Observable<Response> { |
|
|
@ -44,70 +44,76 @@ export class DataService { |
|
|
|
} |
|
|
|
|
|
|
|
private doPost(url: string, data: any, needId: boolean, params?: any): Observable<Response> { |
|
|
|
let options: RequestOptionsArgs = {}; |
|
|
|
let options = {}; |
|
|
|
|
|
|
|
options.headers = new Headers(); |
|
|
|
options["headers"] = new HttpHeaders(); |
|
|
|
if (this.securityService) { |
|
|
|
options.headers.append('Authorization', 'Bearer ' + this.securityService.GetToken()); |
|
|
|
options["headers"].append('Authorization', 'Bearer ' + this.securityService.GetToken()); |
|
|
|
} |
|
|
|
if (needId) { |
|
|
|
let guid = Guid.newGuid(); |
|
|
|
options.headers.append('x-requestid', guid); |
|
|
|
options["headers"].append('x-requestid', guid); |
|
|
|
} |
|
|
|
|
|
|
|
return this.http.post(url, data, options).map( |
|
|
|
(res: Response) => { |
|
|
|
return res; |
|
|
|
}).catch(this.handleError); |
|
|
|
return this.http.post(url, data, options) |
|
|
|
.pipe( |
|
|
|
map((res: Response) => { |
|
|
|
return res; |
|
|
|
}), |
|
|
|
catchError(this.handleError) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private doPut(url: string, data: any, needId: boolean, params?: any): Observable<Response> { |
|
|
|
let options: RequestOptionsArgs = {}; |
|
|
|
let options = {}; |
|
|
|
|
|
|
|
options.headers = new Headers(); |
|
|
|
options["headers"] = new HttpHeaders(); |
|
|
|
if (this.securityService) { |
|
|
|
options.headers.append('Authorization', 'Bearer ' + this.securityService.GetToken()); |
|
|
|
options["headers"].append('Authorization', 'Bearer ' + this.securityService.GetToken()); |
|
|
|
} |
|
|
|
if (needId) { |
|
|
|
let guid = Guid.newGuid(); |
|
|
|
options.headers.append('x-requestid', guid); |
|
|
|
options["headers"].append('x-requestid', guid); |
|
|
|
} |
|
|
|
|
|
|
|
return this.http.put(url, data, options).map( |
|
|
|
(res: Response) => { |
|
|
|
return res; |
|
|
|
}).catch(this.handleError); |
|
|
|
return this.http.put(url, data, options) |
|
|
|
.pipe( |
|
|
|
map((res: Response) => { |
|
|
|
return res; |
|
|
|
}), |
|
|
|
catchError(this.handleError) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
delete(url: string, params?: any) { |
|
|
|
let options: RequestOptionsArgs = {}; |
|
|
|
let options = {}; |
|
|
|
|
|
|
|
if (this.securityService) { |
|
|
|
options.headers = new Headers(); |
|
|
|
options.headers.append('Authorization', 'Bearer ' + this.securityService.GetToken()); |
|
|
|
options["headers"] = new HttpHeaders(); |
|
|
|
options["headers"].append('Authorization', 'Bearer ' + this.securityService.GetToken()); |
|
|
|
} |
|
|
|
|
|
|
|
console.log('data.service deleting'); |
|
|
|
// return this.http.delete(url, options).subscribe(
|
|
|
|
// return res;
|
|
|
|
// );
|
|
|
|
|
|
|
|
this.http.delete(url, options).subscribe((res) => { |
|
|
|
console.log('deleted'); |
|
|
|
this.http.delete(url, options) |
|
|
|
.subscribe((res) => {console.log('deleted'); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private handleError(error: any) { |
|
|
|
console.error('server error:', error); |
|
|
|
if (error instanceof Response) { |
|
|
|
let errMessage = ''; |
|
|
|
try { |
|
|
|
errMessage = error.json(); |
|
|
|
} catch (err) { |
|
|
|
errMessage = error.statusText; |
|
|
|
} |
|
|
|
return Observable.throw(errMessage); |
|
|
|
if (error.error instanceof ErrorEvent) { |
|
|
|
// A client-side or network error occurred. Handle it accordingly.
|
|
|
|
console.error('Client side network error occurred:', error.message); |
|
|
|
} else { |
|
|
|
// The backend returned an unsuccessful response code.
|
|
|
|
// The response body may contain clues as to what went wrong,
|
|
|
|
console.error('Backend - ' + |
|
|
|
`status: ${error.status}, ` + |
|
|
|
`statusText: ${error.statusText}, ` + |
|
|
|
`message: ${error.message}`); |
|
|
|
} |
|
|
|
return Observable.throw(error || 'server error'); |
|
|
|
|
|
|
|
// return an observable with a user-facing error message
|
|
|
|
return throwError(error || 'server error'); |
|
|
|
} |
|
|
|
} |