familysearch
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -10,5 +10,16 @@ | ||
function accessorize(link) { | ||
if (true || !link.type) return 'application/json' | ||
var fs = 'application/x-fs-v1+json' | ||
, gx = 'application/x-gedcomx-v1+json' | ||
if (link.type.indexOf(gx) !== -1) return gx | ||
if (link.type.indexOf(fs) !== -1) return fs | ||
return 'application/json' | ||
} | ||
var FS = function FS() { | ||
this.links = null; | ||
this.ondone = []; | ||
this.queue = {} | ||
this.queuing = {}; | ||
this.getMeta(); | ||
@@ -26,4 +37,9 @@ }; | ||
error('Unable to get app-meta', err); | ||
throw new Error('Familysearch api failing?') | ||
return; | ||
} | ||
if (res.status !== 400 && !res.body.links) { | ||
console.error('links meta returned unexpected response', res.status, res.header, res.text) | ||
throw new Error('meta links bad') | ||
} | ||
self.links = res.body.links; | ||
@@ -45,3 +61,3 @@ debug('Got meta links', self.links.length); | ||
// next(err, body, res) | ||
getRaw: function (url, token, etag, next) { | ||
getRaw: function (endpoint, options, token, etag, next) { | ||
if (arguments.length === 3) { | ||
@@ -51,5 +67,11 @@ next = etag | ||
} | ||
requestlog('Getting', url, token); | ||
var req = request.get(url) | ||
req = req.set('Accept', 'application/json') | ||
if (!this.links[endpoint]) { | ||
error('Invalid endpoint', endpoint); | ||
return next('Invalid endpoint'); | ||
} | ||
var url = this.links[endpoint].href || this.links[endpoint].url(options) | ||
, req = request.get(url) | ||
, accept = accessorize(this.links[endpoint]) | ||
debug('Getting', url, this.links[endpoint].type, accept, token) | ||
req = req.set('Accept', accept) | ||
.set('Authorization', 'Bearer ' + token) | ||
@@ -62,8 +84,11 @@ if (etag) { | ||
error('Error', err.message); | ||
return next(err); | ||
return next(err, null, res); | ||
} | ||
requestlog('from url', url, res.body); | ||
if (res.body.code && res.body.code !== 200) { | ||
return next(res.body); | ||
return next(res.body, null, res); | ||
} | ||
if (res.header.warning) { | ||
error('Got a warning from familysearch:', res.header.warning, url, token, etag) | ||
} | ||
next(null, res.body, res); | ||
@@ -74,2 +99,3 @@ }); | ||
get: function (endpoint, options, token, etag, next) { | ||
var self = this | ||
if (arguments.length === 4) { | ||
@@ -79,8 +105,36 @@ next = etag | ||
} | ||
if (!this.links[endpoint]) { | ||
error('Invalid endpoint', endpoint); | ||
return next('Invalid endpoint'); | ||
if (this.queuing[endpoint]) return this.enqueue(endpoint, options, token, etag, next) | ||
return this.getRaw(endpoint, options, token, etag, function (err, data, res) { | ||
if (err && err.code === 429) { | ||
console.log('Got throttled!', res && res.header) | ||
return self.enqueue(endpoint, options, token, etag, next, parseInt(res.header['retry-after'], 10) || 500) | ||
} | ||
next(err, data, res) | ||
}); | ||
}, | ||
enqueue: function (endpoint, options, token, etag, next, wait) { | ||
if (!this.queue[endpoint]) this.queue[endpoint] = [] | ||
this.queue[endpoint].push([options, token, etag, next]) | ||
if (!this.queuing[endpoint]) { | ||
this.queuing[endpoint] = true | ||
setTimeout(this.dequeue.bind(this, endpoint), wait || 500) | ||
} | ||
var url = this.links[endpoint].href || this.links[endpoint].url(options); | ||
return this.getRaw(url, token, etag, next); | ||
}, | ||
dequeue: function (endpoint) { | ||
if (!this.queue[endpoint]) return console.error('Queue not there while dequeuing', endpoint) | ||
if (!this.queue[endpoint].length) { | ||
this.queuing[endpoint] = false | ||
return | ||
} | ||
var next = this.queue[endpoint].shift() | ||
, self = this | ||
this.getRaw(endpoint, next[0], next[1], next[2], function (err, data, res) { | ||
if (err && err.code === 429) { | ||
console.log('Got throttled again!', res.header) | ||
self.queue[endpoint].unshift(next) | ||
return setTimeout(self.dequeue.bind(self, endpoint), parseInt(res.header['retry-after'], 10) || 500) | ||
} | ||
next[3](err, data, res) | ||
self.dequeue(endpoint) | ||
}) | ||
} | ||
@@ -95,3 +149,3 @@ }; | ||
single: function () { | ||
if (single == null) single = new FS(); | ||
if (single === null) single = new FS(); | ||
return single; | ||
@@ -98,0 +152,0 @@ } |
{ | ||
"name": "familysearch", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "FamilySearch API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8615
10
193