-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
59 lines (49 loc) · 1.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var util = require('util'),
EventEmitter = require('events').EventEmitter,
Auth = require('./lib/OAuth').Auth,
OAuthRequest = require('./lib/OAuth').Request;
var NodeFlix = function(config, endPoints) {
this.config = config;
this.endPoints = {
host: 'api-public.netflix.com',
oauth_request_url: 'http://api-public.netflix.com/oauth/request_token',
oauth_access_url: 'http://api-public.netflix.com/oauth/access_token'
};
copyTo(endPoints, this.endPoints);
this.auth = new Auth(this.config);
};
var copyTo = function(source, dest) {
for (var prop in source) {
dest[prop] = source[prop];
}
};
NodeFlix.prototype = {
get: function(path, params, callback) {
return this.request('GET', path, params, callback);
},
post: function(path, params, callback) {
return this.request('POST', path, params, callback);
},
put: function(path, params, callback) {
return this.request('PUT', path, params, callback);
},
delete: function(path, params, callback) {
return this.request('DELETE', path, params, callback);
},
request: function(method, path, params, callback) {
if(typeof(params) === 'function') {
callback = params;
params = {};
}
for(var prop in this.config) {
path = path.replace(':' + prop, this.config[prop], 'g');
}
var req = new OAuthRequest(this.auth, method, this.endPoints.host, path, params);
if(typeof(callback) === 'function') {
return req.end(callback);
} else {
return req;
}
}
};
module.exports = NodeFlix;