From e0fb977257f0ee0e3de8e6eaf22afc4debe11ad1 Mon Sep 17 00:00:00 2001 From: Doug Edey Date: Tue, 24 Jan 2023 15:43:12 -0500 Subject: [PATCH 1/2] Allow using the response_headers or headers when getting the Rate Limit information from the headers --- lib/octokit/rate_limit.rb | 10 ++++++---- spec/octokit/client_spec.rb | 15 ++++++++------- spec/octokit/rate_limit_spec.rb | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/octokit/rate_limit.rb b/lib/octokit/rate_limit.rb index b7e030219..fd6020d3e 100644 --- a/lib/octokit/rate_limit.rb +++ b/lib/octokit/rate_limit.rb @@ -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 diff --git a/spec/octokit/client_spec.rb b/spec/octokit/client_spec.rb index 2e49e7e12..8f473d2d5 100644 --- a/spec/octokit/client_spec.rb +++ b/spec/octokit/client_spec.rb @@ -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) @@ -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) @@ -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) diff --git a/spec/octokit/rate_limit_spec.rb b/spec/octokit/rate_limit_spec.rb index 5543c15d3..1968abd28 100644 --- a/spec/octokit/rate_limit_spec.rb +++ b/spec/octokit/rate_limit_spec.rb @@ -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) @@ -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) @@ -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) From 303e4f858e8c32e016fbc47bb636dada7ad2a8f9 Mon Sep 17 00:00:00 2001 From: Doug Edey Date: Fri, 3 Feb 2023 18:48:35 -0500 Subject: [PATCH 2/2] Update spec/octokit/client_spec.rb --- spec/octokit/client_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/octokit/client_spec.rb b/spec/octokit/client_spec.rb index 8f473d2d5..0fe8a323d 100644 --- a/spec/octokit/client_spec.rb +++ b/spec/octokit/client_spec.rb @@ -1203,7 +1203,7 @@ 'content_type' => 'application/json', 'X-RateLimit-Limit' => 60, 'X-RateLimit-Remaining' => 42, - 'X-RateLimit-Reset' => (Time.now + 60).to_i + 'X-RateLimit-Reset' => (Time.now + 60).to_i }, body: { message: 'You have exceeded a secondary rate limit.' }.to_json begin