mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
refactor: move web http client to a new file
This commit is contained in:
@@ -18,10 +18,11 @@ import {NgModule} from '@angular/core';
|
|||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {IonicModule} from '@ionic/angular';
|
import {IonicModule} from '@ionic/angular';
|
||||||
import {DataRoutingModule} from './data-routing.module';
|
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 {DataDetailComponent} from './detail/data-detail.component';
|
||||||
import {DataListItem} from './list/data-list-item.component';
|
import {DataListItem} from './list/data-list-item.component';
|
||||||
import {DataListComponent} from './list/data-list.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 {DishDetailContentComponent} from './types/dish/dish-detail-content.component';
|
||||||
import {DishListItem} from './types/dish/dish-list-item.component';
|
import {DishListItem} from './types/dish/dish-list-item.component';
|
||||||
import {EventListItemComponent} from './types/event/event-list-item.component';
|
import {EventListItemComponent} from './types/event/event-list-item.component';
|
||||||
|
|||||||
@@ -12,60 +12,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import {HttpClient, HttpResponse} from '@angular/common/http';
|
|
||||||
import {Injectable} from '@angular/core';
|
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<TYPE_OF_BODY> extends HttpResponse<TYPE_OF_BODY> {
|
|
||||||
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<TYPE_OF_BODY>(
|
|
||||||
requestConfig: HttpClientRequest,
|
|
||||||
): Promise<Response<TYPE_OF_BODY>> {
|
|
||||||
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<TYPE_OF_BODY> = await this.http.request<TYPE_OF_BODY>(
|
|
||||||
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.
|
Generated class for the DataProvider provider.
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import {Client} from '@openstapps/api/lib/client';
|
|||||||
import {SCThing} from '@openstapps/core';
|
import {SCThing} from '@openstapps/core';
|
||||||
import {Subject} from 'rxjs';
|
import {Subject} from 'rxjs';
|
||||||
import {debounceTime, distinctUntilChanged} from 'rxjs/operators';
|
import {debounceTime, distinctUntilChanged} from 'rxjs/operators';
|
||||||
import {StAppsWebHttpClient} from '../data.provider';
|
import {StAppsWebHttpClient} from '../stapps-web-http-client.provider';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'stapps-data-list',
|
selector: 'stapps-data-list',
|
||||||
@@ -39,14 +39,14 @@ export class DataListComponent {
|
|||||||
loading: HTMLIonLoadingElement;
|
loading: HTMLIonLoadingElement;
|
||||||
|
|
||||||
constructor(private loadingController: LoadingController,
|
constructor(private loadingController: LoadingController,
|
||||||
private alertController: AlertController,
|
private alertController: AlertController,
|
||||||
swHttpClient: StAppsWebHttpClient) {
|
swHttpClient: StAppsWebHttpClient) {
|
||||||
this.client = new Client(swHttpClient, 'https://stappsbe01.innocampus.tu-berlin.de', '1.0.6');
|
this.client = new Client(swHttpClient, 'https://stappsbe01.innocampus.tu-berlin.de', '1.0.6');
|
||||||
|
|
||||||
this.queryChanged
|
this.queryChanged
|
||||||
.pipe(
|
.pipe(
|
||||||
debounceTime(1000),
|
debounceTime(1000),
|
||||||
distinctUntilChanged())
|
distinctUntilChanged())
|
||||||
.subscribe((model) => {
|
.subscribe((model) => {
|
||||||
this.from = 0;
|
this.from = 0;
|
||||||
this.query = model;
|
this.query = model;
|
||||||
|
|||||||
69
src/app/modules/data/stapps-web-http-client.provider.ts
Normal file
69
src/app/modules/data/stapps-web-http-client.provider.ts
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
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<TYPE_OF_BODY>(
|
||||||
|
requestConfig: HttpClientRequest,
|
||||||
|
): Promise<Response<TYPE_OF_BODY>> {
|
||||||
|
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<TYPE_OF_BODY> = await this.http.request<TYPE_OF_BODY>(
|
||||||
|
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<TYPE_OF_BODY> extends HttpResponse<TYPE_OF_BODY> {
|
||||||
|
body: TYPE_OF_BODY;
|
||||||
|
statusCode: number;
|
||||||
|
}
|
||||||
@@ -48,8 +48,8 @@ describe('StorageProvider', () => {
|
|||||||
|
|
||||||
it('should call set method of storage to put a value', () => {
|
it('should call set method of storage to put a value', () => {
|
||||||
spyOn(storage, 'set');
|
spyOn(storage, 'set');
|
||||||
storageProvider.put('some-uid', { some: 'thing' });
|
storageProvider.put('some-uid', {some: 'thing'});
|
||||||
expect(storage.set).toHaveBeenCalledWith('some-uid', { some: 'thing' });
|
expect(storage.set).toHaveBeenCalledWith('some-uid', {some: 'thing'});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call get method of storage to get a value', () => {
|
it('should call get method of storage to get a value', () => {
|
||||||
@@ -99,7 +99,7 @@ describe('StorageProvider', () => {
|
|||||||
spyOn(storageProvider, 'get').and.callThrough();
|
spyOn(storageProvider, 'get').and.callThrough();
|
||||||
await storageProvider.putMultiple(sampleEntries);
|
await storageProvider.putMultiple(sampleEntries);
|
||||||
const entries = await storageProvider.getAll();
|
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());
|
.toEqual(Array.from(sampleEntries.values()).map((item) => (item.foo) ? item.foo : item).sort());
|
||||||
expect(Array.from(entries.keys()).sort()).toEqual(['bar', 'foo', 'foobar']);
|
expect(Array.from(entries.keys()).sort()).toEqual(['bar', 'foo', 'foobar']);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user