import axios from 'axios';

const getAxios = () => {
    if (process.env.API_URL) {
        console.log("API_URL is", process.env.API_URL);
        return axios.create({
            baseURL: process.env.API_URL
        });
    } else {
        return axios;
    }
}

function callApi(url, method = 'get', data, shibbolethId) {
    const header = {
        headers: {
            "shibbolointiId": shibbolethId,
        },
    }
    switch (method) {
        case "get":
            return getAxios().get(url, header);
        case "post":
            return getAxios().post(url, data, header);
        case "put":
            return getAxios().put(url, data, header);
        case "delete":
            return getAxios().delete(url, header);
        default:
            console.error("Invalid http method");
    }
}

export const callController = (route, prefix, data, method = 'get') => dispatch => {
    const payload = {
        route,
        method,
        data,
        prefix
    }
    dispatch({ type: prefix + "ATTEMPT", payload });
}

//If you feel a sudden urge to call this. Don't.
export const handleRequest = store => next => action => {
    next(action);
    const payload = action.payload;
    if (payload) {
        const shibbolethId = store.getState().user.shibbolethId
        callApi(payload.route, payload.method, payload.data, shibbolethId)
            .then(res => store.dispatch({ type: payload.prefix + "SUCCESS", response: res.data }))
            .catch(err => store.dispatch({ type: payload.prefix + "FAILURE", response: err }));
    }
}
@@ -306,126 +306,11 @@
 data
-, shibbolethId) %7B%0A    const header = %7B%0A        headers: %7B%0A            %22shibbolointiId%22: shibbolethId,%0A        %7D,%0A    %7D
+) %7B
 %0A   
@@ -381,32 +381,24 @@
 os().get(url
-, header
 );%0A        c
@@ -449,32 +449,24 @@
 st(url, data
-, header
 );%0A        c
@@ -519,24 +519,16 @@
 rl, data
-, header
 );%0A     
@@ -589,16 +589,8 @@
 (url
-, header
 );%0A 
@@ -1077,72 +1077,8 @@
 ) %7B%0A
-        const shibbolethId = store.getState().user.shibbolethId%0A
     
@@ -1136,22 +1136,8 @@
 data
-, shibbolethId
 )%0A  

remove shibbolointiid