-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase.js
123 lines (110 loc) · 3.16 KB
/
base.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
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
'use strict';
const Needle = require('needle');
const Errors = require('../errors');
const InvalidResponseError = Errors.InvalidResponseError;
class BaseAdapter {
constructor (config, logger) {
this._config = config || {};
this._logger = logger || console;
this._supportedFormats = this._config.supportedFormats || BaseAdapter.defaultSupportedFormats;
this._retryCount = this._config.retryCount || 3; // number of automatic retries before giving up
this._retryBackoff = this._config.retryBackoff || 250; // milliseconds between retries... increases linearly
this._requests = {};
}
static get defaultSupportedFormats () {
return [
'prettywkt',
'wkt',
'esriwkt',
'proj4',
'js',
'usgs',
'geoserver',
'mapfile',
'mapnik',
'sql'
];
}
isValidCRS (crs) {
if (crs === 'local') {
return true;
}
else if (crs.indexOf('arbitrary') !== -1) {
return true;
}
else {
const code = crs.replace('epsg:', '');
if (code.match('[0-9]+') && code.length < 7) {
return true;
} else {
return false;
}
}
}
isValidCRSFormat (format) {
return this._supportedFormats.indexOf(format) !== -1;
}
get (crs, format) { // eslint-disable-line no-unused-vars
this._logger.error('This method should be overridden by the subclass');
}
makeRequest (method, url, data, options) {
let requests = this._requests[url];
if (!requests || !requests.length) {
requests = this._requests[url] = this._prepareRequests(method, url, data, options);
}
const request = requests.shift();
if (requests.length === 0) {
this._logger.info('Last retry...');
return request();
}
else {
return request()
.catch(() => {
this._logger.info(`Retrying request for ${url}... ${this._retryCount - requests.length}`);
return this.makeRequest(method, url, data, options);
})
.then((res) => {
requests = [];
return res;
});
}
}
_makeRequest (method, url, data, options) {
options = options || {};
return new Promise((resolve, reject) => {
this._logger.info(`Sending request for ${url}`);
Needle.request(method, url, data, options, (err, res) => {
if (err) {
reject(err);
}
else if (res.statusCode !== 200) {
reject(new InvalidResponseError(res.statusCode, res.body, {
url: url,
adapter: this.constructor.name,
method: method,
data: data
}));
}
else {
resolve(res.body);
}
});
});
}
_prepareRequests (method, url, data, options) {
const requests = [];
for (var i = 0; i < this._retryCount; i++) {
const makeRequestFn = this._makeRequest.bind(this, method, url, data, options);
const wait = this._retryBackoff * i;
requests.push(function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
makeRequestFn().then(resolve).catch(reject);
}, wait);
});
});
}
return requests;
}
}
module.exports = BaseAdapter;