From 6621f9fc427b673a36dde0a233b36c95b662a910 Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Sun, 14 Mar 2021 11:17:27 +0300 Subject: [PATCH] Add custom http methods wrapper. - Logs response and duration of request. --- .../_services/http-wrapper.service.spec.ts | 16 +++++ src/app/_services/http-wrapper.service.ts | 59 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/app/_services/http-wrapper.service.spec.ts create mode 100644 src/app/_services/http-wrapper.service.ts diff --git a/src/app/_services/http-wrapper.service.spec.ts b/src/app/_services/http-wrapper.service.spec.ts new file mode 100644 index 0000000..b18b6b1 --- /dev/null +++ b/src/app/_services/http-wrapper.service.spec.ts @@ -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(); + }); +}); diff --git a/src/app/_services/http-wrapper.service.ts b/src/app/_services/http-wrapper.service.ts new file mode 100644 index 0000000..4e17804 --- /dev/null +++ b/src/app/_services/http-wrapper.service.ts @@ -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 { + return this.request('GET', url, null, options); + } + + post(url: string, body: any, options?: any): Observable { + return this.request('POST', url, body, options); + } + + put(url: string, body: any, options?: any): Observable { + return this.request('PUT', url, body, options); + } + + delete(url: string, options?: any): Observable { + 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 { + 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; + } + }); + }); + } +}