Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added limts.js #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 73 additions & 56 deletions lib/moviedb.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@
*/

var request = require('superagent');
var limits = require('limits.js');

var endpoints = require('./endpoints.json');

/*
* Exports the constructor
*/

module.exports = function(api_key, base_url) {
if(api_key) return new MovieDB(api_key, base_url);
module.exports = function(api_key, timeLimit, requestsLimit, base_url) {
if(api_key) return new MovieDB(api_key, timeLimit, requestsLimit, base_url);
else throw new Error('Bad api key');
};

/*
* Constructor
*/

function MovieDB(api_key, base_url) {
function MovieDB(api_key, timeLimit, requestsLimit, base_url) {
if(typeof timeLimit === 'undefined'){
timeLimit = 10500;
}
if(typeof requestsLimit === 'undefined'){
requestsLimit = 40;
}
this.api_key = api_key;
this.queue = limits();
this.queue.within(timeLimit, requestsLimit);
if(base_url) endpoints.base_url = base_url;
return this;
}
Expand All @@ -32,17 +42,19 @@ function MovieDB(api_key, base_url) {
MovieDB.prototype.requestToken = function(fn){
var self = this;

request
.get(endpoints.base_url + endpoints.authentication.requestToken)
.query({'api_key': self.api_key})
.set('Accept', 'application/json')
.end(function(err, res){
if(err) {
fn(err);
} else {
self.token = res.body;
fn();
}
this.queue.push(function(){
request
.get(endpoints.base_url + endpoints.authentication.requestToken)
.query({'api_key': self.api_key})
.set('Accept', 'application/json')
.end(function(err, res){
if(err) {
fn(err);
} else {
self.token = res.body;
fn();
}
});
});

return this;
Expand All @@ -55,20 +67,22 @@ MovieDB.prototype.requestToken = function(fn){
MovieDB.prototype.session = function(fn){
var self = this;

request
.get(endpoints.base_url + endpoints.authentication.session)
.query({'api_key': self.api_key, 'request_token': self.token.request_token})
.set('Accept', 'application/json')
.end(function(err, res){
if(err) {
fn(err);
} else {
if (res.body.success) {
self.session_id = res.body.session_id;
fn();
}
else fn(res.body);
}
this.queue.push(function(){
request
.get(endpoints.base_url + endpoints.authentication.session)
.query({'api_key': self.api_key, 'request_token': self.token.request_token})
.set('Accept', 'application/json')
.end(function(err, res){
if(err) {
fn(err);
} else {
if (res.body.success) {
self.session_id = res.body.session_id;
fn();
}
else fn(res.body);
}
});
});

return this;
Expand All @@ -89,50 +103,53 @@ Object.keys(endpoints.methods).forEach(function(method){
params = {};
}

if(!this.token || Date.now() > +new Date(this.token.expires_at)) {
this.requestToken(function(err){
if(!self.token || Date.now() > +new Date(self.token.expires_at)) {
self.requestToken(function(err){
if(err) return fn(err);
execMethod.call(self, met[m].method, params, met[m].resource, fn);
});
});
} else {
execMethod.call(this, met[m].method, params, met[m].resource, fn);
}
execMethod.call(self, met[m].method, params, met[m].resource, fn);
}

return this;
};
});
});

var execMethod = function(type, params, endpoint, fn){
var self = this;
params = params || {};
endpoint = endpoint.replace(':id', params.id).replace(':season_number', params.season_number).replace(':episode_number', params.episode_number);
type = type.toUpperCase();

var req = request(type, endpoints.base_url + endpoint)
.query({api_key : this.api_key, session_id: this.session_id})
.set('Accept', 'application/json');

if (params.ifNoneMatch) {
req=req.set('If-None-Match', params.ifNoneMatch);

} else if (params.ifModifiedSince) {
var t=params.ifModifiedSince;
if (t.toUTCString) {
t=t.toUTCString();
this.queue.push(function(){
var req = request(type, endpoints.base_url + endpoint)
.query({api_key : self.api_key, session_id: self.session_id})
.set('Accept', 'application/json');

if (params.ifNoneMatch) {
req=req.set('If-None-Match', params.ifNoneMatch);

} else if (params.ifModifiedSince) {
var t=params.ifModifiedSince;
if (t.toUTCString) {
t=t.toUTCString();
}
req=req.set('If-Modified-Since', t);
}
req=req.set('If-Modified-Since', t);
}

if(type === 'GET')
req.query(params);
else
req.send(params);
if(type === 'GET')
req.query(params);
else
req.send(params);

req.end(function(err, res){
if(err){
fn(err, null, res);
} else {
fn(null, res.body, res);
}
req.end(function(err, res){
if(err){
fn(err, null, res);
} else {
fn(null, res.body, res);
}
});
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"main": "index.js",
"author": "Dan Zajdband <dan.zajdband@gmail.com>",
"dependencies": {
"limits.js": "^0.1.4",
"superagent": "^2.3.0"
},
"devDependencies": {
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,42 @@ describe('moviedb', function() {
done();
});
});
});

describe('spam', function() {

this.timeout(30000);

it ('should not crash when spammed with requests', function(done) {
var requests = 50;
var requestsFinished = 0;

var callback = function (err){
if(requestsFinished < 0) return;
if(err) {
done(err);
requestsFinished = -1;
return;
}

requestsFinished++;
if(requestsFinished == requests){
done();
}
}

// wait for requestToken to finish
api.genreMovieList({}, function(err, res) {
for(var i = 0; i < requests; i++){
api.genreMovieList({}, function(err, res) {
if(err) return callback(err);

res.should.be.an('object');
res.should.have.property('genres');
res.genres.should.be.an('array');
callback();
})
}
})
});
});