mirror of
https://github.com/KevinMidboe/moviedb.git
synced 2025-10-29 17:50:25 +00:00
reduced lib size and complexity
This commit is contained in:
25
lib/auth.js
25
lib/auth.js
@@ -1,25 +0,0 @@
|
|||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies
|
|
||||||
*/
|
|
||||||
|
|
||||||
var request = require('superagent')
|
|
||||||
, endpoints = require('./endpoints.json')
|
|
||||||
, MovieDB = module.parent.exports;
|
|
||||||
|
|
||||||
|
|
||||||
MovieDB.prototype.requestToken = function(fn){
|
|
||||||
var that = this;
|
|
||||||
|
|
||||||
request
|
|
||||||
.get(endpoints.base_url + endpoints.authentication.requestToken)
|
|
||||||
.send({api_key: that.api_key})
|
|
||||||
.set('Accept', 'application/json')
|
|
||||||
.end(function(res){
|
|
||||||
if(res.ok) that.token = res.body;
|
|
||||||
else throw new Error('Invalid authentication');
|
|
||||||
|
|
||||||
fn();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
@@ -1,9 +1,16 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
* Module dependencies
|
||||||
|
*/
|
||||||
|
|
||||||
|
var request = require('superagent')
|
||||||
|
, endpoints = require('./endpoints.json');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Exports the constructor
|
* Exports the constructor
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = function MovieDB(api_key){
|
var MovieDB = module.exports = function(api_key){
|
||||||
if(api_key) this.api_key = api_key;
|
if(api_key) this.api_key = api_key;
|
||||||
else throw new Error('Bad api key');
|
else throw new Error('Bad api key');
|
||||||
};
|
};
|
||||||
@@ -12,10 +19,58 @@ module.exports = function MovieDB(api_key){
|
|||||||
* API auth
|
* API auth
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require('./auth');
|
MovieDB.prototype.requestToken = function(fn){
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
request
|
||||||
|
.get(endpoints.base_url + endpoints.authentication.requestToken)
|
||||||
|
.send({'api_key': self.api_key})
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.end(function(res){
|
||||||
|
if(res.ok) self.token = res.body;
|
||||||
|
else throw new Error('Invalid authentication');
|
||||||
|
fn();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* API request
|
* Generate API methods
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require('./request');
|
Object.keys(endpoints.methods).forEach(function(method){
|
||||||
|
var met = endpoints.methods[method];
|
||||||
|
Object.keys(met).forEach(function(m){
|
||||||
|
MovieDB.prototype[method + m] = function(params, fn){
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
if("function" == typeof params) {
|
||||||
|
fn = params;
|
||||||
|
params = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!this.token || Date.now() > +new Date(this.token.expires_at)) {
|
||||||
|
this.requestToken(function(){
|
||||||
|
execMethod.call(self, met[m].method, params, met[m].resource, fn);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
execMethod.call(this, met[m].method, params, met[m].resource, fn);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
var execMethod = function(type, params, endpoint, fn){
|
||||||
|
params = params || {};
|
||||||
|
endpoint = endpoint.replace(':id', params.id);
|
||||||
|
|
||||||
|
request(type, endpoints.base_url + endpoint)
|
||||||
|
.query({api_key : this.api_key})
|
||||||
|
.send(params)
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.end(function(res){
|
||||||
|
if(res.ok) fn(null, res.body);
|
||||||
|
else if(res.body && res.body.status_message) fn(new Error(res.body.status_message), null);
|
||||||
|
else fn(res.error, null);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies
|
|
||||||
*/
|
|
||||||
|
|
||||||
var request = require('superagent')
|
|
||||||
var endpoints = require('./endpoints.json')
|
|
||||||
, methods = endpoints.methods
|
|
||||||
, base_url = endpoints.base_url
|
|
||||||
, MovieDB = module.exports = module.parent.exports;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Generate API methods
|
|
||||||
*/
|
|
||||||
|
|
||||||
Object.keys(methods).forEach(function(method){
|
|
||||||
Object.keys(methods[method]).forEach(function(m){
|
|
||||||
MovieDB.prototype[method + m] = function(params, fn){
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
if("function" == typeof params) {
|
|
||||||
fn = params;
|
|
||||||
params = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!this.token || Date.now() > +new Date(this.token.expires_at)) {
|
|
||||||
this.requestToken(function(){
|
|
||||||
execMethod.call(self, methods[method][m].method, params, methods[method][m].resource, fn);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
execMethod.call(this, methods[method][m].method, params, methods[method][m].resource, fn);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
var execMethod = function(type, params, endpoint, fn){
|
|
||||||
params = params || {};
|
|
||||||
endpoint = endpoint.replace(':id', params.id);
|
|
||||||
|
|
||||||
var req;
|
|
||||||
|
|
||||||
if(type == "get") {
|
|
||||||
req = request.get(base_url + endpoint)
|
|
||||||
} else if(type == "post") {
|
|
||||||
req = request.post(base_url + endpoint);
|
|
||||||
} else {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user