Correct way of loading symbols

This commit is contained in:
ziajka 2018-03-21 10:06:39 +01:00
parent d399a59d7f
commit 201b802707
2 changed files with 31 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {HttpHeaders, HttpClient, HttpParams, HttpResponse} from '@angular/common/http'; import {HttpHeaders, HttpClient, HttpParams} from '@angular/common/http';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import {Server} from "../models/server"; import {Server} from "../models/server";
@ -23,7 +23,7 @@ export type TextOptions = {
headers?: HttpHeaders | { headers?: HttpHeaders | {
[header: string]: string | string[]; [header: string]: string | string[];
}; };
observe: 'response'; observe?: 'body';
params?: HttpParams | { params?: HttpParams | {
[param: string]: string | string[]; [param: string]: string | string[];
}; };
@ -37,7 +37,6 @@ export type HeadersOptions = {
[header: string]: string | string[]; [header: string]: string | string[];
}; };
}; };
/* tslint:enable:interface-over-type-literal */ /* tslint:enable:interface-over-type-literal */
@ -47,50 +46,72 @@ export class HttpServer {
constructor(private http: HttpClient) { } constructor(private http: HttpClient) { }
get<T>(server: Server, url: string, options?: JsonOptions): Observable<T> { get<T>(server: Server, url: string, options?: JsonOptions): Observable<T> {
options = this.getJsonOptions(options);
const intercepted = this.getOptionsForServer<JsonOptions>(server, url, options); const intercepted = this.getOptionsForServer<JsonOptions>(server, url, options);
return this.http.get<T>(intercepted.url, intercepted.options as JsonOptions); return this.http.get<T>(intercepted.url, intercepted.options as JsonOptions);
}; }
getText(server: Server, url: string, options?: TextOptions): Observable<HttpResponse<string>> { getText(server: Server, url: string, options?: TextOptions): Observable<string> {
options = this.getTextOptions(options);
const intercepted = this.getOptionsForServer<TextOptions>(server, url, options); const intercepted = this.getOptionsForServer<TextOptions>(server, url, options);
return this.http.get(intercepted.url, intercepted.options); return this.http.get(intercepted.url, intercepted.options as TextOptions);
} }
post<T>(server: Server, url: string, body: any | null, options?: JsonOptions): Observable<T> { post<T>(server: Server, url: string, body: any | null, options?: JsonOptions): Observable<T> {
options = this.getJsonOptions(options);
const intercepted = this.getOptionsForServer(server, url, options); const intercepted = this.getOptionsForServer(server, url, options);
return this.http.post<T>(intercepted.url, body, intercepted.options); return this.http.post<T>(intercepted.url, body, intercepted.options);
} }
put<T>(server: Server, url: string, body: any, options?: JsonOptions): Observable<T> { put<T>(server: Server, url: string, body: any, options?: JsonOptions): Observable<T> {
options = this.getJsonOptions(options);
const intercepted = this.getOptionsForServer(server, url, options); const intercepted = this.getOptionsForServer(server, url, options);
return this.http.put<T>(intercepted.url, body, intercepted.options); return this.http.put<T>(intercepted.url, body, intercepted.options);
} }
delete<T>(server: Server, url: string, options?: JsonOptions): Observable<T> { delete<T>(server: Server, url: string, options?: JsonOptions): Observable<T> {
options = this.getJsonOptions(options);
const intercepted = this.getOptionsForServer(server, url, options); const intercepted = this.getOptionsForServer(server, url, options);
return this.http.delete<T>(intercepted.url, intercepted.options); return this.http.delete<T>(intercepted.url, intercepted.options);
} }
patch<T>(server: Server, url: string, body: any, options?: JsonOptions): Observable<T> { patch<T>(server: Server, url: string, body: any, options?: JsonOptions): Observable<T> {
options = this.getJsonOptions(options);
const intercepted = this.getOptionsForServer(server, url, options); const intercepted = this.getOptionsForServer(server, url, options);
return this.http.patch<T>(intercepted.url, body, intercepted.options); return this.http.patch<T>(intercepted.url, body, intercepted.options);
} }
head<T>(server: Server, url: string, options?: JsonOptions): Observable<T> { head<T>(server: Server, url: string, options?: JsonOptions): Observable<T> {
options = this.getJsonOptions(options);
const intercepted = this.getOptionsForServer(server, url, options); const intercepted = this.getOptionsForServer(server, url, options);
return this.http.head<T>(intercepted.url, intercepted.options); return this.http.head<T>(intercepted.url, intercepted.options);
} }
options<T>(server: Server, url: string, options?: JsonOptions): Observable<T> { options<T>(server: Server, url: string, options?: JsonOptions): Observable<T> {
options = this.getJsonOptions(options);
const intercepted = this.getOptionsForServer(server, url, options); const intercepted = this.getOptionsForServer(server, url, options);
return this.http.options<T>(intercepted.url, intercepted.options); return this.http.options<T>(intercepted.url, intercepted.options);
} }
private getOptionsForServer<T extends HeadersOptions>(server: Server, url: string, options: T) { private getJsonOptions(options: JsonOptions): JsonOptions {
if (options === null) { if (!options) {
options = {} as T; return {
responseType: "json"
};
} }
return options;
}
private getTextOptions(options: TextOptions): TextOptions {
if (!options) {
return {
responseType: "text"
};
}
return options;
}
private getOptionsForServer<T extends HeadersOptions>(server: Server, url: string, options: T) {
url = `http://${server.ip}:${server.port}/v2${url}`; url = `http://${server.ip}:${server.port}/v2${url}`;
if (options.headers === null) { if (options.headers === null) {

View File

@ -28,7 +28,7 @@ export class SymbolService {
const streams = symbols.map(symbol => this.raw(server, symbol.symbol_id)); const streams = symbols.map(symbol => this.raw(server, symbol.symbol_id));
Observable.forkJoin(streams).subscribe((results) => { Observable.forkJoin(streams).subscribe((results) => {
symbols.forEach((symbol: Symbol, i: number) => { symbols.forEach((symbol: Symbol, i: number) => {
symbol.raw = results[i].body; symbol.raw = results[i];
}); });
this.symbols.next(symbols); this.symbols.next(symbols);
}); });