// Dependencies
var fs = require('fs');
var request = require('request-json');
var conflate = require('conflate');

// Constants
var ENDPOINT_URL = 'http://trader.adgear.com/api/v2/';

/**
 * Constructor for the API client
 * @param authToken
 * @param config
 * @returns {Trader}
 * @constructor
 */
function Trader(authToken, config) {
	if (!authToken) throw new TypeError('required auth token');
	
	// Defaults
	config = config || {};
	
	// Config
	var self = this;
		self.authToken = authToken;
		self.endpoint = config.endpoint || ENDPOINT_URL;
		self.client = request.createClient(this.endpoint);
	
	return self;
}

/**
 * Requests JSON from the API and returns to callback
 * @param methodName
 * @param methodType
 * @param params
 * @param callback
 * @private
 */
Trader.prototype._request = function(methodName, methodType, params, callback) {
	if (!callback) throw new Error('Missing callback.');
	
	// Defaults
	params = params || {};
	
	// Add auth token
	params.qs = params.qs || {}; 
	params.qs.auth_token = this.authToken;
	
	var url = (typeof methodName === 'object') ? methodName.join('/') : methodName;
	
	return this.client[methodType](url, params, function(err, res, body) {
		if (err) throw new Error(url + ' error');
		
		callback(body);
	});
};

// Load resources from the resources folder to extend the prototype
var files = fs.readdirSync(__dirname + '/resources/');
for (var i = 0; i < files.length; i++) {
	if (files[i].match(/.*\.js/)) {
		var res = require('./resources/' + files[i]);
		conflate(Trader.prototype, res);
	}
}

module.exports = function(authToken, config) {
	return new Trader(authToken, config);
};
@@ -67,13 +67,8 @@
 uest
--json
 ');%0A
@@ -542,61 +542,8 @@
 RL;%0A
-%09%09self.client = request.createClient(this.endpoint);%0A
 %09%0A%09r
@@ -625,25 +625,23 @@
  @param 
-methodNam
+resourc
 e%0A * @pa
@@ -650,20 +650,16 @@
 m method
-Type
 %0A * @par
@@ -740,25 +740,23 @@
 unction(
-methodNam
+resourc
 e, metho
@@ -756,20 +756,16 @@
 , method
-Type
 , params
@@ -881,65 +881,173 @@
 %09// 
-Add auth token%0A%09params.qs = params.qs %7C%7C %7B%7D; %0A%09params.qs.
+Resource%0A%09resource = ((typeof resource === 'object') ? resource.join('/') : resource);%0A%09%0A%09var properties = %7B%0A%09%09method:method,%0A%09%09url:this.endpoint + resource,%0A%09%09qs:%7B 
 auth
@@ -1052,19 +1052,17 @@
 th_token
- = 
+:
 this.aut
@@ -1071,137 +1071,215 @@
 oken
-;%0A%09%0A%09var url = (typeo
+ %7D,%0A%09%09headers:%7B%0A%09%09%09'Content-Type':'application/json'%0A%09%09%7D%0A%09%7D;%0A%09%0A%09i
 f 
+(
 method
-Name
  === '
-object') ? methodName.join('/') : methodName;%0A%09%0A%09return this.client%5BmethodType%5D(url, param
+get') %7B%0A%09%09conflate(properties.qs, params);%0A%09%7D else %7B%0A%09%09properties.body = JSON.stringify(params);%0A%09%7D%0A%09%0A%09return request(propertie
 s, f
@@ -1331,16 +1331,27 @@
 w Error(
+properties.
 url + ' 
@@ -1356,16 +1356,30 @@
 ' error'
+ + err.message
 );%0A%09%09%0A%09%09
@@ -1387,21 +1387,33 @@
 allback(
+JSON.parse(
 body)
+)
 ;%0A%09%7D);%0A%7D

fix post methods