All files / src/app/shared/api settings.service.ts

92.86% Statements 26/28
80% Branches 12/15
87.5% Functions 7/8
92% Lines 23/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 51100x 100x     100x         100x 125x   125x   100x 37x 37x 19x   12x 12x               18x 11x   7x 5x           100x 3x     100x 18x     100x 5x   100x  
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
 
import * as _ from 'lodash';
import { ApiModule } from './api.module';
 
@Injectable({
  providedIn: ApiModule
})
export class SettingsService {
  constructor(private http: HttpClient) {}
 
  private settings: { [url: string]: string } = {};
 
  ifSettingConfigured(url: string, fn: (value?: string) => void, elseFn?: () => void): void {
    const setting = this.settings[url];
    if (setting === undefined) {
      this.http.get(url).subscribe(
        (data: any) => {
          this.settings[url] = this.getSettingsValue(data);
          this.ifSettingConfigured(url, fn, elseFn);
        },
        (resp) => {
          if (resp.status !== 401) {
            this.settings[url] = '';
          }
        }
      );
    } else if (setting !== '') {
      fn(setting);
    } else {
      if (elseFn) {
        elseFn();
      }
    }
  }
 
  // Easiest way to stop reloading external content that can't be reached
  disableSetting(url) {
    this.settings[url] = '';
  }
 
  private getSettingsValue(data: any): string {
    return data.value || data.instance || '';
  }
 
  validateGrafanaDashboardUrl(uid) {
    return this.http.get(`api/grafana/validation/${uid}`);
  }
}