Skip to content
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

Allow using the response_headers or headers when getting Rate Limit Info #1533

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/octokit/rate_limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class RateLimit < Struct.new(:limit, :remaining, :resets_at, :resets_in)
# @return [RateLimit]
def self.from_response(response)
info = new
if response.respond_to?(:headers) && !response.headers.nil?
info.limit = (response.headers['X-RateLimit-Limit'] || 1).to_i
info.remaining = (response.headers['X-RateLimit-Remaining'] || 1).to_i
info.resets_at = Time.at((response.headers['X-RateLimit-Reset'] || Time.now).to_i)
headers = response.headers if response.respond_to?(:headers) && !response.headers.nil?
headers ||= response.response_headers if response.respond_to?(:response_headers) && !response.response_headers.nil?
if headers
info.limit = (headers['X-RateLimit-Limit'] || 1).to_i
info.remaining = (headers['X-RateLimit-Remaining'] || 1).to_i
info.resets_at = Time.at((headers['X-RateLimit-Reset'] || Time.now).to_i)
info.resets_in = [(info.resets_at - Time.now).to_i, 0].max
end

Expand Down
15 changes: 8 additions & 7 deletions spec/octokit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1167,12 +1167,13 @@
stub_get('/user').to_return \
status: 403,
headers: {
'content_type' => 'application/json'
'content_type' => 'application/json',
'X-RateLimit-Limit' => 60,
'X-RateLimit-Remaining' => 42,
'X-RateLimit-Reset' => (Time.now + 60).to_i
},
body: { message: 'rate limit exceeded' }.to_json
begin
rate_limit_headers = { 'X-RateLimit-Limit' => 60, 'X-RateLimit-Remaining' => 42, 'X-RateLimit-Reset' => (Time.now + 60).to_i }
expect_any_instance_of(Faraday::Env).to receive(:headers).at_least(:once).and_return(rate_limit_headers)
Octokit.get('/user')
rescue Octokit::TooManyRequests => e
expect(e.context).to be_an_instance_of(Octokit::RateLimit)
Expand All @@ -1189,7 +1190,6 @@
},
body: { message: 'You have exceeded a secondary rate limit.' }.to_json
begin
expect_any_instance_of(Faraday::Env).to receive(:headers).at_least(:once).and_return({})
Octokit.get('/user')
rescue Octokit::TooManyRequests => e
expect(e.context).to be_an_instance_of(Octokit::RateLimit)
Expand All @@ -1200,12 +1200,13 @@
stub_get('/user').to_return \
status: 403,
headers: {
'content_type' => 'application/json'
'content_type' => 'application/json',
'X-RateLimit-Limit' => 60,
'X-RateLimit-Remaining' => 42,
'X-RateLimit-Reset' => (Time.now + 60).to_i
},
body: { message: 'You have exceeded a secondary rate limit.' }.to_json
begin
rate_limit_headers = { 'X-RateLimit-Limit' => 60, 'X-RateLimit-Remaining' => 42, 'X-RateLimit-Reset' => (Time.now + 60).to_i }
expect_any_instance_of(Faraday::Env).to receive(:headers).at_least(:once).and_return(rate_limit_headers)
Octokit.get('/user')
rescue Octokit::TooManyRequests => e
expect(e.context).to be_an_instance_of(Octokit::RateLimit)
Expand Down
6 changes: 3 additions & 3 deletions spec/octokit/rate_limit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let(:response) { double('response') }

it 'parses rate limit info from response headers' do
expect(response).to receive(:headers)
expect(response).to receive(:response_headers)
.at_least(:once)
.and_return(response_headers)
info = described_class.from_response(response)
Expand All @@ -27,7 +27,7 @@
end

it 'returns a positive rate limit for Enterprise' do
expect(response).to receive(:headers)
expect(response).to receive(:response_headers)
.at_least(:once)
.and_return({})
info = described_class.from_response(response)
Expand All @@ -46,7 +46,7 @@
end

it 'handles resets_in time in past' do
expect(response).to receive(:headers)
expect(response).to receive(:response_headers)
.at_least(:once)
.and_return(
response_headers.merge('X-RateLimit-Reset' => (Time.now - 60).to_i)
Expand Down