Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for headers both as function/object and replaced moment with dateTime #64

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change Log

## [0.0.7-dev]

- Updating dependencies
6 changes: 5 additions & 1 deletion src/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ export const examples = [
url: 'https://httpbin.org/basic-auth/user/pass',
withCredentials: true,
params_js: '{}',
header_js: '{\n' + " Authorization: 'Basic ' + btoa('user' + ':' + 'pass')\n" + "// Authorization: 'Basic dXNlcjpwYXNz'\n" + '}',
header_js:
'{\n' +
" Authorization: 'Basic ' + btoa('user' + ':' + 'pass')\n" +
"// Authorization: 'Basic dXNlcjpwYXNz'\n" +
'}',
},
},
{
Expand Down
19 changes: 13 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { MetricsPanelCtrl } from 'grafana/app/plugins/sdk';
import $ from 'jquery';
import _ from 'lodash';
import appEvents from 'grafana/app/core/app_events';
import moment from 'moment';
import './style.css';
import { DSInfo, RenderMode } from './types';
import { examples } from './examples';

import { DataQueryResponse, DataSourceApi } from '@grafana/data';
import { DataQueryResponse, DataSourceApi, dateTime } from '@grafana/data';

class AjaxCtrl extends MetricsPanelCtrl {
static templateUrl = 'partials/module.html';
Expand Down Expand Up @@ -473,6 +472,14 @@ class AjaxCtrl extends MetricsPanelCtrl {
}

process(rsp: any) {
const headers = (key: any) => {
if (typeof rsp.headers === 'function') {
return key != null ? rsp.headers(key) : rsp.headers();
}

return key != null ? rsp.headers[key] : rsp.headers;
};

if (this.panel.showTime) {
let txt: string = this.panel.showTimePrefix ? this.panel.showTimePrefix : '';
if (this.panel.showTimeValue) {
Expand All @@ -483,20 +490,20 @@ class AjaxCtrl extends MetricsPanelCtrl {
when = Date.now();
} else if (this.panel.showTimeValue.startsWith('header-')) {
const h = this.panel.showTimeValue.substring('header-'.length);
const v = rsp.headers[h];
const v = headers(h);
if (v) {
console.log('TODO, parse header', v, h);
} else {
const vals: any = {};
for (const key in rsp.headers()) {
vals[key] = rsp.headers[key];
for (const key in headers()) {
vals[key] = headers(key);
}
console.log('Header:', h, 'not found in:', vals, rsp);
}
}

if (when) {
txt += moment(when).format(this.panel.showTimeFormat);
txt += dateTime(when).format(this.panel.showTimeFormat);
} else {
txt += 'missing: ' + this.panel.showTimeValue;
}
Expand Down