
//var threescale = require('3scale');
//var Promise = require('bluebird');

//var Client = threescale.Client;
//var client = new Client(process.env.PROVIDER_KEY);
var http = require('http');
var fetch = require('node-fetch');
fetch.Promise = require('bluebird');
var providerKey = process.env.PROVIDER_KEY;
var serviceID = process.env.SERVICE_ID;

exports.authenticate = function(user_key) { // user_key comes from the query or header, method comes from path matching with hardcoded values
   
// var options = {
//         'user_key': user_key,
//         'service_id': serviceID
//     };

    // http.get('http://su1.3scale.net/transactions/authrep.xml?provider_key=' + providerKey + '&user_key=' + user_key + '&service_id=' + serviceID, function(res) {
    //     console.log(res.statusCode);
    //     if (res.statusCode == 200) {
    //         callback(null,true);
    //     } else {
    //         callback(new Error('403, unauthorized'),null);
    //     }
    // });

    return fetch('http://su1.3scale.net/transactions/authrep.xml?provider_key=' + providerKey + '&user_key=' + user_key + '&service_id=' + serviceID)
        .then(function(res) {
            if (res.status == 200) {
                return true; //callback(null,true);
            } else {
                return false; //callback(new Error('403, unauthorized'),null);
            }
        });


    // client.authrep_with_user_key(options, function(res) {
    //     if (res.is_success()) {
    //         callback(null, res.is_success());
    //     } else {
    //         callback('403, unauthorized');
    //     }
    // });
};

/** 
* Lambda function to be used as an AWS API gateway endpoint handler. 
* Requires setting up the following Mapping Template: 
*   - Content-Type: application/json 
*   - Template: 
*       { 
*         "user_key": "$input.params('user_key')", 
*         "resourcePath": "$context.resourcePath", 
*         "httpMethod": "$context.httpMethod", 
*         "body": $input.json('$') 
*       } 
* */


@@ -1,168 +1,4 @@
-%0A//var threescale = require('3scale');%0A//var Promise = require('bluebird');%0A%0A//var Client = threescale.Client;%0A//var client = new Client(process.env.PROVIDER_KEY);%0A
 var 
@@ -328,494 +328,8 @@
    %0A
-// var options = %7B%0A//         'user_key': user_key,%0A//         'service_id': serviceID%0A//     %7D;%0A%0A    // http.get('http://su1.3scale.net/transactions/authrep.xml?provider_key=' + providerKey + '&user_key=' + user_key + '&service_id=' + serviceID, function(res) %7B%0A    //     console.log(res.statusCode);%0A    //     if (res.statusCode == 200) %7B%0A    //         callback(null,true);%0A    //     %7D else %7B%0A    //         callback(new Error('403, unauthorized'),null);%0A    //     %7D%0A    // %7D);%0A%0A
     
@@ -703,24 +703,24 @@
           %7D%0A
+
         %7D);%0A
@@ -723,245 +723,8 @@
 %7D);%0A
-%0A%0A    // client.authrep_with_user_key(options, function(res) %7B%0A    //     if (res.is_success()) %7B%0A    //         callback(null, res.is_success());%0A    //     %7D else %7B%0A    //         callback('403, unauthorized');%0A    //     %7D%0A    // %7D);%0A
 %7D;%0A%0A

Change index.json to use promise