diff --git a/lib/request.js b/lib/request.js index f919bce..e0c603b 100644 --- a/lib/request.js +++ b/lib/request.js @@ -9,19 +9,14 @@ var endpoints = require('./endpoints.json') , base_url = endpoints.base_url , MovieDB = module.exports = module.parent.exports; -var get, post; +/* + * Generate API methods + */ Object.keys(methods).forEach(function(method){ - get = methods[method].get; - post = methods[method].post; - - // GET requests - - Object.keys(methods[method].get).forEach(function(g){ - - MovieDB.prototype[method + g] = function(params, fn){ - - var that = this; + Object.keys(methods[method]).forEach(function(m){ + MovieDB.prototype[method + m] = function(params, fn){ + var self = this; if("function" == typeof params) { fn = params; @@ -29,95 +24,49 @@ Object.keys(methods).forEach(function(method){ } if(!this.token || Date.now() > +new Date(this.token.expires_at)) { - this.requestToken(function(){ - execMethod.call(that, 'get', params, methods[method].get[g], fn); + execMethod.call(self, methods[method][m].method, params, methods[method][m].resource, fn); }); - } else { - - execMethod.call(this, 'get', params, methods[method].get[g], fn); - + execMethod.call(this, methods[method][m].method, params, methods[method][m].resource, fn); } - }; }); - - // POST requests - Object.keys(methods[method].post).forEach(function(p){ - - MovieDB.prototype[method + p] = function(params, fn){ - - var that = this; - - if("function" == typeof params) { - fn = params; - params = {}; - } - - if(!this.token || Date.now() > +new Date(this.token.expires_at)) { - - this.requestToken(function(){ - execMethod.call(that, 'post', params, methods[method].post[p], fn); - }); - - } else { - - execMethod.call(this, 'post', params, methods[method].post[p], fn); - - } - - }; - - }); - }); var execMethod = function(type, params, endpoint, fn){ - params = params || {}; endpoint = endpoint.replace(':id', params.id); - delete params.id; - - if(type === 'post'){ - request - .post(base_url + endpoint) - .query({api_key : this.api_key}) - .send(params) - .set('Accept', 'application/json') - .end(function(res){ - if(res.ok){ - try{ - var body = JSON.parse(res.text); - fn(null, body); - } catch(e){ - fn(e, null); - } - - } else { - fn(res.error, null); - } - }); + + var req; + if(type == "get") { + req = request.get(base_url + endpoint) + } else if(type == "post") { + req = request.post(base_url + endpoint); } else { - request - .get(base_url + endpoint) - .query({api_key : this.api_key}) - .send(params) - .set('Accept', 'application/json') - .end(function(res){ - if(res.ok){ - try{ - var body = JSON.parse(res.text); - fn(null, body); - } catch(e){ - fn(e, null); - } - + fn(new Error("Method is not well implemented, needs to be a get or post request"), null); + } + + req + .query({api_key : this.api_key}) + .send(params) + .set('Accept', 'application/json') + .end(function(res){ + if(res.ok){ + try{ + var body = JSON.parse(res.text); + fn(null, body); + } catch(e){ + fn(e, null); + } + } else { + if(res.body && res.body.status_message){ + fn(new Error(res.body.status_message), null); } else { fn(res.error, null); } - }); - } + } + }); };