130 lines
5.3 KiB
JavaScript
130 lines
5.3 KiB
JavaScript
import { Alert, Platform } from 'react-native';
|
|
import RNLocation from 'react-native-location';
|
|
import EP from './endpoints.js';
|
|
import DeviceInfo from 'react-native-device-info';
|
|
import Environment from '../environment.js';
|
|
import axios from 'axios';
|
|
import DB from '../storage';
|
|
|
|
export default async function API(endpoint, method, headers, params, body, onSuccess, onError, cancelToken) {
|
|
const SESSION = await DB.session();
|
|
|
|
let deviceID = DeviceInfo.getDeviceId();
|
|
let versionNumb = DeviceInfo.getVersion();
|
|
let OS = Platform.OS === "android" ? "Android" : "iOS";
|
|
let appBundleID = OS === 'android' ? "com.project.yondu.unioilloyaltyapp" : "com.rnunioilloyaltyapp";
|
|
let versionRelease = DeviceInfo.getBuildNumber();
|
|
let location = await RNLocation.getLatestLocation(latestLocation => latestLocation);
|
|
|
|
const urlWithCustomHeaders = ["getFunding", "postClaim", "postpay", "addCreditCard"];
|
|
|
|
try {
|
|
if(method == "post") {
|
|
let Head = {
|
|
'Authorization': `${SESSION.token}`
|
|
}
|
|
|
|
let url = endpoint.includes(":") ? endpoint : EP[endpoint];
|
|
|
|
if(Object.keys(params).length > 0) {
|
|
let paramsItem = Object.entries(params).map(([key, value]) => `${key}=${value}`)
|
|
url = paramsItem.length > 1 ? `${EP[endpoint]}?${paramsItem.join('&').toString()}` : `${EP[endpoint]}/${Object.entries(params).map(([key, value]) => value).join('').toString()}`
|
|
}
|
|
|
|
if(endpoint === "getFunding") {
|
|
body.device_id = deviceID;
|
|
}
|
|
|
|
if(urlWithCustomHeaders.includes(endpoint)) {
|
|
Head = {
|
|
...Head,
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
'x-p97-tenantid': Environment.POST_PAY_TENANT_ID,
|
|
'x-p97-apikey': Environment.POST_PAY_API_KEY,
|
|
'x-p97-latitude': location?.latitude?.toString(),
|
|
'x-p97-longitude': location?.longitude?.toString(),
|
|
'x-p97-deviceid': deviceID,
|
|
'x-p97-appversionnumber': versionNumb,
|
|
'x-p97-appbundleid': appBundleID,
|
|
'x-p97-os': OS,
|
|
'x-p97-clientosversion': versionRelease
|
|
}
|
|
}
|
|
|
|
let response = await axios({
|
|
url: url,
|
|
method: method,
|
|
data: body,
|
|
params: params,
|
|
headers: Head,
|
|
cancelToken: cancelToken?.token
|
|
})
|
|
onSuccess(response.data)
|
|
} else if(method == "get") {
|
|
let url;
|
|
|
|
let Head = {
|
|
'Authorization': `${SESSION.token}`
|
|
}
|
|
|
|
if(endpoint.includes("/")) {
|
|
const newURL = endpoint.split("/");
|
|
url = newURL[0].includes(":") ? newURL[0] : EP[newURL[0]];
|
|
url = `${url}/${newURL[1]}`;
|
|
} else if(endpoint === "getTransactionStatusDetails") {
|
|
url = EP[endpoint] + `/${params.transactionID}`
|
|
} else if(Object.keys(params).length > 0) {
|
|
let paramsItem = Object.entries(params).map(([key, value]) => `${key}=${value}`)
|
|
url = paramsItem.length > 1 ? `${EP[endpoint]}?${paramsItem.join('&').toString()}` : `${EP[endpoint]}/${Object.entries(params).map(([key, value]) => value).join('').toString()}`
|
|
} else {
|
|
url = EP[endpoint]
|
|
}
|
|
|
|
if(urlWithCustomHeaders.includes(endpoint)) {
|
|
Head = {
|
|
...Head,
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
'x-p97-tenantid': Environment.POST_PAY_TENANT_ID,
|
|
'x-p97-apikey': Environment.POST_PAY_API_KEY,
|
|
'x-p97-latitude': location?.latitude?.toString(),
|
|
'x-p97-longitude': location?.longitude?.toString(),
|
|
'x-p97-deviceid': deviceID,
|
|
'x-p97-appversionnumber': versionNumb,
|
|
'x-p97-appbundleid': appBundleID,
|
|
'x-p97-os': OS,
|
|
'x-p97-clientosversion': versionRelease
|
|
}
|
|
}
|
|
|
|
let response = await axios({
|
|
url: url,
|
|
method: method,
|
|
headers: Head,
|
|
cancelToken: cancelToken?.token
|
|
})
|
|
onSuccess(response.data)
|
|
} else if(method == "delete") {
|
|
let url = !params.noID ? (EP[endpoint] + "?" + params) : EP[endpoint] + "/" + params.value
|
|
if(headers.Authorization){
|
|
let response = await axios({
|
|
url: endpoint.includes(":") ? endpoint : url,
|
|
method: method,
|
|
headers: new Headers({
|
|
'Accept': 'application/json',
|
|
'Authorization': `${headers.Authorization || ''}`
|
|
})
|
|
})
|
|
onSuccess(response.data)
|
|
} else {
|
|
let response = await axios({
|
|
url: endpoint.includes(":") ? endpoint : url,
|
|
})
|
|
onSuccess(response.data)
|
|
}
|
|
}
|
|
} catch (error) {
|
|
onError(error)
|
|
}
|
|
} |