simpler requests

This commit is contained in:
Dan Zajdband
2012-08-14 11:42:57 -03:00
parent 0522132dd6
commit ce2dea3a14

View File

@@ -9,19 +9,14 @@ var endpoints = require('./endpoints.json')
, base_url = endpoints.base_url , base_url = endpoints.base_url
, MovieDB = module.exports = module.parent.exports; , MovieDB = module.exports = module.parent.exports;
var get, post; /*
* Generate API methods
*/
Object.keys(methods).forEach(function(method){ Object.keys(methods).forEach(function(method){
get = methods[method].get; Object.keys(methods[method]).forEach(function(m){
post = methods[method].post; MovieDB.prototype[method + m] = function(params, fn){
var self = this;
// GET requests
Object.keys(methods[method].get).forEach(function(g){
MovieDB.prototype[method + g] = function(params, fn){
var that = this;
if("function" == typeof params) { if("function" == typeof params) {
fn = params; fn = params;
@@ -29,95 +24,49 @@ Object.keys(methods).forEach(function(method){
} }
if(!this.token || Date.now() > +new Date(this.token.expires_at)) { if(!this.token || Date.now() > +new Date(this.token.expires_at)) {
this.requestToken(function(){ 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 { } else {
execMethod.call(this, methods[method][m].method, params, methods[method][m].resource, fn);
execMethod.call(this, 'get', params, methods[method].get[g], 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){ var execMethod = function(type, params, endpoint, fn){
params = params || {}; params = params || {};
endpoint = endpoint.replace(':id', params.id); endpoint = endpoint.replace(':id', params.id);
delete params.id;
if(type === 'post'){ var req;
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);
}
});
if(type == "get") {
req = request.get(base_url + endpoint)
} else if(type == "post") {
req = request.post(base_url + endpoint);
} else { } else {
request fn(new Error("Method is not well implemented, needs to be a get or post request"), null);
.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);
}
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 { } else {
fn(res.error, null); fn(res.error, null);
} }
}); }
} });
}; };