Skip to content

Commit

Permalink
Merge pull request elastic#8097 from ycombinator/fwd-port-8043
Browse files Browse the repository at this point in the history
Adding system API module
  • Loading branch information
ycombinator authored Sep 1, 2016
2 parents 48da824 + 3bdffb7 commit 9bff067
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ingest from './server/routes/api/ingest';
import search from './server/routes/api/search';
import settings from './server/routes/api/settings';
import scripts from './server/routes/api/scripts';
import * as systemApi from './server/lib/system_api';

module.exports = function (kibana) {
const kbnBaseUrl = '/app/kibana';
Expand Down Expand Up @@ -92,6 +93,8 @@ module.exports = function (kibana) {
search(server);
settings(server);
scripts(server);

server.expose('systemApi', systemApi);
}
});

Expand Down
22 changes: 22 additions & 0 deletions src/core_plugins/kibana/server/lib/__tests__/system_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import expect from 'expect.js';
import { isSystemApiRequest } from '../system_api';

describe('system_api', () => {
describe('#isSystemApiRequest', () => {
it ('returns true for a system API HTTP request', () => {
const mockHapiRequest = {
headers: {
'kbn-system-api': true
}
};
expect(isSystemApiRequest(mockHapiRequest)).to.be(true);
});

it ('returns false for a non-system API HTTP request', () => {
const mockHapiRequest = {
headers: {}
};
expect(isSystemApiRequest(mockHapiRequest)).to.be(false);
});
});
});
11 changes: 11 additions & 0 deletions src/core_plugins/kibana/server/lib/system_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const SYSTEM_API_HEADER_NAME = 'kbn-system-api';

/**
* Checks on the *server-side*, if an HTTP request is a system API request
*
* @param request HAPI request object
* @return true if request is a system API request; false, otherwise
*/
export function isSystemApiRequest(request) {
return !!request.headers[SYSTEM_API_HEADER_NAME];
}
6 changes: 6 additions & 0 deletions src/ui/public/chrome/directives/kbn_chrome.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import $ from 'jquery';
import { remove } from 'lodash';

import './kbn_chrome.less';
import UiModules from 'ui/modules';
import { isSystemApiRequest } from 'ui/system_api';

export default function (chrome, internals) {

Expand Down Expand Up @@ -41,6 +43,10 @@ export default function (chrome, internals) {
$rootScope.$on('$routeUpdate', onRouteChange);
onRouteChange();

const allPendingHttpRequests = () => $http.pendingRequests;
const removeSystemApiRequests = (pendingHttpRequests = []) => remove(pendingHttpRequests, isSystemApiRequest);
$scope.$watchCollection(allPendingHttpRequests, removeSystemApiRequests);

// and some local values
chrome.httpActive = $http.pendingRequests;
$scope.notifList = require('ui/notify')._notifs;
Expand Down
38 changes: 38 additions & 0 deletions src/ui/public/system_api/__tests__/system_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import expect from 'expect.js';
import ngMock from 'ng_mock';
import { addSystemApiHeader, isSystemApiRequest } from '../system_api';

describe('system_api', () => {
describe('#addSystemApiHeader', () => {
it ('adds the correct system API header', () => {
const headers = {
'kbn-version': '4.6.0'
};
const newHeaders = addSystemApiHeader(headers);

expect(newHeaders).to.have.property('kbn-system-api');
expect(newHeaders['kbn-system-api']).to.be(true);

expect(newHeaders).to.have.property('kbn-version');
expect(newHeaders['kbn-version']).to.be('4.6.0');
});
});

describe('#isSystemApiRequest', () => {
it ('returns true for a system API HTTP request', () => {
const mockRequest = {
headers: {
'kbn-system-api': true
}
};
expect(isSystemApiRequest(mockRequest)).to.be(true);
});

it ('returns false for a non-system API HTTP request', () => {
const mockRequest = {
headers: {}
};
expect(isSystemApiRequest(mockRequest)).to.be(false);
});
});
});
26 changes: 26 additions & 0 deletions src/ui/public/system_api/system_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const SYSTEM_API_HEADER_NAME = 'kbn-system-api';

/**
* Adds a custom header designating request as system API
* @param originalHeaders Object representing set of headers
* @return Object representing set of headers, with system API header added in
*/
export function addSystemApiHeader(originalHeaders) {
const systemApiHeaders = {
[SYSTEM_API_HEADER_NAME]: true
};
return {
...originalHeaders,
...systemApiHeaders
};
}

/**
* Returns true if request is a system API request; false otherwise
*
* @param request Object Request object created by $http service
* @return true if request is a system API request; false otherwise
*/
export function isSystemApiRequest(request) {
return !!request.headers[SYSTEM_API_HEADER_NAME];
}

0 comments on commit 9bff067

Please sign in to comment.