forked from mediremi/twitch.tv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (26 loc) · 768 Bytes
/
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
var request = require("request")
module.exports = function getAPI(apiMethod, options, callback) {
var baseUrl = "https://api.twitch.tv/kraken/"
if (typeof options === "function") {
callback = options
options = null
}
options = options || {}
baseUrl = options.baseUrl || baseUrl
var headers = {
"User-Agent": options.ua || "node.js twitch.tv by mediremi",
"Accept": "application/vnd.twitchtv.v" + (options.apiVersion || "2") + "+json",
"Client-ID": options.clientID || ""
}
request({
url: baseUrl + apiMethod,
headers: headers
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
// TODO: What if JSON.parse throws an error?
callback(null, JSON.parse(body))
} else {
callback(error, null)
}
})
}