-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.js
46 lines (38 loc) · 1.24 KB
/
API.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* @flow */
import axios from 'axios';
import * as APITypes from './APITypes';
export class APICallerClass {
__extraHeaders : {[string]: string} = {
Authorization: '',
'X-CSRFToken': '',
};
__makeRequest(
requestMethod: APITypes.RequestMethod,
endpoint: string,
params: Object,
success: (any) => any,
failure?: (APITypes.ErrorType) => any) {
if (params.headers === undefined)
params.headers = {};
params.headers = {...params.headers, ...this.__extraHeaders};
axios({
method: requestMethod,
url: endpoint,
...params,
}).then((ret: Object) => {
success(ret.data);
}).catch((err: APITypes.ErrorType) => {
if (failure)
failure(err);
});
}
setCSRFToken (token: string) {
this.__extraHeaders['X-CSRFToken'] = token;
}
setToken(token: string) {
this.__extraHeaders['Authorization'] = 'Token ' + token;
}
requestToken(success: (APITypes.CreatedToken) => any, failure?: (APITypes.ErrorType) => any, requestData?: Object) {
this.__makeRequest('post', '/api/tokens/', {data: requestData}, success, failure);
}
};