Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Add option to ignore limit check, use headers for limit check #152

Merged
merged 7 commits into from
Feb 9, 2019

Conversation

AustinLeeGordon
Copy link
Contributor

Why

Currently uses up 2 API calls for each call. This adds a simple option to the constructor to ignore it:

const Hubspot = require('hubspot');
const hubspot = new Hubspot({
    apiKey: 'key',
    checkLimit: false // Ignores API limit check
});

A more elegant solution needs to be put in place for rate limit checking (something involving the API response headers), so this will be removed in the future.

This change addresses the need by:

Also

  • fixed npm scripts and prettier/editorconfig settings to work cross env
  • and corrected some style issues in contributing.md and README.md.

- Running prettier on Windows caused all files to change from CRLF to LF
- npm scripts didn't work on Windows
- Also fixed syntax in contributing.md

Fixed list syntax in contributing.md
- Added `checkLimit` option to ignore API limit check
@AustinLeeGordon
Copy link
Contributor Author

Realized that the maxUsePercent option already allows for ignoring the check. I'm almost finished implementing a fix using headers from the API response.

@AustinLeeGordon AustinLeeGordon changed the title Add option to ignore API limit check WIP Add option to ignore API limit check Jan 11, 2019
@pcothenet
Copy link
Contributor

Awesome PR @AustinLeeGordon . Really appreciated!

I think I had confirmed with HubSpot that the checkLimit call does not count towards the API limit but I see the need for making it less chatty.

@AustinLeeGordon AustinLeeGordon changed the title WIP Add option to ignore API limit check WIP Add option to ignore limit check, use headers for limit check Jan 14, 2019
@AustinLeeGordon AustinLeeGordon changed the title WIP Add option to ignore limit check, use headers for limit check Add option to ignore limit check, use headers for limit check Jan 14, 2019
@AustinLeeGordon
Copy link
Contributor Author

AustinLeeGordon commented Jan 14, 2019

Commit 76e937e uses response headers from each request to update the current usage. @pcothenet You're right - tested getApiLimit and it didn't count toward the usage. Definitely better to only make one request though!

I left in getApiLimit even though it's not being used in the request. I figured it was still useful for specifically checking the limit. Also keeps the tests passing.

The request doesn't do a check on the first call since updateApiLimit is called after receiving the first response. I can add in a conditional to fall back on getApiLimit for the first request and update the usageLimit and currentUsage from it if you'd like!

@chrisbaldauf
Copy link
Collaborator

@pcothenet I checked and the Check daily API usage endpoint does actually count against the daily and burst (per second) limit. I'm going to look into whether or not we can safely relax that restriction.

In general, I recommend using the x-hubspot-ratelimit-* headers, but please be aware that those headers will be absent when using OAuth tokens or in some exceptional cases.

Disclaimer: I work for HubSpot

I ran this experiment against an isolated account and I'm sure this was the only API traffic in flight at the time.

$ curl https://api.hubapi.com/integrations/v1/limit/daily?hapikey=$HK -i
HTTP/1.1 200 OK
...
X-HubSpot-RateLimit-Daily: 40000
X-HubSpot-RateLimit-Daily-Remaining: 39999
X-HubSpot-RateLimit-Secondly: 10
X-HubSpot-RateLimit-Secondly-Remaining: 9
...

$ curl https://api.hubapi.com/integrations/v1/limit/daily?hapikey=$HK -i
HTTP/1.1 200 OK
...
X-HubSpot-RateLimit-Daily: 40000
X-HubSpot-RateLimit-Daily-Remaining: 39998
X-HubSpot-RateLimit-Secondly: 10
X-HubSpot-RateLimit-Secondly-Remaining: 9
...

@AustinLeeGordon
Copy link
Contributor Author

@chrisbaldauf Thanks for checking into that! For some reason it seemed to count for mine in some cases but didn't in others. Might have just missed something. The latest few commits in this thread change it to check from the x-hubspot-ratelimit-* headers instead of making another call.

@pcothenet
Copy link
Contributor

Thanks @chrisbaldauf @AustinLeeGordon . I'll be able to review and hopefully GTM this weekend.

@pcothenet
Copy link
Contributor

@chrisbaldauf can you also shed some light on why the headers are not returned when using OAuth? Seems like a surprising design decision, no?

@AustinLeeGordon
Copy link
Contributor Author

Hey @pcothenet! Any update on this? Let me know if there's anything I can do to help!

@pcothenet
Copy link
Contributor

@AustinLeeGordon I'm still quite confused by @chrisbaldauf comment about "please be aware that those headers will be absent when using OAuth tokens".

I know that in my use case, I was relying on this check to not blow up the API limit (and we're using OAuth)

@pcothenet
Copy link
Contributor

But all things considered, I think this is an improvement. Merging.

@pcothenet pcothenet merged commit bcc85f5 into MadKudu:master Feb 9, 2019
@AustinLeeGordon
Copy link
Contributor Author

@pcothenet Glad to see it go through! I haven’t used it with OAuth, but does it typically count toward the api count whenever using OAuth? If not, then this shouldn’t add any unnecessary calls, it just wouldn’t check the headers for it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants