module RDStation::RetryableRequest

Constants

MAX_RETRIES

Public Instance Methods

refresh_access_token(authorization) click to toggle source
# File lib/rdstation/retryable_request.rb, line 27
def refresh_access_token(authorization)
  client = RDStation::Authentication.new
  response = client.update_access_token(authorization.refresh_token)
  authorization.access_token = response['access_token']
  authorization.access_token_expires_in = response['expires_in']
  RDStation.configuration&.access_token_refresh_callback&.call(authorization)
end
retry_possible?(authorization) click to toggle source
# File lib/rdstation/retryable_request.rb, line 19
def retry_possible?(authorization)
  [
    RDStation.configuration&.client_id,
    RDStation.configuration&.client_secret,
    authorization.refresh_token
  ].all?
end
retryable_request(authorization) { |authorization| ... } click to toggle source
# File lib/rdstation/retryable_request.rb, line 6
def retryable_request(authorization)
  retries = 0
  begin
    yield authorization
  rescue ::RDStation::Error::ExpiredAccessToken, ::RDStation::Error::Unauthorized => e
    raise if !retry_possible?(authorization) || retries >= MAX_RETRIES

    retries += 1
    refresh_access_token(authorization)
    retry
  end
end