Skip to content

Commit 7864620

Browse files
committed
Add custom params to API
1 parent 8191777 commit 7864620

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/DataSource.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ import { JsonApiQuery, JsonApiVariableQuery, JsonApiDataSourceOptions } from './
1717

1818
export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOptions> {
1919
api: API;
20-
queryParams: string;
2120

2221
constructor(instanceSettings: DataSourceInstanceSettings<JsonApiDataSourceOptions>) {
2322
super(instanceSettings);
24-
this.api = new API(instanceSettings.url!);
25-
this.queryParams = instanceSettings.jsonData.queryParams || '';
23+
this.api = new API(instanceSettings.url!, instanceSettings.jsonData.queryParams || '');
2624
}
2725

2826
async query(request: DataQueryRequest<JsonApiQuery>): Promise<DataQueryResponse> {
2927
const promises = request.targets.map(async query => {
30-
const response = await this.api.cachedGet(query.cacheDurationSeconds, this.queryParams);
28+
const response = await this.api.cachedGet(query.cacheDurationSeconds);
3129

3230
const fields = query.fields
3331
.filter(field => field.jsonPath)
@@ -70,7 +68,9 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
7068
if (!query.jsonPath) {
7169
return [];
7270
}
73-
return JSONPath({ path: query.jsonPath, json: await this.api.get() }).map((_: any) => ({ text: _ }));
71+
return JSONPath({ path: query.jsonPath, json: await this.api.get() }).map((_: any) => ({
72+
text: _,
73+
}));
7474
}
7575

7676
/**
@@ -80,7 +80,7 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
8080
const defaultErrorMessage = 'Cannot connect to API';
8181

8282
try {
83-
const response = await this.api.test(this.queryParams);
83+
const response = await this.api.test();
8484
if (response.status === 200) {
8585
return {
8686
status: 'success',

src/api.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import cache from 'memory-cache';
44
export default class Api {
55
cache: any;
66
baseUrl: string;
7+
params: string;
78
lastCacheDuration: number | undefined;
89

9-
constructor(baseUrl: string) {
10+
constructor(baseUrl: string, params: string) {
1011
this.baseUrl = baseUrl;
12+
this.params = params;
1113
this.cache = new cache.Cache();
1214
}
1315

1416
/**
1517
* Queries the API and returns the response data.
1618
*/
17-
async get(params?: string) {
19+
async get() {
1820
const req = {
19-
url: `${this.baseUrl}${params?.length ? `?${params}` : ''}`,
21+
url: `${this.baseUrl}${this.params.length ? `?${this.params}` : ''}`,
2022
method: 'GET',
2123
};
2224

@@ -28,9 +30,9 @@ export default class Api {
2830
/**
2931
* Used as a health check.
3032
*/
31-
async test(params?: string) {
33+
async test() {
3234
const req = {
33-
url: `${this.baseUrl}${params?.length ? `?${params}` : ''}`,
35+
url: `${this.baseUrl}${this.params.length ? `?${this.params}` : ''}`,
3436
method: 'GET',
3537
};
3638
return getBackendSrv().datasourceRequest(req);
@@ -41,7 +43,7 @@ export default class Api {
4143
*/
4244
async cachedGet(cacheDurationSeconds: number, params?: string) {
4345
if (cacheDurationSeconds === 0) {
44-
return await this.get(params);
46+
return await this.get();
4547
}
4648

4749
const force = this.lastCacheDuration !== cacheDurationSeconds;
@@ -56,7 +58,7 @@ export default class Api {
5658
}
5759
this.lastCacheDuration = cacheDurationSeconds;
5860

59-
const result = await this.get(params);
61+
const result = await this.get();
6062

6163
this.cache.put(this.baseUrl, result, Math.max(cacheDurationSeconds * 1000, 1));
6264

0 commit comments

Comments
 (0)