Created
March 9, 2018 14:18
-
-
Save xmanemran/b31bce1d2fe1530c55a965c736a74151 to your computer and use it in GitHub Desktop.
code sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import url from './url'; | |
import store from '../store'; | |
import {AsyncStorage, Alert} from 'react-native'; | |
import {client_id, client_secret} from "../settings/constent"; | |
import {credential, loadingStart, loadingEnd} from '../actions/io' | |
import {userData} from "../actions/app"; | |
export default { | |
post, | |
login, | |
get, | |
profile, | |
logout, | |
requestForToken, | |
deleteRq | |
} | |
function login(username, password){ | |
let options = { | |
method: 'POST', | |
body: preFromData({ | |
client_id, | |
client_secret, | |
grant_type: 'password', | |
username, | |
password | |
}) | |
}; | |
return processCredential(options) | |
} | |
function requestForToken(refresh_token){ | |
let options = { | |
method: 'POST', | |
body: preFromData({ | |
client_id, | |
client_secret, | |
grant_type: 'refresh_token', | |
refresh_token | |
}) | |
}; | |
return processCredential(options) | |
} | |
function processCredential(options){ | |
return fetchData(url.LOGIN, options) | |
.then(({access_token, refresh_token, token_type})=>{ | |
store.dispatch(credential({access_token, refresh_token, token_type})); | |
AsyncStorage.setItem('refresh_token', refresh_token); | |
return true; | |
}); | |
} | |
function logout(){ | |
let {access_token, token_type} = store.getState().io; | |
let options = { | |
method: 'POST', | |
body: preFromData({ | |
client_id, | |
client_secret, | |
token: access_token, | |
}) | |
}; | |
return fetchData(url.LOGOUT, options) | |
.then((data)=>console.log('LOGOUT', data)) | |
} | |
function profile(){ | |
return get(url.PROFILE) | |
.then(({data})=>{ | |
store.dispatch(userData(data)); | |
return data.id; | |
}) | |
} | |
function get(url, header = {}){ | |
let {access_token, token_type} = store.getState().io; | |
let options = { | |
url: url, | |
method: 'GET', | |
headers: {'Authorization': `${token_type} ${access_token}`, ...header} | |
}; | |
return fetchData(url, options) | |
.then(data=>data.response) | |
} | |
function post(url, body = {}, isJson = false){ | |
let {access_token, token_type} = store.getState().io; | |
let options = { | |
body: isJson ? JSON.stringify({...body}) : preFromData({ | |
...defaultPostBody, | |
...body | |
}), | |
headers: {'Authorization': `${token_type} ${access_token}`}, | |
method: 'POST' | |
}; | |
if(isJson) | |
options.headers = {...options.headers, 'Content-Type': 'application/json'}; | |
return fetchData(url, options) | |
.then(data=>{ | |
if(data.response) | |
return data.response; | |
else | |
return data; | |
}) | |
} | |
function deleteRq(url){ | |
let {access_token, token_type} = store.getState().io; | |
let options = { | |
method: 'DELETE', | |
headers: {'Authorization': `${token_type} ${access_token}`}, | |
}; | |
return fetchData(url, options) | |
} | |
function fetchData(url, options){ | |
store.dispatch(loadingStart()); | |
console.log('OPTIONS', options); | |
console.log('URL', url); | |
return fetch(url, options) | |
.then(response=>{ | |
console.log('RESPONSE', response); | |
return response; | |
}) | |
.then(res=>checkStatus(res)) | |
.then(res=>res.json()) | |
.then((data)=>{ | |
store.dispatch(loadingEnd()); | |
return data | |
}) | |
.catch((err)=>{ | |
store.dispatch(loadingEnd()); | |
console.log(err); | |
// return err | |
}) | |
} | |
let defaultPostBody = {}; | |
function defaultHeader (){ | |
// let loginState = store.getState().app.login; | |
// return { | |
// 'Authentication': `Bearer ${loginState.access_token}` | |
// } | |
} | |
function checkStatus(res){ | |
if(res.status === 401 || res.status === 400){ | |
Alert.alert('Error', 'Invalid login information'); | |
AsyncStorage.removeItem('refresh_token'); | |
return false | |
} | |
if(res.status < 200 || res.status > 299 ) { | |
Alert.alert('Error', 'Unable to process: ' + res.status); | |
return false | |
} | |
return res; | |
} | |
function preHeaderData(obj){ | |
return keyValuePushIntoObj(new Headers(), obj) | |
} | |
function preFromData(obj){ | |
return keyValuePushIntoObj(new FormData(), obj) | |
} | |
function keyValuePushIntoObj(pushTo, obj){ | |
Object.keys(obj).forEach((key)=>{ | |
pushTo.append(key, obj[key]); | |
}); | |
return pushTo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment