From 9974d4af912475369485fe0a8197e61cc91ba9fe Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 6 Feb 2016 12:42:19 +0000 Subject: [PATCH] Write CircleCI api myself --- app/api/CircleCI.js | 9 +++++++++ app/api/endpoints.js | 8 ++++++++ app/api/request.js | 15 +++++++++++++++ app/api/token.js | 11 +++++++++++ package.json | 7 ++++--- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 app/api/CircleCI.js create mode 100644 app/api/endpoints.js create mode 100644 app/api/request.js create mode 100644 app/api/token.js diff --git a/app/api/CircleCI.js b/app/api/CircleCI.js new file mode 100644 index 0000000..51ad6a5 --- /dev/null +++ b/app/api/CircleCI.js @@ -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; + }); +} diff --git a/app/api/endpoints.js b/app/api/endpoints.js new file mode 100644 index 0000000..eec93f4 --- /dev/null +++ b/app/api/endpoints.js @@ -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') +}); diff --git a/app/api/request.js b/app/api/request.js new file mode 100644 index 0000000..48da8a3 --- /dev/null +++ b/app/api/request.js @@ -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); +} diff --git a/app/api/token.js b/app/api/token.js new file mode 100644 index 0000000..858a5c3 --- /dev/null +++ b/app/api/token.js @@ -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); +} diff --git a/package.json b/package.json index bea0596..8957d33 100644 --- a/package.json +++ b/package.json @@ -8,13 +8,13 @@ "test": "npm run lint" }, "dependencies": { - "circleci": "=0.3.1", "immutable": "=3.7.6", "react-native": "=0.19.0", "react-native-modalbox": "=1.2.8", "react-native-side-menu": "=0.18.0", "react-native-vector-icons": "=1.0.4", - "underscore": "=1.8.3" + "underscore": "=1.8.3", + "url-assembler": "=1.2.4" }, "devDependencies": { "babel-eslint": "=4.1.7", @@ -23,6 +23,7 @@ "eslint-plugin-react": "=3.14.0", "eslint-plugin-react-native": "=0.5.0", "react": "=0.14.6", - "react-addons-test-utils": "=0.14.6" + "react-addons-test-utils": "=0.14.6", + "url": "=0.11.0" } }