lib/redd/api_client.rb in redd-0.8.4 vs lib/redd/api_client.rb in redd-0.8.5
- old
+ new
@@ -66,32 +66,42 @@
def request(verb, path, raw: false, params: {}, **options)
# Make sure @access is populated by a valid access
ensure_access_is_valid
# Setup base API params and make request
api_params = { api_type: 'json', raw_json: 1 }.merge(params)
- response = @rate_limiter.after_limit { super(verb, path, params: api_params, **options) }
- # Check for errors in the returned response
- response_error = @error_handler.check_error(response, raw: raw)
- raise response_error unless response_error.nil?
- # All done, return the response
- @failures = 0
- response
- rescue Redd::ServerError, HTTP::TimeoutError => e
- # FIXME: maybe only retry GET requests, for obvious reasons?
- @failures += 1
- raise e if @failures > @max_retries
- warn "Redd got a #{e.class.name} error (#{e.message}), retrying..."
- retry
+
+ # This loop is retried @max_retries number of times until it succeeds
+ handle_retryable_errors do
+ response = @rate_limiter.after_limit { super(verb, path, params: api_params, **options) }
+ # Raise errors if encountered at the API level.
+ response_error = @error_handler.check_error(response, raw: raw)
+ raise response_error unless response_error.nil?
+ # All done, return the response
+ response
+ end
end
private
# Makes sure a valid access is present, raising an error if nil
def ensure_access_is_valid
# If access is nil, panic
raise 'client access is nil, try calling #authenticate' if @access.nil?
# Refresh access if auto_refresh is enabled
refresh if @access.expired? && @auto_refresh
+ end
+
+ def handle_retryable_errors
+ response = yield
+ rescue Redd::ServerError, HTTP::TimeoutError => e
+ # FIXME: maybe only retry GET requests, for obvious reasons?
+ @failures += 1
+ raise e if @failures > @max_retries
+ warn "Redd got a #{e.class.name} error (#{e.message}), retrying..."
+ retry
+ else
+ @failures = 0
+ response
end
def connection
super.auth("Bearer #{@access.access_token}")
end