import {requestFailed} from './actions'

let execute = function(dispatch, uri, options) {
    options = options || {}
    options.credentials = options.credentials || 'same-origin'
    if (options.headers === undefined) {
        options.headers = new Headers()
    }
    options.headers.set('Content-Type', 'application/json')
    options.headers.set('Accept', 'application/json')

    return new Promise((resolve, reject) => {
        fetch(uri, options)
        .then(response => {
            if (response.ok) {
                if (response.headers.get('content-type').indexOf('application/json') !== -1) {
                    resolve(response.json())
                } else {
                    dispatch(requestFailed({
                        error: 'Server error',
                        message: 'Oops, something is with our server, sorry for the inconvenience',
                        debug: 'Response from "' + response.url + '" has unexpected "content-type"'
                    }))
                }
            } else {
                response.json()
                .then(json => { reject(json) })
                .catch(error => {
                    dispatch(requestFailed({
                        error: 'Server error',
                        message: 'Oops, something is with our server, sorry for the inconvenience',
                        debug: error
                    }))
                })
            }
        })
        .catch(response => {
            dispatch(requestFailed({
                error: 'Server unreachable',
                message: 'Please check your internet connection',
                debug: response
            }))
        })
    })
}

const api = {
    get: function(dispatch, uri, options) {
        let getOptions = {
            method: 'GET'
        }
        if (options !== undefined) {
            Object.assign(getOptions, options)
        }

        return execute(dispatch, uri, getOptions)
    },
    post: function(dispatch, uri, payload, options) {
        let postOptions = {
            method: 'POST',
            body: JSON.stringify(payload)
        }
        if (options !== undefined) {
            Object.assign(postOptions, options)
        }

        return execute(dispatch, uri, postOptions)
    }
}

export default api

@@ -1967,110 +1967,688 @@
     
-post: function(dispatch, uri, payload, options) %7B%0A        let postOptions = %7B%0A            method: 'POS
+delete: function(dispatch, uri, options) %7B%0A        let getOptions = %7B%0A            method: 'DELETE'%0A        %7D%0A        if (options !== undefined) %7B%0A            Object.assign(getOptions, options)%0A        %7D%0A%0A        return execute(dispatch, uri, getOptions)%0A    %7D,%0A    post: function(dispatch, uri, payload, options) %7B%0A        let postOptions = %7B%0A            method: 'POST',%0A            body: JSON.stringify(payload)%0A        %7D%0A        if (options !== undefined) %7B%0A            Object.assign(postOptions, options)%0A        %7D%0A%0A        return execute(dispatch, uri, postOptions)%0A    %7D%0A    put: function(dispatch, uri, payload, options) %7B%0A        let postOptions = %7B%0A            method: 'PU
 T',%0A

Add api methods