2017-03-02 22:07:01 +00:00
|
|
|
import local from 'utilities/local';
|
|
|
|
import Request from 'kolide/request';
|
2016-10-05 14:03:55 +00:00
|
|
|
|
2016-09-16 13:55:46 +00:00
|
|
|
class Base {
|
|
|
|
constructor () {
|
2017-03-01 21:14:26 +00:00
|
|
|
const { origin } = global.window.location;
|
2016-09-16 13:55:46 +00:00
|
|
|
|
2016-09-20 22:36:36 +00:00
|
|
|
this.baseURL = `${origin}/api`;
|
|
|
|
this.bearerToken = local.getItem('auth_token');
|
2016-09-16 13:55:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 22:07:01 +00:00
|
|
|
static post (endpoint, body = {}, headers = {}) {
|
|
|
|
const { POST } = Request.REQUEST_METHODS;
|
2017-02-22 17:45:01 +00:00
|
|
|
|
2017-03-02 22:07:01 +00:00
|
|
|
return Base._request(POST, endpoint, body, headers);
|
2017-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 15:58:40 +00:00
|
|
|
static get (endpoint, overrideHeaders = {}) {
|
|
|
|
const { GET } = Request.REQUEST_METHODS;
|
|
|
|
return Base._request(GET, endpoint, {}, overrideHeaders);
|
|
|
|
}
|
|
|
|
|
2017-02-22 17:45:01 +00:00
|
|
|
static _deleteRequest (endpoint, headers) {
|
2017-03-02 22:07:01 +00:00
|
|
|
const { DELETE: method } = Request.REQUEST_METHODS;
|
|
|
|
const request = new Request({ endpoint, method, headers });
|
2017-02-22 17:45:01 +00:00
|
|
|
|
2017-03-02 22:07:01 +00:00
|
|
|
return request.send();
|
2017-02-22 17:45:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 22:07:01 +00:00
|
|
|
static _request (method, endpoint, body, headers) {
|
|
|
|
const { GET } = Request.REQUEST_METHODS;
|
2016-10-21 23:13:41 +00:00
|
|
|
const requestAttrs = method === GET
|
2017-03-02 22:07:01 +00:00
|
|
|
? { endpoint, method, headers }
|
|
|
|
: { endpoint, method, body, headers };
|
|
|
|
const request = new Request(requestAttrs);
|
2016-10-05 14:03:55 +00:00
|
|
|
|
2017-03-02 22:07:01 +00:00
|
|
|
return request.send();
|
2016-09-19 23:43:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 21:08:57 +00:00
|
|
|
_authenticatedHeaders = (headers) => {
|
|
|
|
return {
|
|
|
|
...headers,
|
2016-09-19 23:43:35 +00:00
|
|
|
Authorization: `Bearer ${this.bearerToken}`,
|
2016-09-20 13:42:08 +00:00
|
|
|
};
|
2016-10-14 21:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_authenticatedRequest(method, endpoint, body, overrideHeaders) {
|
|
|
|
const headers = this._authenticatedHeaders(overrideHeaders);
|
2016-09-19 23:43:35 +00:00
|
|
|
|
2016-10-21 23:13:41 +00:00
|
|
|
return Base._request(method, endpoint, body, headers);
|
2016-09-16 13:55:46 +00:00
|
|
|
}
|
2017-03-02 22:07:01 +00:00
|
|
|
|
|
|
|
_endpoint (pathname) {
|
|
|
|
return this.baseURL + pathname;
|
|
|
|
}
|
2016-09-16 13:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Base;
|