/http-wrapper.service.ts Secret
Last active
July 1, 2018 21:39
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import 'rxjs/add/operator/catch'; | |
import { | |
Headers, | |
Http, | |
RequestMethod, | |
RequestOptionsArgs, | |
RequestOptions, | |
Request, | |
Response | |
} from '@angular/http'; | |
import { Router } from '@angular/router'; | |
import { Observable } from 'rxjs'; | |
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'; | |
import { LogService } from '@app/core/log/log.service'; | |
import * as moment from 'moment'; | |
import { Moment } from 'moment'; | |
@Injectable() | |
export class HttpWrapperService { | |
constructor(private http: HttpClient, private logService: LogService) {} | |
get(url: string, options?: any): Observable<Response> { | |
console.log('option in get', options); | |
return this.request('GET', url, options); | |
} | |
post(url: string, options?: any): Observable<Response> { | |
return this.request('POST', url, options); | |
} | |
put(url: string, options?: any): Observable<Response> { | |
return this.request('PUT', url, options); | |
} | |
delete(url: string, options?: any): Observable<Response> { | |
return this.request('DELETE', url, options); | |
} | |
private logTime(startMoment: Moment, url: string, method: string) { | |
const requestDuration = moment().diff(startMoment, 'milliseconds'); | |
this.logService.logHttpInfo(`HTTP ${method}`, requestDuration, url); | |
} | |
private request(method: string, url: string, options?: any) { | |
console.log('options', options); | |
return Observable.create((observer: any) => { | |
const requestBeginTime = moment(); | |
this.http.request(new HttpRequest(method, url, options)).subscribe( | |
(response) => { | |
this.logTime(requestBeginTime, `${url}`, method), | |
observer.next(response); | |
observer.complete(); | |
}, | |
(error) => { | |
switch (error.status) { | |
case 403: | |
observer.complete(); | |
break; | |
default: | |
observer.error(error); | |
break; | |
} | |
} | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment