From 246dddd5a5a09f9c9586441d96e2fd5deaf49fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Kruni=C4=87?= Date: Fri, 14 Dec 2018 10:52:52 +0100 Subject: [PATCH] refactor: move web http client to a new file --- src/app/modules/data/data.module.ts | 3 +- src/app/modules/data/data.provider.ts | 53 -------------- .../modules/data/list/data-list.component.ts | 10 +-- .../data/stapps-web-http-client.provider.ts | 69 +++++++++++++++++++ .../modules/storage/storage.provider.spec.ts | 6 +- 5 files changed, 79 insertions(+), 62 deletions(-) create mode 100644 src/app/modules/data/stapps-web-http-client.provider.ts diff --git a/src/app/modules/data/data.module.ts b/src/app/modules/data/data.module.ts index 4dde6df9..25c46bb7 100644 --- a/src/app/modules/data/data.module.ts +++ b/src/app/modules/data/data.module.ts @@ -18,10 +18,11 @@ import {NgModule} from '@angular/core'; import {FormsModule} from '@angular/forms'; import {IonicModule} from '@ionic/angular'; import {DataRoutingModule} from './data-routing.module'; -import {DataProvider, StAppsWebHttpClient} from './data.provider'; +import {DataProvider} from './data.provider'; import {DataDetailComponent} from './detail/data-detail.component'; import {DataListItem} from './list/data-list-item.component'; import {DataListComponent} from './list/data-list.component'; +import {StAppsWebHttpClient} from './stapps-web-http-client.provider'; import {DishDetailContentComponent} from './types/dish/dish-detail-content.component'; import {DishListItem} from './types/dish/dish-list-item.component'; import {EventListItemComponent} from './types/event/event-list-item.component'; diff --git a/src/app/modules/data/data.provider.ts b/src/app/modules/data/data.provider.ts index 0b59d909..e461a7d8 100644 --- a/src/app/modules/data/data.provider.ts +++ b/src/app/modules/data/data.provider.ts @@ -12,60 +12,7 @@ * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ -import {HttpClient, HttpResponse} from '@angular/common/http'; import {Injectable} from '@angular/core'; -import {HttpClientInterface, HttpClientRequest} from '@openstapps/api/lib/httpClientInterface'; - -/** - * Response with generic for the type of body that is returned from the request - */ -export interface Response extends HttpResponse { - body: TYPE_OF_BODY; - statusCode: number; -} - -/** - * HttpClient that is based on angular's HttpClient (@TODO: move it to provider or independent package) - */ -@Injectable() -export class StAppsWebHttpClient implements HttpClientInterface { - constructor(private http: HttpClient) { - } - - /** - * Make a request - * @param requestConfig Configuration of the request - */ - async request( - requestConfig: HttpClientRequest, - ): Promise> { - const options: { - [key: string]: any; - observe: 'response'; - } = { - body: {}, - observe: 'response', - responseType: 'json', - }; - - if (typeof requestConfig.body !== 'undefined') { - options.body = requestConfig.body; - } - - if (typeof requestConfig.headers !== 'undefined') { - options.headers = requestConfig.headers; - } - - try { - const response: HttpResponse = await this.http.request( - requestConfig.method || 'GET', requestConfig.url.toString(), options) - .toPromise(); - return Object.assign(response, {statusCode: response.status, body: response.body || {}}); - } catch (err) { - throw Error(err); - } - } -} /* Generated class for the DataProvider provider. diff --git a/src/app/modules/data/list/data-list.component.ts b/src/app/modules/data/list/data-list.component.ts index ab3b9cae..8d71520c 100644 --- a/src/app/modules/data/list/data-list.component.ts +++ b/src/app/modules/data/list/data-list.component.ts @@ -18,7 +18,7 @@ import {Client} from '@openstapps/api/lib/client'; import {SCThing} from '@openstapps/core'; import {Subject} from 'rxjs'; import {debounceTime, distinctUntilChanged} from 'rxjs/operators'; -import {StAppsWebHttpClient} from '../data.provider'; +import {StAppsWebHttpClient} from '../stapps-web-http-client.provider'; @Component({ selector: 'stapps-data-list', @@ -39,14 +39,14 @@ export class DataListComponent { loading: HTMLIonLoadingElement; constructor(private loadingController: LoadingController, - private alertController: AlertController, - swHttpClient: StAppsWebHttpClient) { + private alertController: AlertController, + swHttpClient: StAppsWebHttpClient) { this.client = new Client(swHttpClient, 'https://stappsbe01.innocampus.tu-berlin.de', '1.0.6'); this.queryChanged .pipe( - debounceTime(1000), - distinctUntilChanged()) + debounceTime(1000), + distinctUntilChanged()) .subscribe((model) => { this.from = 0; this.query = model; diff --git a/src/app/modules/data/stapps-web-http-client.provider.ts b/src/app/modules/data/stapps-web-http-client.provider.ts new file mode 100644 index 00000000..41681a90 --- /dev/null +++ b/src/app/modules/data/stapps-web-http-client.provider.ts @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2018 StApps + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ +import {HttpClient, HttpResponse} from '@angular/common/http'; +import {Injectable} from '@angular/core'; +import {HttpClientInterface} from '@openstapps/api/lib/httpClientInterface'; +import {HttpClientRequest} from '@openstapps/api/lib/httpClientInterface'; + +/** + * HttpClient that is based on angular's HttpClient (@TODO: move it to provider or independent package) + */ +@Injectable() +export class StAppsWebHttpClient implements HttpClientInterface { + constructor(private http: HttpClient) { + } + + /** + * Make a request + * @param requestConfig Configuration of the request + */ + async request( + requestConfig: HttpClientRequest, + ): Promise> { + const options: { + [key: string]: any; + observe: 'response'; + } = { + body: {}, + observe: 'response', + responseType: 'json', + }; + + if (typeof requestConfig.body !== 'undefined') { + options.body = requestConfig.body; + } + + if (typeof requestConfig.headers !== 'undefined') { + options.headers = requestConfig.headers; + } + + try { + const response: HttpResponse = await this.http.request( + requestConfig.method || 'GET', requestConfig.url.toString(), options) + .toPromise(); + return Object.assign(response, {statusCode: response.status, body: response.body || {}}); + } catch (err) { + throw Error(err); + } + } +} + +/** + * Response with generic for the type of body that is returned from the request + */ +export interface Response extends HttpResponse { + body: TYPE_OF_BODY; + statusCode: number; +} diff --git a/src/app/modules/storage/storage.provider.spec.ts b/src/app/modules/storage/storage.provider.spec.ts index 6ab4af5b..27da31d5 100644 --- a/src/app/modules/storage/storage.provider.spec.ts +++ b/src/app/modules/storage/storage.provider.spec.ts @@ -48,8 +48,8 @@ describe('StorageProvider', () => { it('should call set method of storage to put a value', () => { spyOn(storage, 'set'); - storageProvider.put('some-uid', { some: 'thing' }); - expect(storage.set).toHaveBeenCalledWith('some-uid', { some: 'thing' }); + storageProvider.put('some-uid', {some: 'thing'}); + expect(storage.set).toHaveBeenCalledWith('some-uid', {some: 'thing'}); }); it('should call get method of storage to get a value', () => { @@ -99,7 +99,7 @@ describe('StorageProvider', () => { spyOn(storageProvider, 'get').and.callThrough(); await storageProvider.putMultiple(sampleEntries); const entries = await storageProvider.getAll(); - expect(Array.from(entries.values()).map((item) => (item.foo) ? item.foo : item).sort()) + expect(Array.from(entries.values()).map((item) => (item.foo) ? item.foo : item).sort()) .toEqual(Array.from(sampleEntries.values()).map((item) => (item.foo) ? item.foo : item).sort()); expect(Array.from(entries.keys()).sort()).toEqual(['bar', 'foo', 'foobar']); });