Repository navigation
Expose Management API rate limit info via a client callback #774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ProdigyTom
wants to merge
4
commits into
auth0:master
Choose a base branch
from
ProdigyTom:feat/expose-rate-limit-data
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
df19130
Expose Management API rate limit info via a client callback (v6)
ProdigyTom 05bd194
Merge branch 'master' into feat/expose-rate-limit-data
kishore7snehil d42e861
Address review feedback on rate limit handler
ProdigyTom 0173174
Raise Auth0::Exception instead of Auth0::Unsupported for handler attach
ProdigyTom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Auth0 | ||
| module Internal | ||
| module Http | ||
| # Rate limit information parsed from the `x-ratelimit-*` headers Auth0 | ||
| # returns on API responses. | ||
| # | ||
| # @see https://auth0.com/docs/troubleshoot/customer-support/operational-policies/rate-limit-policy | ||
| class RateLimit | ||
| # @return [Integer, nil] the maximum number of requests allowed in the current window | ||
| attr_reader :limit | ||
| # @return [Integer, nil] the number of requests remaining in the current window | ||
| attr_reader :remaining | ||
| # @return [Time, nil] the UTC time at which the current window resets | ||
| attr_reader :reset | ||
|
|
||
| # @param limit [Integer, nil] | ||
| # @param remaining [Integer, nil] | ||
| # @param reset [Time, nil] | ||
| def initialize(limit:, remaining:, reset:) | ||
| @limit = limit | ||
| @remaining = remaining | ||
| @reset = reset | ||
| end | ||
|
|
||
| # Build from an object that exposes headers via `#[]` using the HTTP | ||
| # header name (e.g. a `Net::HTTPResponse`, whose `#[]` is | ||
| # case-insensitive). Used by the Management API (RawClient) path. | ||
| # | ||
| # @param response [#[]] responds to `[]` with header access | ||
| # @return [Auth0::Internal::Http::RateLimit] | ||
| def self.from_http_response(response) | ||
| build( | ||
| response["x-ratelimit-limit"], | ||
| response["x-ratelimit-remaining"], | ||
| response["x-ratelimit-reset"] | ||
| ) | ||
| end | ||
|
|
||
| # Build from a plain headers hash (e.g. RestClient's, whose keys are | ||
| # symbols like `:x_ratelimit_remaining`). Keys are matched | ||
| # case-insensitively and dash/underscore-agnostically. Used by the | ||
| # Authentication API (HTTPProxy) path. | ||
| # | ||
| # @param headers [Hash, nil] | ||
| # @return [Auth0::Internal::Http::RateLimit] | ||
| def self.from_headers(headers) | ||
| normalized = (headers || {}).each_with_object({}) do |(key, value), acc| | ||
| acc[key.to_s.downcase.tr("-", "_")] = value | ||
| end | ||
|
|
||
| build( | ||
| normalized["x_ratelimit_limit"], | ||
| normalized["x_ratelimit_remaining"], | ||
| normalized["x_ratelimit_reset"] | ||
| ) | ||
| end | ||
|
|
||
| # @return [Auth0::Internal::Http::RateLimit] | ||
| def self.build(limit, remaining, reset) | ||
| reset_epoch = to_integer(reset) | ||
|
|
||
| new( | ||
| limit: to_integer(limit), | ||
| remaining: to_integer(remaining), | ||
| reset: reset_epoch.nil? ? nil : Time.at(reset_epoch).utc | ||
| ) | ||
| end | ||
| private_class_method :build | ||
|
|
||
| # Parse an integer header value, returning nil for blank or non-numeric | ||
| # input (so a malformed header is never silently reported as 0). | ||
| def self.to_integer(value) | ||
| Integer(value.to_s.strip, exception: false) | ||
| end | ||
| private_class_method :to_integer | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| describe Auth0::Internal::Http::RateLimit do | ||
| module TestRateLimit | ||
| RateLimit = Auth0::Internal::Http::RateLimit | ||
|
|
||
| # Mimics Net::HTTPResponse#[], which is case-insensitive. | ||
| class CaseInsensitiveResponse | ||
| def initialize(headers) | ||
| @headers = headers.transform_keys { |k| k.to_s.downcase } | ||
| end | ||
|
|
||
| def [](name) | ||
| @headers[name.to_s.downcase] | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe ".from_headers" do | ||
| it "parses symbol keys as RestClient returns them" do | ||
| rate_limit = TestRateLimit::RateLimit.from_headers( | ||
| x_ratelimit_limit: "100", | ||
| x_ratelimit_remaining: "42", | ||
| x_ratelimit_reset: "1724000000" | ||
| ) | ||
|
|
||
| _(rate_limit.limit).must_equal 100 | ||
| _(rate_limit.remaining).must_equal 42 | ||
| _(rate_limit.reset).must_equal Time.at(1_724_000_000).utc | ||
| end | ||
|
|
||
| it "parses dashed and mixed-case string keys" do | ||
| rate_limit = TestRateLimit::RateLimit.from_headers("X-RateLimit-Remaining" => "7") | ||
|
|
||
| _(rate_limit.remaining).must_equal 7 | ||
| end | ||
|
|
||
| it "reports a remaining of 0 as an integer, not nil" do | ||
| _(TestRateLimit::RateLimit.from_headers(x_ratelimit_remaining: "0").remaining).must_equal 0 | ||
| end | ||
|
|
||
| it "trims surrounding whitespace" do | ||
| _(TestRateLimit::RateLimit.from_headers(x_ratelimit_limit: " 100 ").limit).must_equal 100 | ||
| end | ||
|
|
||
| it "treats blank or non-numeric values (including reset) as nil instead of a misleading 0" do | ||
| rate_limit = TestRateLimit::RateLimit.from_headers( | ||
| x_ratelimit_limit: "", | ||
| x_ratelimit_remaining: "not-a-number", | ||
| x_ratelimit_reset: "garbage" | ||
| ) | ||
|
|
||
| _(rate_limit.limit).must_be_nil | ||
| _(rate_limit.remaining).must_be_nil | ||
| _(rate_limit.reset).must_be_nil | ||
| end | ||
| end | ||
|
|
||
| describe ".from_http_response" do | ||
| it "reads headers case-insensitively via the response's #[]" do | ||
| response = TestRateLimit::CaseInsensitiveResponse.new( | ||
| "X-RateLimit-Limit" => "100", | ||
| "X-RateLimit-Remaining" => "9", | ||
| "X-RateLimit-Reset" => "1724000000" | ||
| ) | ||
|
|
||
| rate_limit = TestRateLimit::RateLimit.from_http_response(response) | ||
|
|
||
| _(rate_limit.limit).must_equal 100 | ||
| _(rate_limit.remaining).must_equal 9 | ||
| _(rate_limit.reset).must_equal Time.at(1_724_000_000).utc | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reaches into the generated Management client for its
@raw_clientby instance variable, and theif raw_clientmeans that if a future regeneration renames or restructures that variable, the handler just never gets attached and the feature goes quiet with no error anywhere. This file is kept across regeneration but the generated class it reaches into is not, so the two can drift apart and nothing would flag it.We should be handling this carefully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made this raise a
Auth0::Unsupportederror for now so that a future internals change surfaces immediately instead of silently skipping.I'm using the ivar because the generated
Auth0::Managementdoesn't expose its raw client, and since that class is regenerated, we can't add an accessor from our side without it being clobbered. If you'd be open to exposing a public reader for the raw client on the generated Management class, I would switch to it and drop the instance_variable_get entirely.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A public reader on
Auth0::Managementwould be the cleaner fix, but that class is generated, so a hand edit would be overwritten on the next regen. It would need to come from the Fern side, so not blocking this PR on it.The
respond_to?check with the raise is enough for now, since a rename would fail loudly instead of silently dropping the handler.One small question on the error.
Auth0::Unsupportedis part of the HTTP error family, and this isn't an HTTP failure. Would a plainAuth0::Exceptionfit better here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh ya that makes sense, thanks for pointing that out. made the change.