Radek Krejci | 80e003e | 2017-09-21 14:20:20 +0200 | [diff] [blame] | 1 | import { Injectable } from '@angular/core'; |
Jakub Man | 8f64287 | 2018-06-13 15:48:28 +0200 | [diff] [blame] | 2 | import { HttpClient } from '@angular/common/http'; |
Radek Krejci | 80e003e | 2017-09-21 14:20:20 +0200 | [diff] [blame] | 3 | import { Observable } from 'rxjs/Observable'; |
Jakub Man | 59c4ea1 | 2018-06-13 15:21:32 +0200 | [diff] [blame] | 4 | import { catchError } from 'rxjs/operators'; |
Radek Krejci | 80e003e | 2017-09-21 14:20:20 +0200 | [diff] [blame] | 5 | |
| 6 | import { Device } from './device'; |
| 7 | |
| 8 | @Injectable() |
| 9 | export class DevicesService { |
Jakub Man | 59c4ea1 | 2018-06-13 15:21:32 +0200 | [diff] [blame] | 10 | constructor(private http: HttpClient) {} |
Radek Krejci | 80e003e | 2017-09-21 14:20:20 +0200 | [diff] [blame] | 11 | |
| 12 | getDevices(): Observable<Device[]> { |
Jakub Man | 59c4ea1 | 2018-06-13 15:21:32 +0200 | [diff] [blame] | 13 | return this.http.get<Device[]>('/netopeer/inventory/devices/list') |
| 14 | .pipe( |
| 15 | catchError(err => Observable.throw(err)) |
| 16 | ); |
Radek Krejci | 80e003e | 2017-09-21 14:20:20 +0200 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | addDevice(device: Device) { |
Jakub Man | 59c4ea1 | 2018-06-13 15:21:32 +0200 | [diff] [blame] | 20 | // let options = new HttpOptions({ body: JSON.stringify(device) }); |
| 21 | return this.http.post<object>('/netopeer/inventory/devices', device) |
| 22 | .pipe( |
| 23 | catchError(err => Observable.throw(err)) |
| 24 | ); |
Radek Krejci | 80e003e | 2017-09-21 14:20:20 +0200 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | rmDevice(device_id: number) { |
Jakub Man | 59c4ea1 | 2018-06-13 15:21:32 +0200 | [diff] [blame] | 28 | // We need to use generic HTTP request, because HttpClient does not support body in DELETE requests. |
| 29 | return this.http.request('DELETE', '/netopeer/inventory/devices', { body: JSON.stringify({'id':device_id}) }) |
| 30 | .pipe( |
| 31 | catchError(err => Observable.throw(err)) |
| 32 | ); |
Radek Krejci | 80e003e | 2017-09-21 14:20:20 +0200 | [diff] [blame] | 33 | } |
| 34 | } |