diff --git a/devday/src/app/services/basic-rest.service.spec.ts b/devday/src/app/services/basic-rest.service.spec.ts new file mode 100644 index 0000000..6b85d46 --- /dev/null +++ b/devday/src/app/services/basic-rest.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { BasicRestService } from './basic-rest.service'; + +describe('BasicRestService', () => { + let service: BasicRestService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(BasicRestService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/devday/src/app/services/basic-rest.service.ts b/devday/src/app/services/basic-rest.service.ts new file mode 100644 index 0000000..fd02aba --- /dev/null +++ b/devday/src/app/services/basic-rest.service.ts @@ -0,0 +1,213 @@ +import { + HttpClient, + HttpParams, + HttpHeaders, + HttpResponse +} from '@angular/common/http'; +import { Injectable, isDevMode } from '@angular/core'; +import { Observable } from 'rxjs'; +import { tap } from 'rxjs/operators'; + +@Injectable({ + providedIn: 'root' +}) +export class BasicRestService { + + constructor( + private httpClient: HttpClient + ) { + } + + /** + * Does a basic HTTP-GET request against a given path. + * @param path the path the request is fired to + * Returns Observable which can be subscribed to + */ + public get(path: string, options?: { + headers?: HttpHeaders | { + [header: string]: string | string[]; + }; + observe?: 'body'; + params?: HttpParams | { + [param: string]: string | string[]; + }; + reportProgress?: boolean; + responseType?: 'json'; + withCredentials?: boolean; + }): Observable { + return this.httpClient.get(path, options).pipe( + tap( + () => { + if (isDevMode()) { + console.log('⬇ GET success ' + path); + } + + } + ) + ); + } + + /** + * Does a basic HTTP-GET request against a given path. + * @param path the path the request is fired to + * Returns Observable which can be subscribed to + */ + public getRaw(path: string, options?: { + headers?: HttpHeaders | { + [header: string]: string | string[]; + }; + observe?: 'body'; + params?: HttpParams | { + [param: string]: string | string[]; + }; + reportProgress?: boolean; + responseType: 'text'; + withCredentials?: boolean; + }): Observable { + return this.httpClient.get(path, {responseType: 'text'}).pipe( + tap( + () => { + if (isDevMode()) { + console.log('GET success ' + path); + } + + } + ) + ); + } + + getBlob(url: string, options: { + headers?: HttpHeaders | { + [header: string]: string | string[]; + }; + observe: 'response'; + params?: HttpParams | { + [param: string]: string | string[]; + }; + reportProgress?: boolean; + responseType: 'blob'; + withCredentials?: boolean; + }): Observable> { + return this.httpClient.get(url, options) + } + + /** + * Constructs a `GET` request that interprets the body as a JSON object and + * returns the full `HTTPResponse`. + * + * @param url The endpoint URL. + * @param options The HTTP options to send with the request. + * + * @return An `Observable` of the full `HTTPResponse` for the request, + * with a response body in the requested type. + */ + getWithHeaders(path: string, options: { + headers?: HttpHeaders | { + [header: string]: string | string[]; + }; + observe: 'response'; + params?: HttpParams | { + [param: string]: string | string[]; + }; + reportProgress?: boolean; + responseType?: 'json'; + withCredentials?: boolean; + }): Observable> { + return this.httpClient.get(path, options).pipe( + tap( + () => { + if (isDevMode()) { + console.log('GET success ' + path); + } + + } + ) + ); + } + + /** + * Sends given data with a HTTP-POST request to a given path + * @param path the path the request is fired to + * @param data the post data which is send to the given path + * Returns Observable which can be subscribed to + */ + public post(path: string, data: any, options?: { + headers?: HttpHeaders | { + [header: string]: string | string[]; + }; + observe?: 'body'; + params?: HttpParams | { + [param: string]: string | string[]; + }; + reportProgress?: boolean; + responseType?: 'json'; + withCredentials?: boolean; + }): Observable { + return this.httpClient.post(path, data, options).pipe( + tap( + () => { + if (isDevMode()) { + console.log('POST success' + path); + } + } + ) + ); + } + + /** + * Sends given data with a HTTP-PUT request to a given path. + * @param path the path the request is fired to + * @param data the put data which is send to the given path + * Returns Observable which can be subscribed to + */ + public put(path: string, data: any, options?: { + headers?: HttpHeaders | { + [header: string]: string | string[]; + }; + observe?: 'body'; + params?: HttpParams | { + [param: string]: string | string[]; + }; + reportProgress?: boolean; + responseType?: 'json'; + withCredentials?: boolean; + }): Observable { + return this.httpClient.put(path, data, options).pipe( + tap( + () => { + if (isDevMode()) { + console.log('Put success' + path); + } + } + ) + ); + } + + /** + * Sends a HTTP-DELETE requst to a given path + * @param path the path the request is fired to + * Returns Observable which can be subscribed to + */ + public delete(path: string, options?: { + headers?: HttpHeaders | { + [header: string]: string | string[]; + }; + observe?: 'body'; + params?: HttpParams | { + [param: string]: string | string[]; + }; + reportProgress?: boolean; + responseType?: 'json'; + withCredentials?: boolean; + }): Observable { + return this.httpClient.delete(path, options).pipe( + tap( + () => { + if (isDevMode()) { + console.log('DELETE success' + path); + } + } + ) + ); + } +}