73 lines
No EOL
2 KiB
JavaScript
73 lines
No EOL
2 KiB
JavaScript
import axios from "axios";
|
|
import analyticsMixIn from "@/mixins/analytics-mixin.js";
|
|
import { useMainStore } from "@/store";
|
|
|
|
import { applicationConfig } from "@/constants/application-config.js";
|
|
import { GaCategories, GaActions, GaLabels } from "@/constants/analytics";
|
|
import { headerKeys } from "@/constants/header-keys";
|
|
|
|
export default {
|
|
callHttpClient({ method, endpoint, payload, logApiCall = true}) {
|
|
return new Promise((resolve, reject) => {
|
|
const store = useMainStore();
|
|
const cfDistroUrl = applicationConfig.CONSUMER_CF_DISTRO;
|
|
const payloadAndAnalyticsData = Object.assign({}, payload, { AppName: "SelfService" });
|
|
const headers = {
|
|
[headerKeys.EXPERIMENT]: JSON.stringify(store.experimentSettings),
|
|
};
|
|
|
|
axios({
|
|
method: method,
|
|
url: cfDistroUrl + endpoint,
|
|
data: payloadAndAnalyticsData,
|
|
crossDomain: true,
|
|
responseType: 'json',
|
|
headers: headers,
|
|
})
|
|
.then((response) => {
|
|
if (logApiCall) {
|
|
analyticsMixIn.methods.pushEventToGA(
|
|
GaCategories.API_RESPONSE,
|
|
GaActions.RESULT,
|
|
`${GaLabels.SUCCESS}_${endpoint}`,
|
|
true
|
|
);
|
|
}
|
|
return resolve(response);
|
|
},
|
|
error => {
|
|
console.error(error);
|
|
|
|
//implement if analytics service is down
|
|
if(endpoint.includes("analytics")){
|
|
return resolve({data: ""});
|
|
}
|
|
|
|
return reject(error.response);
|
|
}
|
|
);
|
|
});
|
|
},
|
|
|
|
//used for mocked services
|
|
async mockCallHttpClient(method, endpoint) {
|
|
return new Promise((resolve, reject) => {
|
|
axios({
|
|
method: method,
|
|
url: endpoint,
|
|
crossDomain: true,
|
|
responseType: {}
|
|
})
|
|
.then((response) => {
|
|
return resolve(response);
|
|
},
|
|
error => {
|
|
console.error(error);
|
|
return reject(error.response);
|
|
}
|
|
);
|
|
});
|
|
|
|
|
|
}
|
|
}; |