Add custom http methods wrapper.
- Logs response and duration of request.
This commit is contained in:
parent
c96ebec1d2
commit
6621f9fc42
16
src/app/_services/http-wrapper.service.spec.ts
Normal file
16
src/app/_services/http-wrapper.service.spec.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { HttpWrapperService } from './http-wrapper.service';
|
||||
|
||||
describe('HttpWrapperService', () => {
|
||||
let service: HttpWrapperService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(HttpWrapperService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
59
src/app/_services/http-wrapper.service.ts
Normal file
59
src/app/_services/http-wrapper.service.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {HttpClient, HttpRequest} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import * as moment from 'moment';
|
||||
import { Moment } from 'moment';
|
||||
import {LoggingService} from '@app/_services/logging.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class HttpWrapperService {
|
||||
|
||||
constructor(
|
||||
private http: HttpClient,
|
||||
private loggingService: LoggingService,
|
||||
) { }
|
||||
|
||||
get(url: string, options?: any): Observable<Response> {
|
||||
return this.request('GET', url, null, options);
|
||||
}
|
||||
|
||||
post(url: string, body: any, options?: any): Observable<Response> {
|
||||
return this.request('POST', url, body, options);
|
||||
}
|
||||
|
||||
put(url: string, body: any, options?: any): Observable<Response> {
|
||||
return this.request('PUT', url, body, options);
|
||||
}
|
||||
|
||||
delete(url: string, options?: any): Observable<Response> {
|
||||
return this.request('DELETE', url, null, options);
|
||||
}
|
||||
|
||||
private logTime(startMoment: Moment, url: string, method: string): void {
|
||||
const requestDuration = moment().diff(startMoment, 'milliseconds');
|
||||
this.loggingService.sendInfoLevelMessage(`HTTP ${method}, URL: ${url}, Duration: ${requestDuration} ms`);
|
||||
}
|
||||
|
||||
private request(method: string, url: string, body?: any, options?: any): Observable<any> {
|
||||
this.loggingService.sendInfoLevelMessage(`Options: ${options}`);
|
||||
return Observable.create((observer: any) => {
|
||||
const requestBeginTime = moment();
|
||||
this.http.request(new HttpRequest(method, url, body, 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;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user