2020-07-13 10:52:10 +02:00
|
|
|
import { Injectable, isDevMode } from '@angular/core';
|
2020-06-19 08:43:22 +02:00
|
|
|
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
|
|
|
import {LocalStorageService} from 'angular-2-local-storage';
|
|
|
|
import {Observable} from 'rxjs';
|
|
|
|
import {ErrorComponent} from '../error/error.component';
|
|
|
|
import {ModalService} from '@inst-iot/bosch-angular-ui-components';
|
|
|
|
|
2020-06-22 10:22:45 +02:00
|
|
|
|
2020-06-19 08:43:22 +02:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ApiService {
|
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
private host = isDevMode() ? '/api' : 'https://definma-api.apps.de1.bosch-iot-cloud.com';
|
2020-06-19 08:43:22 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private http: HttpClient,
|
|
|
|
private storage: LocalStorageService,
|
2020-07-22 10:45:34 +02:00
|
|
|
private modalService: ModalService,
|
|
|
|
private window: Window
|
2020-06-19 08:43:22 +02:00
|
|
|
) { }
|
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
get hostName() {
|
|
|
|
return this.host;
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
// Main HTTP methods
|
2020-07-13 10:52:10 +02:00
|
|
|
get<T>(url, f: (data?: T, err?, headers?) => void = () => {}) {
|
2020-08-12 15:15:31 +02:00
|
|
|
this.requestErrorHandler<T>(this.http.get(this.url(url), this.options()), f);
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
post<T>(url, data = null, f: (data?: T, err?, headers?) => void = () => {}) {
|
2020-08-12 15:15:31 +02:00
|
|
|
this.requestErrorHandler<T>(this.http.post(this.url(url), data, this.options()), f);
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
put<T>(url, data = null, f: (data?: T, err?, headers?) => void = () => {}) {
|
2020-08-12 15:15:31 +02:00
|
|
|
this.requestErrorHandler<T>(this.http.put(this.url(url), data, this.options()), f);
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
delete<T>(url, f: (data?: T, err?, headers?) => void = () => {}) {
|
2020-08-12 15:15:31 +02:00
|
|
|
this.requestErrorHandler<T>(this.http.delete(this.url(url), this.options()), f);
|
2020-07-13 10:52:10 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
// Execute request and handle errors
|
2020-07-13 10:52:10 +02:00
|
|
|
private requestErrorHandler<T>(observable: Observable<any>, f: (data?: T, err?, headers?) => void) {
|
2021-01-18 14:26:40 +01:00
|
|
|
observable.subscribe(data => { // Successful request
|
2020-09-03 15:51:53 +02:00
|
|
|
f(
|
|
|
|
data.body,
|
|
|
|
undefined,
|
|
|
|
data.headers.keys().reduce((s, e) => {s[e.toLowerCase()] = data.headers.get(e); return s; }, {})
|
|
|
|
);
|
2021-01-18 14:26:40 +01:00
|
|
|
}, err => { // Error
|
|
|
|
if (f.length > 1) { // Pass on error
|
2020-08-14 14:29:17 +02:00
|
|
|
f(undefined, err, undefined);
|
2020-06-22 10:22:45 +02:00
|
|
|
}
|
2021-01-18 14:26:40 +01:00
|
|
|
else { // Handle directly
|
2020-08-14 14:29:17 +02:00
|
|
|
this.requestError(err);
|
2020-06-22 10:22:45 +02:00
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
requestError(err) { // Network error dialog
|
2020-08-14 14:29:17 +02:00
|
|
|
const modalRef = this.modalService.openComponent(ErrorComponent);
|
|
|
|
modalRef.instance.message = 'Network request failed!';
|
|
|
|
const details = [err.error.status];
|
|
|
|
if (err.error.details) {
|
|
|
|
details.push(err.error.details);
|
|
|
|
}
|
|
|
|
modalRef.instance.details = details;
|
|
|
|
modalRef.result.then(() => {
|
|
|
|
this.window.location.reload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
private url(url) { // Detect if host was given, otherwise use default host
|
2020-08-12 15:15:31 +02:00
|
|
|
if (/http[s]?:\/\//.test(url)) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return this.host + url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
// Generate request options
|
2020-07-13 10:52:10 +02:00
|
|
|
private options(): {headers: HttpHeaders, observe: 'body'} {
|
|
|
|
return {headers: this.authOptions(), observe: 'response' as 'body'};
|
|
|
|
}
|
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
// Generate Basic Auth
|
2020-07-13 10:52:10 +02:00
|
|
|
private authOptions(): HttpHeaders {
|
2020-06-19 08:43:22 +02:00
|
|
|
const auth = this.storage.get('basicAuth');
|
|
|
|
if (auth) {
|
2020-07-13 10:52:10 +02:00
|
|
|
return new HttpHeaders({Authorization: 'Basic ' + auth});
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-07-13 10:52:10 +02:00
|
|
|
return new HttpHeaders();
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|