simplified api code

This commit is contained in:
Ulf Gebhardt 2021-02-26 21:23:06 +01:00
parent a717edb4b0
commit 94d5dcb778
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
2 changed files with 51 additions and 93 deletions

View File

@ -4,40 +4,42 @@ import axios from 'axios';
const COMMUNITY_API_STATE_BALANCE_URL = 'http://localhost/state-balances/' const COMMUNITY_API_STATE_BALANCE_URL = 'http://localhost/state-balances/'
const COMMUNITY_API_TRANSACTION_CREATION_URL = 'http://localhost/transaction-creations/' const COMMUNITY_API_TRANSACTION_CREATION_URL = 'http://localhost/transaction-creations/'
const apiGet = async (url) => {
try {
const result = await axios.get(url);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state !== 'success'){
throw new Error(result.data.msg)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
}
const apiPost = async (url, payload) => {
try {
const result = await axios.post(url, payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state !== 'success'){
throw new Error(result.data.msg)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
}
const communityAPI = { const communityAPI = {
balance: async (session_id) => { balance: async (session_id) => {
try { return apiGet(COMMUNITY_API_STATE_BALANCE_URL + 'ajaxGetBalance/' + session_id)
const result = await axios.get(COMMUNITY_API_STATE_BALANCE_URL + 'ajaxGetBalance/' + session_id);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.msg)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
}, },
transactions: async (session_id) => { transactions: async (session_id) => {
try { return apiGet(COMMUNITY_API_STATE_BALANCE_URL + 'ajaxListTransactions/' + session_id)
const result = await axios.get(COMMUNITY_API_STATE_BALANCE_URL + 'ajaxListTransactions/' + session_id);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.msg)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
}, },
create: async (session_id, email, amount, memo, target_date = new Date() ) => { create: async (session_id, email, amount, memo, target_date = new Date() ) => {
const payload = { const payload = {
@ -48,21 +50,7 @@ const communityAPI = {
memo, memo,
auto_sign: true auto_sign: true
} }
try { return apiPost(COMMUNITY_API_TRANSACTION_CREATION_URL + 'ajaxCreate/', payload)
const result = await axios.post(COMMUNITY_API_TRANSACTION_CREATION_URL + 'ajaxCreate/', payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.msg)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
} }
} }

View File

@ -9,45 +9,32 @@ const EMAIL_TYPE = {
ADMIN: 5, // if user was registered by an admin ADMIN: 5, // if user was registered by an admin
} }
const apiPost = async (url, payload) => {
try {
const result = await axios.post(url, payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state !== 'success'){
throw new Error(result.data.msg)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
}
const loginAPI = { const loginAPI = {
login: async (email, password) => { login: async (email, password) => {
const payload = { const payload = {
email, email,
password, password,
} }
try { return apiPost(LOGIN_API_URL + 'unsecureLogin', payload)
const result = await axios.post(LOGIN_API_URL + 'unsecureLogin', payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.msg)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
}, },
logout: async (session_id) => { logout: async (session_id) => {
const payload= { session_id } const payload= { session_id }
try { return apiPost(LOGIN_API_URL + 'logout', payload)
const result = await axios.post(LOGIN_API_URL + 'logout', payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.details)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
}, },
create : async (email, first_name, last_name, password) => { create : async (email, first_name, last_name, password) => {
const payload = { const payload = {
@ -58,24 +45,7 @@ const loginAPI = {
emailType: EMAIL_TYPE.DEFAULT, emailType: EMAIL_TYPE.DEFAULT,
login_after_register: true login_after_register: true
} }
try { return apiPost(LOGIN_API_URL + 'createUser', payload)
const result = await axios.post(LOGIN_API_URL + 'createUser', payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.details)
}
if(result.data.state === 'exists'){
throw new Error(result.data.msg)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
}, },
} }