'use strict';
/**
 * @file Easy access to AnyFetch API.
 *
 * @see http://anyFetch.com
 */
var request = require('request')

// TODO: wrap into an Anyfetch class
// TODO: Anyfetch.getAccessToken(APP_ID, APP_SECRET, code, function(err, accessToken)

// TODO: make configurable
var apiUrl = 'http://api.anyfetch.com';

/**
 * Will be used to automatically generate the simplest mapping functions
 * @see api.anyfetch.com/config/routes.js
 */
var defaultDescriptor = {

}

var mappingDescriptor = {
  // TODO: / (retrieve all infos about the current account)
  'getToken': {
    expectedStatus: 200,
    endpoint: '/token',
    verb: 'GET',
    authType: 'Basic'
  },
  'deleteToken': {
    expectedStatus: 200,
    endpoint: '/token',
    verb: 'DELETE',
    authType: 'Basic'
  },
  'getCompanyById': {
    expectedStatus: 200,
    endpoint: '/token',
    verb: 'DELETE',
    authType: 'Basic',
    params: ['id']
  }
}
var mappingFunctions = {};

// var mappingFunctions = mappingDescriptor.map
var generateMapping = function(config) {
  return function(params, cb) {
    var options = {
      url: apiUrl + config.endpoint,
      method: config.verb,
      qs: config.params, // TODO: url encode
      headers: {
        // TODO: use the access token from the Anyfetch object instance
        'Authentication': 'Basic'
      }
      // TODO: make SSL is used
    };

    request(options, function(err, res, body) {
      if (res.statusCode != config.expectedStatus)
        return cb(new Error('Received code ' + res.statusCode + ', expected ' + config.expectedStatus));

      cb(err, res);
    });
  };
};

// Generate the function for each endpoint
for (var name in mappingDescriptor) {
  mappingFunctions[name] = generateMapping(mappingDescriptor[name]);

  // Test the generated function
  mappingFunctions[name]({}, function(err, result) {
    console.log("> Calling " + name);
    console.log(err);
    console.log(result);
  });
}
@@ -240,16 +240,118 @@
 ssToken)
+%0A// Use the access token from the Anyfetch object instance%0Avar accessToken = process.env.ACCESS_TOKEN;
 %0A%0A// TOD
@@ -385,24 +385,25 @@
 iUrl = 'http
+s
 ://api.anyfe
@@ -658,21 +658,23 @@
 )%0A  'get
-Token
+Company
 ': %7B%0A   
@@ -703,37 +703,39 @@
     endpoint: '/
-token
+company
 ',%0A    verb: 'GE
@@ -746,45 +746,46 @@
     
-authType: 'Basic'%0A  %7D,%0A  'deleteToken
+params: %5B'id'%5D%0A  %7D,%0A  'getDocumentById
 ': %7B
@@ -818,37 +818,46 @@
     endpoint: '/
-token
+documents/%7Bid%7D
 ',%0A    verb: 'DE
@@ -858,176 +858,80 @@
 b: '
-DEL
+G
 ET
-E
 ',%0A    
-authType: 'Basic'%0A  %7D,%0A  'getCompanyById': %7B%0A    expectedStatus: 200,%0A    endpoint: '/token',%0A    verb: 'DELETE',%0A    authType: 'Basic',%0A    params: %5B'id'%5D
+params: %5B'id'%5D // Or generate params directly from %60endpoint%60?
 %0A  %7D
@@ -1240,105 +1240,48 @@
     
-// TODO: use the access token from the Anyfetch object instance%0A        'Authentication': 'Basic'
+'Authorization': 'Bearer ' + accessToken
 %0A   
@@ -1305,16 +1305,21 @@
 O: make 
+sure 
 SSL is u
@@ -1549,19 +1549,20 @@
 cb(err, 
-res
+body
 );%0A    %7D
@@ -1757,16 +1757,38 @@
 unction%0A
+  (function(name) %7B%0A  
   mappin
@@ -1828,24 +1828,26 @@
 , result) %7B%0A
+  
     console.
@@ -1872,24 +1872,26 @@
  name);%0A    
+  
 console.log(
@@ -1896,24 +1896,26 @@
 g(err);%0A    
+ 
+ 
 console.log(
@@ -1925,13 +1925,27 @@
 ult);%0A  
-%7D
+  %7D);%0A  %7D(name)
 );%0A%7D

Simplify functions descriptor