-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·97 lines (80 loc) · 2.45 KB
/
index.js
File metadata and controls
executable file
·97 lines (80 loc) · 2.45 KB
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
89
90
91
92
93
94
95
96
97
var request = require('request');
var querystring = require('querystring');
/**
* KeyCDN API Client for NodeJS
* @constructor
* @param {string} apiKey Your account's KeyCDN API Key
* @author KeyCDN
*/
function KeyCDN(apiKey) {
if (typeof apiKey !== 'string') {
throw new Error('api key missing or not a string');
}
this.apiServer = 'https://api.keycdn.com/';
this.apiKey = apiKey;
headers = {
"Accept" : "*/*",
"Connection" : "close",
"User-Agent" : "Node KeyCDN API Client"};
return this;
}
KeyCDN.prototype._call = function(url, method, data, callback) {
var qs, body;
if (method == 'get') {
qs = data;
} else {
body = data;
}
request({
method: method,
url: url,
strictSSL: true,
json: true,
body: body,
qs: qs
}, function (error, response, data) {
if (!data && response.statusCode >= 400) {
error = new Error("Invalid Response: " + response.statusCode, response.statusMessage);
} else if (!error && !!data.status && data.status !== 'success') {
error = new Error(data.description || data.error_message);
} else {
data.remaining = response.headers["x-rate-limit-remaining"];
}
callback(error, data || {});
}).auth(this.apiKey, '');
};
KeyCDN.prototype.get = function get(url, data, callback) {
if (callback === undefined) {
callback = data;
data = '';
}
this._call(this.apiServer + url, 'get', data, callback);
};
KeyCDN.prototype.post = function post(url, data, callback) {
this._call(this.apiServer + url, 'post', this._makeObject(data), callback);
};
KeyCDN.prototype.put = function put(url, data, callback) {
this._call(this.apiServer + url, 'put', this._makeObject(data), callback);
};
KeyCDN.prototype.del = function del(url, data, callback) {
if (callback === undefined) {
callback = data;
data = '';
}
this._call(this.apiServer + url, 'delete', this._makeObject(data), callback);
};
KeyCDN.prototype._makeObject = function _makeObject(params) {
if (typeof params === 'string') {
try {
return JSON.parse(params);
} catch (e) {
try {
return querystring.parse(params);
} catch (ee) {
throw new Error('invalid params string');
}
}
}
return params;
};
module.exports = KeyCDN;