Skip to content

Commit

Permalink
Merge pull request #79 from ryantaylor/rate-limit-error
Browse files Browse the repository at this point in the history
Handle rate limit errors from API
  • Loading branch information
ryantaylor authored Mar 27, 2024
2 parents 113f90d + 87249a1 commit d7d95ef
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ AllCops:
TargetRubyVersion: 3.1
NewCops: enable

Layout/AccessModifierIndentation:
EnforcedStyle: outdent

Metrics/BlockLength:
Enabled: false

Expand Down
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:
relic_link (1.0.0)
relic_link (1.1.0)
faraday (~> 2.7)
faraday-mashify (~> 0.1)
hashie (~> 5.0)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# RelicLink

[![Gem Version](https://badge.fury.io/rb/relic_link.svg)](https://badge.fury.io/rb/relic_link) [![Documentation](https://img.shields.io/badge/View-Documentation-blue.svg)](https://rubydoc.info/github/ryantaylor/relic_link/v1.0.0)
[![Gem Version](https://badge.fury.io/rb/relic_link.svg)](https://badge.fury.io/rb/relic_link) [![Documentation](https://img.shields.io/badge/View-Documentation-blue.svg)](https://rubydoc.info/github/ryantaylor/relic_link/v1.1.0)

A client wrapper for Relic APIs. Currently supports the Company of Heroes 3 leaderboard, stats, and recent matches API.

Expand Down Expand Up @@ -29,7 +29,7 @@ client.recent_match_history(profile_ids: [8230])
client.recent_match_history_by_profile_id(8230)
client.personal_stats(profile_ids: [8230])
```
Consult the [documentation](https://rubydoc.info/github/ryantaylor/relic_link/v1.0.0) for all endpoints that have been discovered and are currently queryable. Note that Relic does not publish official documentation for their endpoints, so the functionality here is based on best estimates and is subject to change without warning. Please open an issue if you are aware of endpoints that are not exposed in this library!
Consult the [documentation](https://rubydoc.info/github/ryantaylor/relic_link/v1.1.0) for all endpoints that have been discovered and are currently queryable. Note that Relic does not publish official documentation for their endpoints, so the functionality here is based on best estimates and is subject to change without warning. Please open an issue if you are aware of endpoints that are not exposed in this library!

## Contributing

Expand Down
4 changes: 3 additions & 1 deletion lib/relic_link/coh3/api/endpoints/leaderboards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Leaderboards
# List available leaderboards and associated metadata.
#
# @raise [RelicLink::Errors::ServerError] if Relic's API is down.
# @raise [RelicLink::Errors::RateLimitError] if you're rate-limited.
def available_leaderboards
get('getAvailableLeaderboards')
end
Expand All @@ -34,6 +35,7 @@ def available_leaderboards
#
# @raise [ArgumentError] if the required parameters aren't provided.
# @raise [RelicLink::Errors::ServerError] if Relic's API is down.
# @raise [RelicLink::Errors::RateLimitError] if you're rate-limited.
# @raise [Errors::NotFound] if +leaderboard_id+ does not correspond to a leaderboard.
# @raise [RelicLink::Errors::BadRequestError] if +start+, +count+, or +sort_by+ are outside the
# limits enumerated above.
Expand All @@ -43,7 +45,7 @@ def leaderboard(options = {})
get('getLeaderboard2', format_sort_key(options))
end

private
private

def format_sort_key(options)
options.transform_keys do |key|
Expand Down
3 changes: 3 additions & 0 deletions lib/relic_link/coh3/api/endpoints/matches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Matches
#
# @raise [ArgumentError] if none of the required parameters are provided.
# @raise [RelicLink::Errors::ServerError] if Relic's API is down.
# @raise [RelicLink::Errors::RateLimitError] if you're rate-limited.
# @raise [Errors::UnknownProfileIds] if no values in +profile_ids+ match known profile IDs.
# @raise [Errors::UnknownAliases] if no values in +aliases+ match known aliases.
# @raise [Errors::UnregisteredProfileName] if no values in +profile_names+ match known profile names.
Expand All @@ -44,6 +45,8 @@ def recent_match_history(options = {})
# @param profile_id [Integer] Internal Relic ID of player whose stats you wish to retrieve (required).
#
# @raise [ArgumentError] if no +profile_id+ provided.
# @raise [RelicLink::Errors::ServerError] if Relic's API is down.
# @raise [RelicLink::Errors::RateLimitError] if you're rate-limited.
# @raise [RelicLink::Errors::BadRequestError] if input violates type constraints.
def recent_match_history_by_profile_id(profile_id)
raise ArgumentError, 'Required argument :profile_id missing' if profile_id.nil?
Expand Down
1 change: 1 addition & 0 deletions lib/relic_link/coh3/api/endpoints/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Stats
#
# @raise [ArgumentError] if none of the required parameters are provided.
# @raise [RelicLink::Errors::ServerError] if Relic's API is down.
# @raise [RelicLink::Errors::RateLimitError] if you're rate-limited.
# @raise [Errors::UnknownProfileIds] if no values in +profile_ids+ match known profile IDs.
# @raise [Errors::UnknownAliases] if no values in +aliases+ match known aliases.
# @raise [Errors::UnregisteredProfileName] if no values in +profile_names+ match known profile names.
Expand Down
2 changes: 1 addition & 1 deletion lib/relic_link/coh3/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Faraday
# Faraday connection implementation for API requests. You shouldn't
# ever have to use this directly.
module Connection
private
private

def connection
@connection ||= ::Faraday.new(
Expand Down
7 changes: 7 additions & 0 deletions lib/relic_link/coh3/faraday/response/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Response
class RaiseError < ::Faraday::Middleware
def on_complete(env)
raise RelicLink::Errors::BadRequestError, env.response if env.status == 400
raise RelicLink::Errors::RateLimitError, env.response if env.status == 429

return unless env.success?

Expand All @@ -16,6 +17,12 @@ def on_complete(env)

return if body['result']['code'].zero?

raise_relic_error!(body, env)
end

private

def raise_relic_error!(body, env)
error_message = body['result']['message']
error_class = RelicLink::Coh3::Api::Errors::ERROR_CLASSES[error_message]
error_class ||= RelicLink::Coh3::Api::Errors::RelicError
Expand Down
7 changes: 7 additions & 0 deletions lib/relic_link/errors/server_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def initialize(response)
end
end

# Raised when a 429 is returned in an API response.
class RateLimitError < HttpRequestError
def initialize(response)
super('rate_limited', response)
end
end

# Raised when an API request times out.
class TimeoutError < HttpRequestError
def initialize(response)
Expand Down
2 changes: 1 addition & 1 deletion lib/relic_link/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RelicLink
VERSION = '1.0.0'
VERSION = '1.1.0'
end

0 comments on commit d7d95ef

Please sign in to comment.