class XodClient::Connection

Attributes

config[R]
estab[R]
relying_party[R]
response_logger[R]
secret[R]
token[R]
token_expires_at[R]
token_refreshed_proc[R]

Public Class Methods

new(relying_party, estab, secret, token: nil, token_expires_at: nil, response_logger: nil, token_refreshed_proc: nil) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/xod_client/connection.rb, line 19
def initialize(relying_party, estab, secret,
               token: nil, token_expires_at: nil, response_logger: nil, token_refreshed_proc: nil)
  @relying_party = relying_party
  @estab = estab
  @secret = secret
  @token = token
  @token_expires_at = token_expires_at
  @response_logger = response_logger
  @token_refreshed_proc = token_refreshed_proc
  @config = Config.new
end

Public Instance Methods

endpoint(endpoint_name = nil, **params) click to toggle source
# File lib/xod_client/connection.rb, line 31
def endpoint(endpoint_name = nil, **params)
  endpoint_name ||= params.delete(:endpoint_name) || raise(ArgumentError, 'Endpoint name should be provided')
  ensure_token

  EndpointCall.new(self, endpoint_name, params)
end
ensure_token() click to toggle source
# File lib/xod_client/connection.rb, line 38
def ensure_token
  refresh_token unless valid_token?
end
faraday(**options) click to toggle source
# File lib/xod_client/connection.rb, line 51
def faraday(**options)
  Faraday.new(options) do |conn|
    conn.request :retry, config.retry_options
    conn.response :logger, response_logger, bodies: true if response_logger
    conn.adapter :net_http
    conn.headers['Content-Type'] = 'application/json'
    conn.headers['User-Agent'] = config.user_agent
  end
end
refresh_token() click to toggle source
# File lib/xod_client/connection.rb, line 42
def refresh_token
  @token, @token_expires_at = TokenRefresher.new(self).perform
  token_refreshed_proc&.call(token: @token, token_expires_at: @token_expires_at)
end
valid_token?() click to toggle source
# File lib/xod_client/connection.rb, line 47
def valid_token?
  token && token_expires_at&.future?
end