Write CircleCI api myself

This commit is contained in:
Jake Howard 2016-02-06 12:42:19 +00:00
parent b718ddba33
commit 9974d4af91
5 changed files with 47 additions and 3 deletions

9
app/api/CircleCI.js Normal file
View file

@ -0,0 +1,9 @@
import endpoints from './endpoints';
import request from './request';
export function checkToken(token) {
const url = endpoints.get('USER_INFO');
return request(url, 'GET', {}, token).then(function (response) {
return response.ok;
});
}

8
app/api/endpoints.js Normal file
View file

@ -0,0 +1,8 @@
import { Map } from 'immutable';
import UrlAssembler from 'url-assembler';
const URL_BASE = UrlAssembler('https://circleci.com/api/v1');
export default Map({
USER_INFO: URL_BASE.segment('/me')
});

15
app/api/request.js Normal file
View file

@ -0,0 +1,15 @@
const TOKEN = '';
export default function request(url, method, data = {}, token = TOKEN ) {
const fullURL = url.query({ 'circle-token': token }).toString();
let fetchParams = {
method,
headers: {
'Accept': 'application/json',
},
};
if (method !== 'GET' && method !== 'HEAD') {
fetchParams.body = JSON.stringify(data);
}
return fetch(fullURL, fetchParams);
}

11
app/api/token.js Normal file
View file

@ -0,0 +1,11 @@
import { AsyncStorage } from 'react-native';
const STORAGE_KEY = 'CIRCLE_TOKEN';
export async function get() {
return await AsyncStorage.getItem(STORAGE_KEY);
}
export async function set(token) {
return await AsyncStorage.setItem(STORAGE_KEY, token);
}

View file

@ -8,13 +8,13 @@
"test": "npm run lint" "test": "npm run lint"
}, },
"dependencies": { "dependencies": {
"circleci": "=0.3.1",
"immutable": "=3.7.6", "immutable": "=3.7.6",
"react-native": "=0.19.0", "react-native": "=0.19.0",
"react-native-modalbox": "=1.2.8", "react-native-modalbox": "=1.2.8",
"react-native-side-menu": "=0.18.0", "react-native-side-menu": "=0.18.0",
"react-native-vector-icons": "=1.0.4", "react-native-vector-icons": "=1.0.4",
"underscore": "=1.8.3" "underscore": "=1.8.3",
"url-assembler": "=1.2.4"
}, },
"devDependencies": { "devDependencies": {
"babel-eslint": "=4.1.7", "babel-eslint": "=4.1.7",
@ -23,6 +23,7 @@
"eslint-plugin-react": "=3.14.0", "eslint-plugin-react": "=3.14.0",
"eslint-plugin-react-native": "=0.5.0", "eslint-plugin-react-native": "=0.5.0",
"react": "=0.14.6", "react": "=0.14.6",
"react-addons-test-utils": "=0.14.6" "react-addons-test-utils": "=0.14.6",
"url": "=0.11.0"
} }
} }