forked from bendl/node-xbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXboxResource.js
88 lines (64 loc) · 2.02 KB
/
XboxResource.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
var request = require('request');
var utils = require('./utils');
var path = require('path');
var when = require('when');
var hasOwn = {}.hasOwnProperty;
var XboxResource = function(xbox, urlData){
this._xbox = xbox;
this._urlData = urlData || {};
this.basePath = utils.makeURLInterpolator(xbox._getApiField('basePath'));
this.path = utils.makeURLInterpolator(this.path);
this.initialize.apply(this, arguments);
};
XboxResource.extend = utils.protoExtend;
XboxResource.method = require('./XboxMethod');
XboxResource.prototype = {
path: '',
initialize: function () {},
createDeferred: function(callback){
var deferred = when.defer();
if(callback){
deferred.promise.then(function(res){
setTimeout(function() { callback(null, res); }, 0);
}, function(err){
setTimeout(function (){ callback(err, null); }, 0);
})
}
return deferred;
},
createUrlData: function(){
var urlData = {};
for(var i in this._urlData){
if(hasOwn.call(this._urlData, i)) {
urlData[i] = this._urlData[i];
}
}
return urlData;
},
createFullPath: function(commandPath, urlData){
return path.join(
this.basePath(urlData),
this.path(urlData),
typeof commandPath == "function" ?
commandPath(urlData) : commandPath
).replace(/\\/g, '/');
},
request: function (method, path, data, callback) {
var self = this;
var headers = {
"X-AUTH": this._xbox._getApiField('key')
};
makeRequest();
function makeRequest(){
request({
method: method,
uri: 'https://' + self._xbox._getApiField('host') + path,
headers: headers
}, function(e, r, b){
return callback.call(self, null, b);
});
}
}
}
module.exports = XboxResource;