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

Catch rate limit errors and raise them so that callers can response appropriately. #2

Merged
merged 2 commits into from
May 23, 2024
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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ghx (0.2.1)
ghx (0.3.0)
faraday (~> 2.9.0)
faraday-retry (~> 2.2.1)
octokit
Expand Down
1 change: 1 addition & 0 deletions lib/ghx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def symbolize_keys!
end

require_relative "version"
require_relative "ghx/errors"
require_relative "ghx/graphql_client"
require_relative "ghx/rest_client"
require_relative "ghx/dependabot"
Expand Down
7 changes: 7 additions & 0 deletions lib/ghx/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module GHX
class GHXError < StandardError; end

class RateLimitExceededError < GHXError; end

class OtherApiError < GHXError; end
end
4 changes: 0 additions & 4 deletions lib/ghx/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def self.search(owner:, repo:, query:)
data.fetch("items").to_a.map do |issue_data|
new(owner: owner, repo: repo, **issue_data)
end
rescue => e
GHX.logger.error "Error searching for issues with query: #{e.message}"
GHX.logger.error "Received data: #{data}"
[]
end

# Find an issue by its number
Expand Down
48 changes: 25 additions & 23 deletions lib/ghx/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@ def initialize(api_key)
def get(path)
uri = URI.parse("https://api.github.com/#{path}")
request = Net::HTTP::Get.new(uri)
request["Accept"] = "application/vnd.github+json"
request["Authorization"] = "Bearer #{@api_key}"
request["X-Github-Api-Version"] = "2022-11-28"

req_options = {
use_ssl: uri.scheme == "https"
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
response = _http_request(uri: uri, request: request)

JSON.parse(response.body)
end
Expand All @@ -40,19 +31,10 @@ def get(path)
def post(path, params)
uri = URI.parse("https://api.github.com/#{path}")
request = Net::HTTP::Post.new(uri)
request["Accept"] = "application/vnd.github+json"
request["Authorization"] = "Bearer #{@api_key}"
request["X-Github-Api-Version"] = "2022-11-28"

req_options = {
use_ssl: uri.scheme == "https"
}

request.body = params.to_json

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
response = _http_request(uri: uri, request: request)

JSON.parse(response.body)
end
Expand All @@ -64,6 +46,17 @@ def post(path, params)
def patch(path, params)
uri = URI.parse("https://api.github.com/#{path}")
request = Net::HTTP::Patch.new(uri)

request.body = params.to_json

response = _http_request(uri: uri, request: request)

JSON.parse(response.body)
end

private

def _http_request(uri:, request:)
request["Accept"] = "application/vnd.github+json"
request["Authorization"] = "Bearer #{@api_key}"
request["X-Github-Api-Version"] = "2022-11-28"
Expand All @@ -72,13 +65,22 @@ def patch(path, params)
use_ssl: uri.scheme == "https"
}

request.body = params.to_json

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end

JSON.parse(response.body)
if response.code.to_i < 400
response
elsif [403, 429].include?(response.code.to_i)
if response["X-RateLimit-Remaining"].to_i == 0
reset_time = Time.at(response["X-RateLimit-Reset"].to_i)
raise GHX::RateLimitExceededError, "GitHub API rate limit exceeded. Try again after #{reset_time}"
else
raise GHX::RateLimitExceededError, "GitHub API rate limit exceeded. Try again later."
end
else
raise GHX::OtherApiError, "GitHub API returned an error: #{response.code} #{response.body}"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GHX
VERSION = "0.2.1"
VERSION = "0.3.0"
end