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 |
1×
1×
1×
1×
1×
1×
1×
3×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1× | 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _URLHelper = require('../Utils/URLHelper');
var _URLHelper2 = _interopRequireDefault(_URLHelper);
var _RegionHelper = require('../Utils/RegionHelper');
var _RegionHelper2 = _interopRequireDefault(_RegionHelper);
var _riotRatelimiter = require('riot-ratelimiter');
var _riotRatelimiter2 = _interopRequireDefault(_riotRatelimiter);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
require('babel-polyfill');
var limiter = new _riotRatelimiter2.default({ debug: true });
function Request(config, serviceName, endpoint, methodName, httpMethodType) {
this.payload = {
method: httpMethodType || 'GET',
serviceName: serviceName,
endpoint: endpoint || '',
query: [],
region: ''
};
this.config = config;
this.retriesLeft = this.config.requestOptions.numberOfRetriesBeforeAbort;
this.methodName = methodName;
}
Request.prototype.region = function (region) {
if (region) this.payload.region = region;
return this;
};
Request.prototype.query = function (obj) {
if (obj) this.payload.query.push(obj);
return this;
};
Request.prototype.then = function then(resolve, reject) {
var self = this;
return new Promise(function (innerResolve, innerReject) {
return self.callback(function (err, res) {
return err ? innerReject(err) : innerResolve(res);
});
}).then(resolve, reject);
};
Request.prototype.catch = function (callback) {
return undefined.then(null, callback);
};
Request.prototype.callback = function (cb) {
if (!this.payload.region) {
this.payload.region = this.config.region;
}
var _payload = this.payload,
endpoint = _payload.endpoint,
query = _payload.query,
region = _payload.region,
serviceName = _payload.serviceName;
var url = _URLHelper2.default.getURLWithQuery(_RegionHelper2.default.asPlatformID(region), serviceName, endpoint, query);
var token = this.config.key;
this.execute(url, token, this.retriesLeft, cb);
};
Request.prototype.execute = function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(url, token, retriesLeft, cb) {
var _this = this;
var _config, cacheOptions, debugOptions, requestOptions, fn;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_config = this.config, cacheOptions = _config.cacheOptions, debugOptions = _config.debugOptions, requestOptions = _config.requestOptions;
fn = function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(err, data) {
var debugURL, res, blob, statusCode;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
debugURL = url + (debugOptions.showKey ? _URLHelper2.default.getAPIKey(url, token) : '');
_context.prev = 1;
if (!data) {
_context.next = 7;
break;
}
if (debugOptions.isEnabled) {
console.log('CACHE HIT @', debugURL);
}
ok(data)(cb);
_context.next = 12;
break;
case 7:
_context.next = 9;
return limiter.executing({ url: url, token: token });
case 9:
res = _context.sent;
if (debugOptions.isEnabled) {
console.log('200 @', debugURL);
}
try {
blob = JSON.parse(res);
cacheOptions && cacheOptions.cache && cacheOptions.cache.set({ key: url, ttl: cacheOptions.ttls[_this.methodName] }, blob);
ok(blob)(cb);
} catch (ex) {
console.log(ex);
}
case 12:
_context.next = 22;
break;
case 14:
_context.prev = 14;
_context.t0 = _context['catch'](1);
statusCode = _context.t0.statusCode;
if (debugOptions.isEnabled) {
console.log(statusCode, '@', debugURL);
}
if (!(requestOptions.shouldRetry && shouldRetry(statusCode))) {
_context.next = 21;
break;
}
if (!(retriesLeft > 0)) {
_context.next = 21;
break;
}
return _context.abrupt('return', setTimeout(function () {
return _this.execute(url, token, retriesLeft - 1, cb);
}, requestOptions.delayBeforeRetry));
case 21:
error(statusCode)(cb);
case 22:
case 'end':
return _context.stop();
}
}
}, _callee, _this, [[1, 14]]);
}));
return function fn(_x5, _x6) {
return _ref2.apply(this, arguments);
};
}();
if (cacheOptions.cache) {
cacheOptions.cache.get({ key: url }, fn);
} else {
fn();
}
case 3:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
return function (_x, _x2, _x3, _x4) {
return _ref.apply(this, arguments);
};
}();
var shouldRetry = function shouldRetry(statusCode) {
return statusCode === 500 || statusCode === 503;
};
var ok = function ok(blob) {
return function (cb) {
return cb(null, blob);
};
};
var error = function error(statusCode) {
return function (cb) {
return cb(statusCode, null);
};
};
exports.default = Request; |