-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxboxapi.js
75 lines (56 loc) · 1.7 KB
/
xboxapi.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
var request = require('request');
var baseUrl = "https://xboxapi.com/v2";
function XboxApi(apikey,options) {
this.apiKey = apikey;
this.myxuid = null;
this.proxy = options.proxy;
this.remainingCalls = null;
}
function makeRequestOptions(api,path) {
var r = {
url : baseUrl + path,
headers: {
"X-AUTH" : api.apiKey
}
};
if( api.proxy ) {
r.proxy = api.proxy;
}
return r;
}
XboxApi.prototype.updateCalls = function(response) {
this.remainingCalls = parseInt(response.headers['x-ratelimit-remaining']);
console.log(this.remainingCalls + " calls remaining");
};
XboxApi.prototype.getXuid = function(callback) {
request(makeRequestOptions(this,"/accountXuid"),function(error,response,body) {
if( error ) {
callback && callback(error);
} else {
this.updateCalls(response);
body = JSON.parse(body);
if( body ) {
this.myxuid = body.xuid;
callback && callback(null,this.myxuid);
} else {
callback && callback("unable to parse body");
}
}
}.bind(this));
};
XboxApi.prototype.getGameClips = function(uid,callback) {
request(makeRequestOptions(this,"/"+uid+"/game-clips"),function(error,response,body) {
if( error ) {
callback && callback(error);
} else {
this.updateCalls(response);
body = JSON.parse(body);
if( body ) {
callback && callback(null,body);
} else {
callback && callback("unable to parse body");
}
}
}.bind(this));
};
module.exports = XboxApi;