class KOSapiClient::OAuth2HttpAdapter

Constants

DEFAULT_AUTH_URL
DEFAULT_TOKEN_URL

Attributes

base_url[R]

Public Class Methods

new(credentials, base_url, opts = {}) click to toggle source
# File lib/kosapi_client/oauth2_http_adapter.rb, line 11
def initialize(credentials, base_url, opts = {})
  @base_url = base_url
  @credentials = credentials
  auth_url = opts[:auth_url] || DEFAULT_AUTH_URL
  token_url = opts[:token_url] || DEFAULT_TOKEN_URL
  connection_opts = opts[:connection_opts] || {}
  MultiXml.parser = :ox # make sure to use Ox because of different namespace handling in other MultiXML parsers
  @client = OAuth2::Client.new(
    credentials[:client_id],
    credentials[:client_secret],
    auth_scheme: :basic_auth,
    site: base_url,
    authorize_url: auth_url,
    token_url: token_url,
    connection_opts: connection_opts)
end

Public Instance Methods

send_request(verb, url, options = {}) click to toggle source
# File lib/kosapi_client/oauth2_http_adapter.rb, line 28
def send_request(verb, url, options = {})
  raise 'No credentials set' if @credentials.empty?
  token.request(verb, url, options)
end

Private Instance Methods

authenticate() click to toggle source
# File lib/kosapi_client/oauth2_http_adapter.rb, line 34
def authenticate
  @token = @client.client_credentials.get_token
end
token() click to toggle source
# File lib/kosapi_client/oauth2_http_adapter.rb, line 38
def token
  authenticate if !@token || @token.expired?
  @token
end