finished filters
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Injectable, isDevMode } from '@angular/core';
|
||||
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
||||
import {LocalStorageService} from 'angular-2-local-storage';
|
||||
import {Observable} from 'rxjs';
|
||||
@ -11,7 +11,7 @@ import {ModalService} from '@inst-iot/bosch-angular-ui-components';
|
||||
})
|
||||
export class ApiService {
|
||||
|
||||
private host = '/api';
|
||||
private host = isDevMode() ? '/api' : 'https://definma-api.apps.de1.bosch-iot-cloud.com';
|
||||
|
||||
constructor(
|
||||
private http: HttpClient,
|
||||
@ -19,25 +19,29 @@ export class ApiService {
|
||||
private modalService: ModalService
|
||||
) { }
|
||||
|
||||
get<T>(url, f: (data?: T, err?) => void = () => {}) {
|
||||
this.requestErrorHandler<T>(this.http.get(this.host + url, this.authOptions()), f);
|
||||
get hostName() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
post<T>(url, data = null, f: (data?: T, err?) => void = () => {}) {
|
||||
this.requestErrorHandler<T>(this.http.post(this.host + url, data, this.authOptions()), f);
|
||||
get<T>(url, f: (data?: T, err?, headers?) => void = () => {}) {
|
||||
this.requestErrorHandler<T>(this.http.get(this.host + url, this.options()), f);
|
||||
}
|
||||
|
||||
put<T>(url, data = null, f: (data?: T, err?) => void = () => {}) {
|
||||
this.requestErrorHandler<T>(this.http.put(this.host + url, data, this.authOptions()), f);
|
||||
post<T>(url, data = null, f: (data?: T, err?, headers?) => void = () => {}) {
|
||||
this.requestErrorHandler<T>(this.http.post(this.host + url, data, this.options()), f);
|
||||
}
|
||||
|
||||
delete<T>(url, f: (data?: T, err?) => void = () => {}) {
|
||||
this.requestErrorHandler<T>(this.http.delete(this.host + url, this.authOptions()), f);
|
||||
put<T>(url, data = null, f: (data?: T, err?, headers?) => void = () => {}) {
|
||||
this.requestErrorHandler<T>(this.http.put(this.host + url, data, this.options()), f);
|
||||
}
|
||||
|
||||
private requestErrorHandler<T>(observable: Observable<any>, f: (data?: T, err?) => void) {
|
||||
delete<T>(url, f: (data?: T, err?, headers?) => void = () => {}) {
|
||||
this.requestErrorHandler<T>(this.http.delete(this.host + url, this.options()), f);
|
||||
}
|
||||
|
||||
private requestErrorHandler<T>(observable: Observable<any>, f: (data?: T, err?, headers?) => void) {
|
||||
observable.subscribe(data => {
|
||||
f(data, undefined);
|
||||
f(data.body, undefined, data.headers.keys().reduce((s, e) => {s[e.toLowerCase()] = data.headers.get(e); return s; }, {}));
|
||||
}, err => {
|
||||
if (f.length === 2) {
|
||||
f(undefined, err);
|
||||
@ -49,13 +53,17 @@ export class ApiService {
|
||||
});
|
||||
}
|
||||
|
||||
private authOptions() {
|
||||
private options(): {headers: HttpHeaders, observe: 'body'} {
|
||||
return {headers: this.authOptions(), observe: 'response' as 'body'};
|
||||
}
|
||||
|
||||
private authOptions(): HttpHeaders {
|
||||
const auth = this.storage.get('basicAuth');
|
||||
if (auth) {
|
||||
return {headers: new HttpHeaders({Authorization: 'Basic ' + auth})};
|
||||
return new HttpHeaders({Authorization: 'Basic ' + auth});
|
||||
}
|
||||
else {
|
||||
return {};
|
||||
return new HttpHeaders();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user