/
GrafanaHTTP.ts
161 lines (145 loc) · 5.4 KB
/
GrafanaHTTP.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {AbstractHTTP} from './AbstractHTTP';
import {OnmsError} from '../api/OnmsError';
import {OnmsHTTPOptions} from '../api/OnmsHTTPOptions';
import {OnmsResult} from '../api/OnmsResult';
import {OnmsServer} from '../api/OnmsServer';
import {log, catRest} from '../api/Log';
import {Category} from 'typescript-logging';
import * as clonedeep from 'lodash.clonedeep';
import {Util} from '../internal/Util';
/** @hidden */
const catGrafana = new Category('grafana', catRest);
/**
* Implementation of the [[IOnmsHTTP]] interface for Grafana.
* @module GrafanaHTTP
* @implements IOnmsHTTP
*/
export class GrafanaHTTP extends AbstractHTTP {
/**
* The Grafana backend object we'll use for making ReST calls.
* @hidden
*/
private backendSrv: any;
/**
* Construct a new GrafanaHTTP implementation.
* @constructor
* @param backendSrv - The Grafana BackendSrv object to use for requests.
* @param server - The OpenNMS server to make requests to.
*/
constructor(backendSrv: any, server?: OnmsServer) {
super(server, undefined);
this.backendSrv = backendSrv;
}
/** Make an HTTP GET call using the Grafana `BackendSrv`. */
public get(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
log.debug('GET ' + realUrl);
const query = this.getConfig(options);
query.method = 'GET';
query.url = realUrl;
return this.backendSrv.datasourceRequest(query).then((response) => {
let type = 'application/xml';
if (query && query.headers && query.headers.accept) {
type = query.headers.accept;
}
if (response.headers && response.headers['content-type']) {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
}).catch(this.handleError);
}
/** Make an HTTP PUT call using the Grafana `BackendSrv`. */
public put(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
log.debug('PUT ' + realUrl);
const query = this.getConfig(options);
query.method = 'PUT';
query.url = realUrl;
query.data = Object.apply({}, query.parameters);
return this.backendSrv.datasourceRequest(query).then((response) => {
let type = 'application/xml';
if (query && query.headers && query.headers.accept) {
type = query.headers.accept;
}
if (response.headers && response.headers['content-type']) {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
}).catch(this.handleError);
}
/** Make an HTTP POST call using the Grafana `BackendSrv`. */
public post(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
log.debug('POST ' + realUrl);
const query = this.getConfig(options);
query.method = 'POST';
query.url = realUrl;
return this.backendSrv.datasourceRequest(query).then((response) => {
let type = 'application/xml';
if (query && query.headers && query.headers.accept) {
type = query.headers.accept;
}
if (response.headers && response.headers['content-type']) {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
}).catch(this.handleError);
}
/** Make an HTTP DELETE call using the Grafana `BackendSrv`. */
public httpDelete(url: string, options?: OnmsHTTPOptions) {
const realUrl = this.getServer(options).resolveURL(url);
log.debug('DELETE ' + realUrl);
const query = this.getConfig(options);
query.method = 'DELETE';
query.url = realUrl;
return this.backendSrv.datasourceRequest(query).then((response) => {
let type = 'application/xml';
if (query && query.headers && query.headers.accept) {
type = query.headers.accept;
}
if (response.headers && response.headers['content-type']) {
type = response.headers['content-type'];
}
return OnmsResult.ok(response.data, undefined, response.status, type);
}).catch(this.handleError);
}
/**
* Internal method to turn [[OnmsHTTPOptions]] into a Grafana `BackendSrv` request object.
* @hidden
*/
private getConfig(options?: OnmsHTTPOptions): any {
const allOptions = this.getOptions(options);
const ret = {} as any;
if (allOptions.headers) {
ret.headers = clonedeep(allOptions.headers);
} else {
ret.headers = {};
}
if (!ret.headers.accept) {
ret.headers.accept = 'application/json';
}
if (!ret.headers['content-type']) {
ret.headers['content-type'] = 'application/json;charset=utf-8';
}
const type = ret.headers.accept;
if (type === 'application/json') {
ret.responseType = 'json';
ret.transformResponse = this.transformJSON;
} else if (type === 'text/plain') {
ret.responseType = 'text';
delete ret.transformResponse;
} else if (type === 'application/xml') {
ret.responseType = 'text';
ret.transformResponse = this.transformXML;
} else {
throw new OnmsError('Unhandled "Accept" header: ' + type);
}
if (allOptions.parameters && Object.keys(allOptions.parameters).length > 0) {
ret.params = Util.encodeParameters(clonedeep(allOptions.parameters));
}
if (allOptions.data) {
ret.data = clonedeep(allOptions.data);
}
return ret;
}
}