# Source: RestHelper.js

    /**
     * Copyright (c) 2013 - 2014 GE Global Research. All rights reserved.
     * The copyright to the computer software herein is the property of
     * GE Global Research. The software may be used and/or copied only
     * with the written permission of GE Global Research or in accordance
     * with the terms and conditions stipulated in the agreement/contract
     * under which the software has been supplied.
     */

    (function () {

        var logger = require('./Logger.js');
        var request = require('superagent');
        require('superagent-proxy')(request);
        require('dotenv').config();
        var proxy = process.env.httpProxy
        //console.log("proxy : " + proxy);

        /**
         * Creates new RestHelper
         * @class RestHelper
         */
        var RestHelper = function () {

            return {
                /**
                 * Perform a get request
                 * @param url         the end point to perform a get request on
                 * @param headers     http headers
                 * @param callback    callback on error and http response
                 */
                executeGetRequest: function (url, headers, callback) {
                    logger.info('\r\nExecuted get request :' + url);
                    if(proxy != null) {
                        request.get(url)
                            .proxy(proxy)
                            .set(headers)
                            .end(onGetResponse);
                    }else{
                        request.get(url)
                            .set(headers)
                            .end(onGetResponse);

                    }
                    function onGetResponse(err, res) {
                        if (err || !res.ok)
                            callback(err, res);
                        else if (res.ok) {
                            //logger.info(JSON.stringify(res.body));
                            logger.info("Get response status=" + res.status);
                            if (typeof callback === "function") {
                                callback(err, res);
                            }
                        }
                    }
                },

                /**
                 * Perform a post request
                 * @param url       the end point to perform a post request
                 * @param headers   http headers
                 * @param body      post request body object
                 * @param callback  callback on error and http response
                 */
                executePostRequest: function (url, headers, body, callback) {
                    logger.info('Executed post request : ' + url);
                    if(proxy != null) {
                        request.post(url)
                            .proxy(proxy)
                            .type('form')
                            .send(body)
                            .set(headers)
                            .end(onPostResponse);
                    }else{
                        request.post(url)
                            .type('form')
                            .send(body)
                            .set(headers)
                            .end(onPostResponse);
                    }

                    function onPostResponse(err, res) {
                        if (err || !res.ok)
                            callback(err, res);
                        else if (res.ok) {
                            logger.info("Post response status=" + res.status);
                            //logger.info( JSON.stringify(res.body));
                            if (typeof callback === "function") {
                                callback(err, res);
                            }
                        }
                    }
                },

                /**
                 * Perform a put request
                 * @param url       the end point to perform a put request
                 * @param headers   http headers
                 * @param body      put request body object
                 * @param callback  callback on error and http response
                 */
                executePutRequest: function (url, headers, body, callback) {
                    logger.info('Executed put request : ' + url);
                    if(proxy != null) {
                        request.put(url)
                            .proxy(proxy)
                            .type('form')
                            .send(body)
                            .set(headers)
                            .end(onPutResponse);
                    }else{
                        request.put(url)
                            .type('form')
                            .send(body)
                            .set(headers)
                            .end(onPutResponse);
                    }

                    function onPutResponse(err, res) {
                        if (err || !res.ok)
                            callback(err, res);
                        else if (res.ok) {
                            logger.info("Put response status=" + res.status);
                            //logger.info( JSON.stringify(res.body));
                            if (typeof callback === "function") {
                                callback(err, res);
                            }
                        }
                    }
                },

                /**
                 * Perform a patch request
                 * @param url       the end point to perform a post request
                 * @param headers   http headers
                 * @param body      patch request body object
                 * @param callback  callback on error and http response
                 */
                executePatchRequest: function (url, headers, body, callback) {
                    logger.info('Executed patch request : ' + url);
                    if(proxy != null) {
                        request.patch(url)
                            .proxy(proxy)
                            .type('form')
                            .send(body)
                            .set(headers)
                            .end(onPatchResponse);
                    }else{
                        request.patch(url)
                            .type('form')
                            .send(body)
                            .set(headers)
                            .end(onPatchResponse);
                    }

                    function onPatchResponse(err, res) {
                        if (err || !res.ok)
                            callback(err, res);
                        else if (res.ok) {
                            logger.info("Patch response status=" + res.status);
                            //logger.info( JSON.stringify(res.body));
                            if (typeof callback === "function") {
                                callback(err, res);
                            }
                        }
                    }
                },

                /**
                 * Perform a delete request
                 * @param url      the end point to perform a delete request
                 * @param headers  http headers
                 * @param callback callback on error and http response
                 */
                executeDeleteRequest: function (url, headers, callback) {
                    logger.info('Executed delete request :' + url);
                    if(proxy != null) {
                        request.del(url)
                            .proxy(proxy)
                            .set(headers)
                            .end(onDeleteResponse);
                    }else{
                        request.del(url)
                            .set(headers)
                            .end(onDeleteResponse);
                    }

                    function onDeleteResponse(err, res) {
                        if (err || !res.ok)
                            callback(err, res);
                        else if (res.ok) {
                            logger.info("Delete response status=" + res.status);
                            //logger.info( JSON.stringify(res.body));
                            if (typeof callback === "function") {
                                callback(err, res);
                            }
                        }
                    }
                },

                /**
                 * An util to get the access_token
                 * @param url       the url to get the access token
                 * @param headers   http headers
                 * @param body      post request body
                 * @param callback
                 */
                getAccessToken: function (url, headers, body, callback) {
                    logger.info('Get access token url :' + url);
                    console.log('Get access token url :' + url);
                    if (proxy != null) {
                        console.log('Use proxy : ' + proxy );
                        request.post(url)
                            .proxy(proxy)
                            .type('form')
                            .send(body)
                            .set(headers)
                            .end(onGetAccessTokenResponse);
                    }else {
                        console.log('No proxy setting');
                        request.post(url)
                            .type('form')
                            .send(body)
                            .set(headers)
                            .end(onGetAccessTokenResponse);
                    }
                    function onGetAccessTokenResponse(err, res) {
                        if (err || !res.ok)
                            callback(err, res);
                        else if (res.ok) {
                            logger.info('Executed getAccessToken : ' + JSON.stringify(res.body));
                            var obj = JSON.parse(JSON.stringify(res.body));
                            //logger.info("token=" + obj.access_token);
                            if (typeof callback === "function") {
                                callback(err, obj.access_token);
                            }
                        }
                    }
                }
            }
        };
        module.exports = new RestHelper();
    }())