-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
248 lines (198 loc) · 5.81 KB
/
index.js
File metadata and controls
248 lines (198 loc) · 5.81 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
var Https = require('https');
var QueryString = require('querystring');
var Zlib = require('zlib');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var ErrorCode = require('./resources/ErrorCode.json');
module.exports = OPSkinsAPI;
OPSkinsAPI.ErrorCode = ErrorCode;
OPSkinsAPI.SaleStatus = require('./resources/SaleStatus.json');
OPSkinsAPI.CashoutProcessor = require('./resources/CashoutProcessor.json');
OPSkinsAPI.Currency = require('./resources/Currency.json');
util.inherits(OPSkinsAPI, EventEmitter);
function OPSkinsAPI(key) {
this.key = key;
}
OPSkinsAPI.prototype.get = function(iface, method, version, input, callback) {
this._req("GET", iface, method, version, input, callback);
};
OPSkinsAPI.prototype.post = function(iface, method, version, input, callback) {
this._req("POST", iface, method, version, input, callback);
};
OPSkinsAPI.prototype.getAgent = function() {
if (this._agent) {
return this._agent;
} else {
this._agent = new Https.Agent({"keepAlive": true});
return this._agent;
}
};
OPSkinsAPI.prototype._req = function(httpMethod, iface, method, version, input, callback) {
if (typeof input === 'function') {
callback = input;
input = null;
}
input = input || {};
var rawInput = input;
var errored = false;
// preprocess arrays
for (var i in input) {
if (!input.hasOwnProperty(i)) {
continue;
}
if (input[i] instanceof Array) {
input[i].forEach(function(value, index) {
input[i + '[' + index + ']'] = value;
});
delete input[i];
}
}
var headers = {"Accept-Encoding": "gzip", "User-Agent": userAgent()};
if (this.key && !(iface == 'IPricing' && ['GetPriceList', 'GetAllLowestListPrices'].indexOf(method) != -1)) {
headers.Authorization = "Basic " + (new Buffer(this.key + ":", 'ascii')).toString('base64');
}
input = QueryString.stringify(input);
if (!iface.match(/^I[A-Z]/)) {
// prepend I if missing
iface = 'I' + iface;
}
var path = "/" + iface + "/" + method + "/v" + version + "/";
if (httpMethod == "GET" && input) {
path += "?" + input;
}
if (httpMethod == "POST" && input) {
headers['Content-Type'] = 'application/x-www-form-urlencoded';
headers['Content-Length'] = input.length;
}
if (this._cfduid) {
headers['Cookie'] = '__cfduid=' + this._cfduid;
}
const base = "api.opskins.com";
this.emit('debug', httpMethod + ' request to https://' + base + path + ' with input: ' + JSON.stringify(rawInput));
var self = this;
var req = Https.request({
"host": base,
"method": httpMethod,
"path": path,
"headers": headers,
"agent": this.getAgent()
}, function(res) {
if (errored) {
return;
}
var err = new Error();
err.httpCode = res.statusCode;
self.emit('debug', "https://" + base + path + " headers: " + JSON.stringify(res.headers));
// Extract __cfduid cookie if it's there
(res.headers['set-cookie'] || []).forEach(function(cookie) {
cookie = cookie.split(';')[0].trim().split('=').map(function(part) { return part.trim(); });
if (cookie[0] == '__cfduid') {
self._cfduid = cookie[1];
}
});
if (res.headers['x-queries-remaining']) {
self.emit('queryLimit', parseInt(res.headers['x-queries-remaining'], 10));
}
if (res.statusCode > 299) {
err.message = (res.statusCode == 401 ? "Invalid or missing API key" : (res.statusMessage || "HTTP error " + res.statusCode));
if (res.headers['cf-ray']) {
err.ray = res.headers['cf-ray'];
}
// Discard the stream
res.on('data', devNull);
callback(err);
return;
}
res.on('error', function(err) {
if (errored) {
return;
}
errored = true;
callback(err);
});
var response = '';
var stream = res;
if (res.headers['content-encoding'] && res.headers['content-encoding'].toLowerCase() == 'gzip') {
// this needs to be decompressed
stream = Zlib.createGunzip();
res.pipe(stream);
}
stream.on('data', function(chunk) {
response += chunk;
});
stream.on('end', function() {
if (errored) {
return;
}
errored = true; // just in case
try {
response = JSON.parse(response);
} catch (e) {
err.message = "Malformed response";
callback(err);
return;
}
if (response.status != ErrorCode.OK) {
err.code = response.status;
if (response.message) {
err.message = response.message;
} else {
// see if we can get the message from the code
for (var i in ErrorCode) {
if (ErrorCode.hasOwnProperty(i) && ErrorCode[i] == response.status) {
err.message = i;
break;
}
}
if (!err.message) {
err.message = "Error " + response.status;
}
}
if (response.response) {
err.data = response.response;
}
callback(err);
return;
}
// Successful query
var res = response.response;
var metadata = response;
delete metadata.response;
if (metadata.time) {
metadata.time = new Date(metadata.time * 1000);
}
callback(null, res, metadata);
});
});
// send the actual request
req.end(httpMethod == "POST" ? input : null);
req.on('error', function(err) {
if (errored) {
return;
}
errored = true;
callback(err);
});
};
OPSkinsAPI.prototype._requireKey = function() {
if (!this.key) {
throw new Error("API key required for this method");
}
};
function userAgent() {
return "node/" + process.versions.node + " node-opskins/" + require('./package.json').version;
}
function devNull() { }
require('./interfaces/ICashout.js');
require('./interfaces/ICrypto.js');
require('./interfaces/IFeed.js');
require('./interfaces/IInventory.js');
require('./interfaces/ILocalization.js');
require('./interfaces/IPayments.js');
require('./interfaces/IPricing.js');
require('./interfaces/ISales.js');
require('./interfaces/IStatus.js');
require('./interfaces/ISupport.js');
require('./interfaces/ITest.js');
require('./interfaces/ITransactions.js');
require('./interfaces/IUser.js');