Skip to content

Commit

Permalink
Merge pull request #22 from clearbit/dc-add-timeout
Browse files Browse the repository at this point in the history
Add timeout
  • Loading branch information
dcadenas committed Nov 9, 2016
2 parents f53e0c1 + b62c9fe commit 7470a6f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,7 @@ var clearbit = new Client({key: 'api_key'});
* `webhook_id` *String*: Custom identifier for the webhook request
* `subscribe` *Boolean*: Set to `true` to subscribe to the changes
* `stream` *Boolean*: Set to `true` to use the [streaming API](https://clearbit.com/docs?shell#streaming) instead of webhooks
* `timeout` *Integer*: The timeout in milliseconds after which a socket closed error will be thrown.

```js
var Person = clearbit.Person;
Expand All @@ -51,6 +52,7 @@ Person.find({email: 'email@domain.com'})
* `domain` *String*: The company domain to look up **(required)**
* `webhook_id` *String*: Custom identifier for the webhook request
* `stream` *Boolean*: Set to `true` to use the [streaming API](https://clearbit.com/docs?shell#streaming) instead of webhooks
* `timeout` *Integer*: The timeout in milliseconds after which a socket closed error will be thrown.

```js
var Company = clearbit.Company;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "clearbit",
"version": "1.2.3",
"version": "1.3.0",
"description": "Client for Clearbit.co business intelligence APIs",
"main": "./src",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/client.js
Expand Up @@ -59,14 +59,16 @@ ClearbitClient.prototype.request = function (options) {
method: 'get'
});

var timeout = options.timeout || options.stream && 60000 || 10000;

return needle.requestAsync(
options.method,
this.url(options),
options.body || options.query,
{
json: options.json,
headers: options.headers,
timeout: options.stream ? 60000 : 10000,
timeout: timeout,
username: this.key,
password: '',
user_agent: 'ClearbitNode/v' + pkg.version
Expand Down
2 changes: 1 addition & 1 deletion src/resource.js
Expand Up @@ -111,7 +111,7 @@ function extractParams (options) {
var params = _.omit(options || {},
'path', 'method', 'params',
'client', 'api', 'stream',
'headers'
'headers', 'timeout'
);

return _.isEmpty(params) ? null : params;
Expand Down
47 changes: 40 additions & 7 deletions test/client.js
Expand Up @@ -100,18 +100,51 @@ describe('Client', function () {
});
});

it('uses a timeout of 60 seconds for streaming requests', function () {
function requestWithOptions(clientRequestOptions) {
sinon.stub(needle, 'request').yieldsAsync(null, {}, undefined);
return client.request({

return client
.request(clientRequestOptions)
.then(function() { return needle.request.firstCall.args[3]; })
.finally(function() { return needle.request.restore(); });
}

it('can specify a custom timeout for a request', function () {
return requestWithOptions({
api: 'person',
timeout: 30000
})
.then(function(needleOptions) {
expect(needleOptions).to.have.property('timeout', 30000);
});
});

it('sets the default timeout to 10 seconds', function () {
return requestWithOptions({ api: 'person' })
.then(function(needleOptions) {
expect(needleOptions).to.have.property('timeout', 10000);
});
});


it('uses a default timeout of 60 seconds for streaming requests', function () {
return requestWithOptions({
api: 'person',
stream: true
})
.then(function () {
expect(needle.request.firstCall.args[3])
.to.have.property('timeout', 60000);
.then(function(needleOptions) {
expect(needleOptions).to.have.property('timeout', 60000);
});
});

it('can specify a custom timeout for streaming requests', function () {
return requestWithOptions({
api: 'person',
stream: true,
timeout: 30000
})
.finally(function () {
needle.request.restore();
.then(function(needleOptions) {
expect(needleOptions).to.have.property('timeout', 30000);
});
});

Expand Down
7 changes: 7 additions & 0 deletions test/person.js
Expand Up @@ -38,6 +38,13 @@ describe('Person', function () {
return Person.find({email: 'alex@alexmaccaw.com', subscribe: true});
});

it('removes non query options from the url', function () {
mock
.get('/v2/people/find?email=alex%40alexmaccaw.com')
.reply(200, alex);
return Person.find({email: 'alex@alexmaccaw.com', timeout: 10000});
});

it('can handle queued requests', function () {
mock
.get('/v2/people/find?email=alex%40alexmaccaw.com')
Expand Down

0 comments on commit 7470a6f

Please sign in to comment.