mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
simplified api code
This commit is contained in:
parent
a717edb4b0
commit
94d5dcb778
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user