]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClient } from '@angular/common/http'; |
2 | import { Injectable } from '@angular/core'; | |
3 | ||
4 | import * as _ from 'lodash'; | |
5 | import { ApiModule } from './api.module'; | |
6 | ||
7 | @Injectable({ | |
8 | providedIn: ApiModule | |
9 | }) | |
10 | export class SettingsService { | |
11 | constructor(private http: HttpClient) {} | |
12 | ||
13 | private settings: { [url: string]: string } = {}; | |
14 | ||
494da23a | 15 | ifSettingConfigured(url: string, fn: (value?: string) => void, elseFn?: () => void): void { |
11fdf7f2 TL |
16 | const setting = this.settings[url]; |
17 | if (setting === undefined) { | |
494da23a TL |
18 | this.http.get(url).subscribe( |
19 | (data: any) => { | |
20 | this.settings[url] = this.getSettingsValue(data); | |
21 | this.ifSettingConfigured(url, fn, elseFn); | |
22 | }, | |
23 | (resp) => { | |
24 | if (resp.status !== 401) { | |
25 | this.settings[url] = ''; | |
26 | } | |
27 | } | |
28 | ); | |
11fdf7f2 TL |
29 | } else if (setting !== '') { |
30 | fn(setting); | |
494da23a TL |
31 | } else { |
32 | if (elseFn) { | |
33 | elseFn(); | |
34 | } | |
11fdf7f2 TL |
35 | } |
36 | } | |
37 | ||
494da23a TL |
38 | // Easiest way to stop reloading external content that can't be reached |
39 | disableSetting(url) { | |
40 | this.settings[url] = ''; | |
41 | } | |
42 | ||
11fdf7f2 TL |
43 | private getSettingsValue(data: any): string { |
44 | return data.value || data.instance || ''; | |
45 | } | |
46 | ||
47 | validateGrafanaDashboardUrl(uid) { | |
48 | return this.http.get(`api/grafana/validation/${uid}`); | |
49 | } | |
50 | } |