This repository was archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
API Access
Nico Hauser edited this page Jan 9, 2017
·
2 revisions
When sending requests, the easiest way is to use json. A helper function may look like the follwing:
const fetchApi = (path = "/", method = "GET", params = {}, accessToken = null, version = API_VERSION) => {
return new Promise((resolve, reject) => {
let http = new XMLHttpRequest();
http.open(method, API_URL + (version ? "/v" + API_VERSION : '') + "/" + path, true);
if(accessToken !== null){
http.setRequestHeader("Authorization", "Bearer " + accessToken);
}
http.onreadystatechange = () => {
if(http.readyState === 4){
if(http.status === 200){
resolve(
JSON.parse(http.responseText)
);
}else{
reject(Error("Error Code: " + http.status));
}
}
}
http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
http.send(JSON.stringify(params));
});
}
Note that this function uses ES6 features such as default parameters and promises, which aren't supported in all browsers yet. You may use a tool like babel in order to make this work all browsers.