In plugin.json I’ve added the following:
"routes": [
{
"path": "local",
"url": "http://localhost:1337",
"method": "GET",
"headers":
[
{
"name": "Authorization",
"content": "<my Authorization token>"
}
]
}
],
Aside from the boilerplate code added by grafana-toolkit I’ve added a call to getBackendSrv().fetch() by folowing the examples on the add authentication for data source plugins page
here’s what I’ve got in datasources.ts:
import defaults from 'lodash/defaults';
import {
DataQueryRequest,
DataQueryResponse,
DataSourceApi,
DataSourceInstanceSettings,
MutableDataFrame,
FieldType,
} from '@grafana/data';
import { getBackendSrv } from '@grafana/runtime'
import { MyQuery, MyDataSourceOptions, defaultQuery } from './types';
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
url?: string;
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
this.url = instanceSettings.url;
}
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
const routePath = '/local';
const { range } = options;
const from = range!.from.valueOf();
const to = range!.to.valueOf();
getBackendSrv().fetch({
url: this.url + routePath,
method: 'GET',
params: {
"start": from,
"end": to,
"params": "myParam"
}
}).subscribe(response => {
console.log(response)
})
// Return a constant for each query.
const data = options.targets.map((target) => {
const query = defaults(target, defaultQuery);
return new MutableDataFrame({
refId: query.refId,
fields: [
{ name: 'Time', values: [from, to], type: FieldType.time },
{ name: 'Value', values: [query.constant, query.constant], type: FieldType.number },
],
});
});
return { data };
}
async testDatasource() {
// Implement a health check for your data source.
return {
status: 'success',
message: 'Success',
};
}
}
I keep getting 502 bad gateway error. It’s proving tricky to debug. please advise.