methods.service.ts 1.5 KB
Newer Older
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Subsystem } from './subsystem';

@Injectable({
  providedIn: 'root'
})
export class MethodsService {

  //private apiUrlBase = 'https://www.x-tee.ee/catalogue/EE/wsdls/';
  public apiUrlBase = 'http://localhost/';
  private apiService = 'index.json';
  private apiUrl = this.apiUrlBase + this.apiService;
  private subsystems: Subsystem[];
  private observable: Observable<Subsystem[]>;

  constructor(private http: HttpClient) {
    this.observable = this.http.get<Subsystem[]>(this.apiUrl)
    .pipe(
      catchError(this.handleError('getMethods', []))
    );
    this.observable.subscribe(subsystems => this.subsystems = subsystems);
  }

  getMethods (): Observable<Subsystem[]> {
    if (this.subsystems) {
      return of(this.subsystems)
    } else {
      return this.observable;
    }
  }


  /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
}