Refactor API logic

This commit is contained in:
Jake Howard 2016-05-05 18:51:31 +01:00
parent 18d5f8c20e
commit 84806baece
2 changed files with 7 additions and 15 deletions

View file

@ -1,6 +1,5 @@
import endpoints from './endpoints';
import request from './request';
import token from './token';
function JSONify(response) {
return response.json();
@ -15,24 +14,16 @@ export async function checkToken(possibleToken) {
}
export async function getUserDetails() {
const CIToken = await token.get();
const url = endpoints.get('USER_INFO');
return await request(url, GET, {}, CIToken).then(JSONify);
return await request(url, GET).then(JSONify);
}
export async function getProjects() {
const CIToken = await token.get();
const url = endpoints.get('ALL_PROJECTS');
return await request(url, GET, {}, CIToken).then(JSONify);
return await request(url, GET).then(JSONify);
}
export async function getProjectRecentBuilds(user, repo, limit = 1, offset = 0) {
const CIToken = await token.get();
const url = endpoints.get('PROJECT_RECENTS').param({ user, repo }).query({ limit, offset });
return await request(
url,
GET,
{},
CIToken)
.then(JSONify);
return await request(url, GET).then(JSONify);
}

View file

@ -1,7 +1,7 @@
import token from './token';
export default function request(url, method, data = {}, CIToken) {
CIToken = CIToken || token.get();
export default async function request(url, method = 'GET', data = {}, CIToken) {
CIToken = CIToken || await token.get();
const fullURL = url.query({ 'circle-token': CIToken }).toString();
let fetchParams = {
method,
@ -11,6 +11,7 @@ export default function request(url, method, data = {}, CIToken) {
};
if (method !== 'GET' && method !== 'HEAD') {
fetchParams.body = JSON.stringify(data);
fetchParams.headers['Content-Type'] = 'application/json';
}
return fetch(fullURL, fetchParams);
return await fetch(fullURL, fetchParams);
}