From 94d5dcb77819b37c59f878b68019d70a2cd0a6d0 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 26 Feb 2021 21:23:06 +0100 Subject: [PATCH] simplified api code --- frontend/src/apis/communityAPI.js | 78 +++++++++++++------------------ frontend/src/apis/loginAPI.js | 66 +++++++------------------- 2 files changed, 51 insertions(+), 93 deletions(-) diff --git a/frontend/src/apis/communityAPI.js b/frontend/src/apis/communityAPI.js index e0c298842..15284a663 100644 --- a/frontend/src/apis/communityAPI.js +++ b/frontend/src/apis/communityAPI.js @@ -4,40 +4,42 @@ import axios from 'axios'; const COMMUNITY_API_STATE_BALANCE_URL = 'http://localhost/state-balances/' 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 = { balance: async (session_id) => { - try { - 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} - } + return apiGet(COMMUNITY_API_STATE_BALANCE_URL + 'ajaxGetBalance/' + session_id) }, transactions: async (session_id) => { - try { - 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} - } + return apiGet(COMMUNITY_API_STATE_BALANCE_URL + 'ajaxListTransactions/' + session_id) }, create: async (session_id, email, amount, memo, target_date = new Date() ) => { const payload = { @@ -48,21 +50,7 @@ const communityAPI = { memo, auto_sign: true } - try { - 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} - } + return apiPost(COMMUNITY_API_TRANSACTION_CREATION_URL + 'ajaxCreate/', payload) } } diff --git a/frontend/src/apis/loginAPI.js b/frontend/src/apis/loginAPI.js index 2f728442d..ef0d35045 100644 --- a/frontend/src/apis/loginAPI.js +++ b/frontend/src/apis/loginAPI.js @@ -9,45 +9,32 @@ const EMAIL_TYPE = { 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 = { login: async (email, password) => { const payload = { email, password, } - try { - 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} - } + return apiPost(LOGIN_API_URL + 'unsecureLogin', payload) }, logout: async (session_id) => { const payload= { session_id } - try { - 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} - } + return apiPost(LOGIN_API_URL + 'logout', payload) }, create : async (email, first_name, last_name, password) => { const payload = { @@ -58,24 +45,7 @@ const loginAPI = { emailType: EMAIL_TYPE.DEFAULT, login_after_register: true } - try { - 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} - } + return apiPost(LOGIN_API_URL + 'createUser', payload) }, }