Sha256: 17509e6dc84aca8a896132b3f877997d9988ca6373486b445ea68d114c9612d8

Contents?: true

Size: 1012 Bytes

Versions: 11

Compression:

Stored size: 1012 Bytes

Contents

# frozen_string_literal: true

module RDStation
  module RetryableRequest
    MAX_RETRIES = 1
    def retryable_request(authorization)
      retries = 0
      begin
        yield authorization
      rescue ::RDStation::Error::ExpiredAccessToken => e
        raise if !retry_possible?(authorization) || retries >= MAX_RETRIES

        retries += 1
        refresh_access_token(authorization)
        retry
      end
    end

    def retry_possible?(authorization)
      [
        RDStation.configuration&.client_id,
        RDStation.configuration&.client_secret,
        authorization.refresh_token
      ].all?
    end

    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
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rdstation-ruby-client-2.8.0 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.7.0 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.6.0 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.5.3 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.5.2 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.5.1 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.5.0 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.4.0 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.3.1 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.3.0 lib/rdstation/retryable_request.rb
rdstation-ruby-client-2.2.0 lib/rdstation/retryable_request.rb