- add DatasetApiService wit HttpService
- Interfaces for stations and phenomena
This commit is contained in:
parent
5740a5ddc3
commit
7857e2c5bb
13 changed files with 2495 additions and 1446 deletions
63
src/app/services/dataset-api.service.ts
Normal file
63
src/app/services/dataset-api.service.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
// https://github.com/52North/helgoland-toolbox/blob/develop/libs/core/src/lib/dataset-api/dataset-impl-api-interface.service.ts
|
||||
// https://github.com/52North/helgoland-toolbox/blob/495f75cadcc3e7232206db1cd5dd8bbf3a172c4b/libs/core/src/lib/abstract-services/api-interface.ts#L6
|
||||
// https://github.com/52North/helgoland-toolbox/blob/495f75cadcc3e7232206db1cd5dd8bbf3a172c4b/libs/core/src/lib/api-communication/connectors/dataset-api-v3-connector/api-v3-interface.ts#L321
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Phenomenon } from '../../shared/models/phenomenon';
|
||||
import { Station } from '../../shared/models/station';
|
||||
|
||||
import { HttpService } from './http.service';
|
||||
import { HttpRequestOptions } from '../../shared/models/http-requests';
|
||||
import { HttpParams } from '@angular/common/http';
|
||||
|
||||
|
||||
// @Injectable({
|
||||
// providedIn: 'root'
|
||||
// })
|
||||
|
||||
@Injectable()
|
||||
export class DatasetApiService {
|
||||
|
||||
// constructor() { }
|
||||
constructor(
|
||||
private httpClient: HttpClient,
|
||||
protected httpService: HttpService,) {
|
||||
}
|
||||
|
||||
public getPhenomena(apiUrl: string, params?, options?) {
|
||||
const url = this.createRequestUrl(apiUrl, 'phenomena');
|
||||
return this.requestApi<Phenomenon[]>(url, params, options);
|
||||
}
|
||||
|
||||
public getStations(apiUrl: string, params?, options?: HttpRequestOptions): Observable<Station[]> {
|
||||
const url = this.createRequestUrl(apiUrl, 'features');
|
||||
return this.requestApi<Station[]>(url, params, options);
|
||||
}
|
||||
|
||||
|
||||
protected createRequestUrl(apiUrl: string, endpoint: string, id?: string) {
|
||||
// TODO Check whether apiUrl ends with slash
|
||||
let requestUrl = apiUrl + endpoint;
|
||||
if (id) { requestUrl += '/' + id; }
|
||||
return requestUrl;
|
||||
}
|
||||
|
||||
protected requestApi<T>(
|
||||
url: string, params: HttpParams = new HttpParams(), options: HttpRequestOptions = {}
|
||||
): Observable<T> {
|
||||
return this.httpService.client(options).get<T>(url,
|
||||
{
|
||||
params,
|
||||
headers: this.createBasicAuthHeader(options.basicAuthToken)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected createBasicAuthHeader(token: string): HttpHeaders {
|
||||
const headers = new HttpHeaders();
|
||||
if (token) { return headers.set('Authorization', token); }
|
||||
return headers;
|
||||
}
|
||||
|
||||
}
|
45
src/app/services/http.service.ts
Normal file
45
src/app/services/http.service.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
// https://github.com/52North/helgoland-toolbox/blob/495f75cadcc3e7232206db1cd5dd8bbf3a172c4b/libs/core/src/lib/dataset-api/http.service.ts#L37
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
|
||||
import { Inject, InjectionToken, Optional } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import { HttpRequestOptions } from '../../shared/models/http-requests';
|
||||
|
||||
export const HTTP_SERVICE_INTERCEPTORS = new InjectionToken<HttpServiceInterceptor>('HTTP_SERVICE_INTERCEPTORS');
|
||||
|
||||
export interface HttpServiceHandler {
|
||||
handle(req: HttpRequest<any>, options: Partial<HttpRequestOptions>): Observable<HttpEvent<any>>;
|
||||
}
|
||||
|
||||
export interface HttpServiceInterceptor {
|
||||
intercept(req: HttpRequest<any>, options: Partial<HttpRequestOptions>, next: HttpServiceHandler): Observable<HttpEvent<any>>;
|
||||
}
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class HttpService {
|
||||
|
||||
private handler: HttpServiceHandler;
|
||||
|
||||
constructor(
|
||||
protected httpHandler: HttpHandler,
|
||||
@Optional() @Inject(HTTP_SERVICE_INTERCEPTORS) interceptors: HttpServiceInterceptor[] | null
|
||||
) {
|
||||
let handler: HttpServiceHandler = {
|
||||
handle: (req, options) => httpHandler.handle(req)
|
||||
};
|
||||
if (interceptors) {
|
||||
handler = interceptors.reduceRight((next, interceptor) => ({
|
||||
handle: (req, options) => interceptor.intercept(req, options, next)
|
||||
}), handler);
|
||||
}
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public client(options: HttpRequestOptions = {}): HttpClient {
|
||||
return new HttpClient({
|
||||
handle: (req) => this.handler.handle(req, options)
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue