-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathendpoint.js
More file actions
537 lines (433 loc) · 16 KB
/
endpoint.js
File metadata and controls
537 lines (433 loc) · 16 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
const qs = require('querystringify')
const unique = require('array-unique')
const chunk = require('chunk')
const hashString = require('./hash')
const clone = (x) => JSON.parse(JSON.stringify(x))
module.exports = class AbstractEndpoint {
constructor (parent) {
this.client = parent.client
this.schemaVersion = parent.schemaVersion || '2019-03-20T00:00:00.000Z'
this.lang = parent.lang
this.apiKey = parent.apiKey
this.fetch = parent.fetch
this.caches = parent.caches
this.debug = parent.debug
this.baseUrl = 'https://api.guildwars2.com'
this.isPaginated = false
this.maxPageSize = 200
this.isBulk = false
this.bulkId = 'id'
this.supportsBulkAll = true
this.isLocalized = false
this.isAuthenticated = false
this.isOptionallyAuthenticated = false
this.credentials = false
this._skipCache = false
}
// Set the schema version
schema (schema) {
this.schemaVersion = schema
this.debugMessage(`set the schema to ${schema}`)
return this
}
// Check if the schema version includes a specific version
_schemaIncludes (date) {
return this.schemaVersion >= date
}
// Set the language for locale-aware endpoints
language (lang) {
this.lang = lang
this.debugMessage(`set the language to ${lang}`)
return this
}
// Set the api key for authenticated endpoints
authenticate (apiKey) {
this.apiKey = apiKey
this.debugMessage(`set the api key to ${apiKey}`)
return this
}
// Set the debugging flag
debugging (flag) {
this.debug = flag
return this
}
// Print out a debug message if debugging is enabled
debugMessage (string) {
if (this.debug) {
console.log(`[gw2api-client] ${string}`)
}
}
// Skip caching and get the live data
live () {
this._skipCache = true
this.debugMessage(`skipping cache`)
return this
}
// Get all ids
ids () {
this.debugMessage(`ids(${this.url}) called`)
if (!this.isBulk) {
return Promise.reject(new Error('"ids" is only available for bulk expanding endpoints'))
}
// There is no cache time set, so always use the live data
if (!this.cacheTime) {
return this._ids()
}
// Get as much as possible out of the cache
const hash = this._cacheHash('ids')
const handleCacheContent = (cached) => {
if (cached) {
this.debugMessage(`ids(${this.url}) resolving from cache`)
return cached
}
return this._ids().then(content => {
this._cacheSetSingle(hash, content)
return content
})
}
// Get the content either from the cache or API, write it into the cache and return a clone
const contentPromise = this._skipCache
? Promise.resolve(false).then(handleCacheContent)
: this._cacheGetSingle(hash).then(handleCacheContent)
return contentPromise.then(clone)
}
// Get all ids from the live API
_ids () {
this.debugMessage(`ids(${this.url}) requesting from api`)
return this._request(this.url)
}
// Get a single entry by id
get (id, url = false) {
this.debugMessage(`get(${this.url}) called`)
if (!id && this.isBulk && !url) {
return Promise.reject(new Error('"get" requires an id'))
}
// There is no cache time set, so always use the live data
if (!this.cacheTime) {
return this._get(id, url)
}
// Get as much as possible out of the cache
const hash = this._cacheHash(id)
const handleCacheContent = (cached) => {
if (cached) {
this.debugMessage(`get(${this.url}) resolving from cache`)
return cached
}
return this._get(id, url).then(content => {
this._cacheSetSingle(hash, content)
return content
})
}
// Get the content either from the cache or API, write it into the cache and return a clone
const contentPromise = this._skipCache
? Promise.resolve(false).then(handleCacheContent)
: this._cacheGetSingle(hash).then(handleCacheContent)
return contentPromise.then(clone)
}
// Get a single entry by id from the live API
_get (id, url) {
this.debugMessage(`get(${this.url}) requesting from api`)
// Request the single id if the endpoint a bulk endpoint
if (this.isBulk && !url) {
return this._request(`${this.url}?id=${id}`)
}
// We are dealing with a custom url instead
if (url) {
return this._request(this.url + id)
}
// Just request the base url
return this._request(this.url)
}
// Get multiple entries by ids
many (ids) {
this.debugMessage(`many(${this.url}) called (${ids.length} ids)`)
if (!this.isBulk) {
return Promise.reject(new Error('"many" is only available for bulk expanding endpoints'))
}
// Exit out early if we don't request any ids
if (ids.length === 0) {
return Promise.resolve([])
}
// Always only work on unique ids, since that's how the API works
ids = unique.immutable(ids)
// There is no cache time set, so always use the live data
if (!this.cacheTime) {
return this._many(ids)
}
// Get as much as possible out of the cache
const hashes = ids.map(id => this._cacheHash(id))
const handleCacheContent = (cached) => {
cached = cached.filter(x => x)
if (cached.length === ids.length) {
this.debugMessage(`many(${this.url}) resolving fully from cache`)
return cached
}
this.debugMessage(`many(${this.url}) resolving partially from cache (${cached.length} ids)`)
const missingIds = getMissingIds(ids, cached)
return this._many(missingIds, cached.length > 0).then(content => {
const cacheContent = content.map(value => [this._cacheHash(value[this.bulkId]), value])
this._cacheSetMany(cacheContent)
// Merge the new content with the cached content and guarantee element order
content = content.concat(cached)
return this._sortByIdList(content, ids)
})
}
// Find the ids that are missing in the cached data
const getMissingIds = (ids, cached) => {
const cachedIds = {}
cached.map(x => {
cachedIds[x[this.bulkId]] = 1
})
return ids.filter(x => cachedIds[x] !== 1)
}
// Get the content either from the cache or API, write it into the cache and return a clone
const contentPromise = this._skipCache
? Promise.resolve([]).then(handleCacheContent)
: this._cacheGetMany(hashes).then(handleCacheContent)
return contentPromise.then(clone)
}
// Get multiple entries by ids from the live API
_many (ids, partialRequest = false) {
this.debugMessage(`many(${this.url}) requesting from api (${ids.length} ids)`)
// Chunk the requests to the max page size
const pages = chunk(ids, this.maxPageSize)
const requests = pages.map(page => `${this.url}?ids=${page.join(',')}`)
// If we are partially caching and all not-cached ids are all invalid,
// simulate the API behaviour by silently swallowing errors.
let handleMissingIds = (err) => {
/* istanbul ignore else */
if (partialRequest && err.response && err.response.status === 404) {
return Promise.resolve([])
}
/* istanbul ignore next */
return Promise.reject(err)
}
// Work on all requests in parallel and then flatten the responses into one
return this._requestMany(requests)
.then(responses => responses.reduce((x, y) => x.concat(y), []))
.catch(handleMissingIds)
}
// Get a single page
page (page, size = this.maxPageSize) {
this.debugMessage(`page(${this.url}) called`)
if (!this.isBulk && !this.isPaginated) {
return Promise.reject(new Error('"page" is only available for bulk expanding or paginated endpoints'))
}
if (size > this.maxPageSize || size <= 0) {
return Promise.reject(new Error(`"size" has to be between 0 and ${this.maxPageSize}, was ${size}`))
}
if (page < 0) {
return Promise.reject(new Error('page has to be 0 or greater'))
}
// There is no cache time set, so always use the live data
if (!this.cacheTime) {
return this._page(page, size)
}
// Get as much as possible out of the cache
const hash = this._cacheHash('page-' + page + '/' + size)
const handleCacheContent = (cached) => {
if (cached) {
this.debugMessage(`page(${this.url}) resolving from cache`)
return cached
}
return this._page(page, size).then(content => {
let cacheContent = [[hash, content]]
if (this.isBulk) {
cacheContent = cacheContent.concat(content.map(value => [this._cacheHash(value[this.bulkId]), value]))
}
this._cacheSetMany(cacheContent)
return content
})
}
// Get the content either from the cache or API, write it into the cache and return a clone
const contentPromise = this._skipCache
? Promise.resolve(false).then(handleCacheContent)
: this._cacheGetSingle(hash).then(handleCacheContent)
return contentPromise.then(clone)
}
// Get a single page from the live API
_page (page, size) {
this.debugMessage(`page(${this.url}) requesting from api`)
return this._request(`${this.url}?page=${page}&page_size=${size}`)
}
// Get all entries
all () {
this.debugMessage(`all(${this.url}) called`)
if (!this.isBulk && !this.isPaginated) {
return Promise.reject(new Error('"all" is only available for bulk expanding or paginated endpoints'))
}
// There is no cache time set, so always use the live data
if (!this.cacheTime) {
return this._all()
}
// Get as much as possible out of the cache
const hash = this._cacheHash('all')
const handleCacheContent = (cached) => {
if (cached) {
this.debugMessage(`all(${this.url}) resolving from cache`)
return cached
}
return this._all().then(content => {
let cacheContent = [[hash, content]]
if (this.isBulk) {
cacheContent = cacheContent.concat(content.map(value => [this._cacheHash(value[this.bulkId]), value]))
}
this._cacheSetMany(cacheContent)
return content
})
}
// Get the content either from the cache or API, write it into the cache and return a clone
const contentPromise = this._skipCache
? Promise.resolve(false).then(handleCacheContent)
: this._cacheGetSingle(hash).then(handleCacheContent)
return contentPromise.then(clone)
}
// Get all entries from the live API
_all () {
this.debugMessage(`all(${this.url}) requesting from api`)
// Use bulk expansion if the endpoint supports the "all" keyword
if (this.isBulk && this.supportsBulkAll) {
return this._request(`${this.url}?ids=all`)
}
// Get everything via all pages instead
let totalEntries
return this._request(`${this.url}?page=0&page_size=${this.maxPageSize}`, 'response')
.then(firstPage => {
// Get the total number of entries off the first page's headers
totalEntries = firstPage.headers.get('X-Result-Total')
return firstPage.json()
})
.then(result => {
// Return early if the first page already includes all entries
if (totalEntries <= this.maxPageSize) {
return result
}
// Request all missing pages in parallel
let requests = []
for (let page = 1; page < Math.ceil(totalEntries / this.maxPageSize); page++) {
requests.push(`${this.url}?page=${page}&page_size=${this.maxPageSize}`)
}
return this._requestMany(requests).then(responses => {
responses = responses.reduce((x, y) => x.concat(y), [])
return result.concat(responses)
})
})
}
// Set a single cache key in all connected cache storages
_cacheSetSingle (key, value) {
this.caches.map(cache => {
cache.set(key, value, this.cacheTime).catch(error => {
console.warn('[gw2api-client] Errored during _cacheSetSingle', { error, cache, key, value })
})
})
}
// Set multiples cache key in all connected cache storages
_cacheSetMany (values) {
values = values.map(value => [value[0], value[1], this.cacheTime])
this.caches.map(cache => {
cache.mset(values).catch(error => {
console.warn('[gw2api-client] Errored during _cacheSetMany', { error, cache, values })
})
})
}
// Get a cached value out of the first possible connected cache storages
_cacheGetSingle (key, index = 0) {
return this.caches[index].get(key).then(value => {
if (value || index === this.caches.length - 1) {
return value
}
return this._cacheGetSingle(key, ++index)
})
}
// Get multiple cached values out of the first possible connected cache storages
_cacheGetMany (keys, index = 0) {
return this.caches[index].mget(keys).then(values => {
const cleanValues = values.filter(x => x)
// We got all the requested keys or are through all storages
if (cleanValues.length === keys.length || index === this.caches.length - 1) {
return values
}
// Try to ask the next storage for the keys that we didn't get
let missingKeys = values
.map((value, i) => value ? false : keys[i])
.filter(value => value)
// Then merge the values of the next storage into the missing slots
return this._cacheGetMany(missingKeys, ++index).then(missingValues => {
let i = 0
return values.map(value => value || missingValues[i++])
})
})
}
// Get a cache hash for an identifier
_cacheHash (id) {
let hash = hashString(this.baseUrl + this.url + ':' + this.schemaVersion)
if (id) {
hash += ':' + id
}
if (this.isLocalized) {
hash += ':' + this.lang
}
if (this._usesApiKey()) {
hash += ':' + hashString(this.apiKey + '')
}
return hash
}
// Execute a single request
_request (url, type = 'json') {
url = this._buildUrl(url)
/* istanbul ignore next */
const credentials = this.credentials ? 'include' : undefined
return this.fetch.single(url, { type, credentials })
}
// Execute multiple requests in parallel
async _requestMany (urls, type = 'json') {
urls = urls.map(url => this._buildUrl(url))
/* istanbul ignore next */
const credentials = this.credentials ? 'include' : undefined
// rudimentry rate limit on single queries (e.g. api.items().all())
const BURST_LIMIT = 300 // hardcoded; 600 limit exposed in headers is inaccurate
const WAIT_TIME = 1 // lets-fetch adds wait time to sequential response rather than enforcing minimum sequential delay
const burst = await this.fetch.many(urls.slice(0, BURST_LIMIT - 1), { type, credentials })
const remaining = await this.fetch.many(urls.slice(BURST_LIMIT), { type, credentials, waitTime: WAIT_TIME })
return [...burst, ...remaining]
}
// Build the headers for localization and authentication
_buildUrl (url) {
// Add the base url
url = this.baseUrl + url
// Parse a possibly existing query
const parsedUrl = url.split('?')
let parsedQuery = qs.parse(parsedUrl[1] || '')
let query = {}
// Set the schema version
query['v'] = this.schemaVersion
// Only set the API key for authenticated endpoints,
// when it is required or optional and set on the client
if (this._usesApiKey()) {
query['access_token'] = this.apiKey
}
// Set the language for localized endpoints
if (this.isLocalized) {
query['lang'] = this.lang
}
// Merge the parsed query parts out of the url
query = Object.assign(query, parsedQuery)
// Build the url with the finished query
query = qs.stringify(query, true).replace(/%2C/g, ',')
return parsedUrl[0] + query
}
// Guarantee the element order of bulk results
_sortByIdList (entries, ids) {
// Hash map of the indexes for better time complexity on big arrays
let indexMap = {}
ids.map((x, i) => {
indexMap[x] = i
})
// Sort by the indexes
entries.sort((a, b) => indexMap[a[this.bulkId]] - indexMap[b[this.bulkId]])
return entries
}
_usesApiKey () {
return this.isAuthenticated && (!this.isOptionallyAuthenticated || this.apiKey)
}
}