Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
namara / lib / namara.rb
Size: Mime:
require 'rest-client'

require_relative 'authorization'
require_relative 'catalog'
require_relative 'category'
require_relative 'exports'
require_relative 'grants'
require_relative 'organization'
require_relative 'query'
require_relative 'references'
require_relative 'sources'
require_relative 'warehouses'
require_relative 'uploads'
require_relative 'authenticators/cookie_authenticator'
require_relative 'authenticators/token_authenticator'
require_relative 'authenticators/api_key_authenticator'
require_relative 'version'

class Namara
  include NamaraVersion

  class UserNotFound < StandardError; end
  class RequestError < StandardError; end
  class ResourceError < StandardError; end

  # Expose these urls so they can be overwritten.
  attr_accessor :api_url, :account_url

  # Allow the caller to not pass any parameters here. If they do that,
  # it's their responsibility to assign the api_url and account_url.
  def initialize(domain: '', protocol: 'https', account_domain: domain)
    @api_url = "#{protocol}://api.#{domain}/twirp"
    @account_url = "#{protocol}://account.#{account_domain}"
  end

  # This auth strategy forwards the user's cookies when making requests.
  def with_cookie_auth!(cookies)
    @authenticator = CookieAuthenticator.new(account_url, cookies)
  end

  def with_token_auth!(token)
    @authenticator = TokenAuthenticator.new(account_url, token)
  end

  def with_api_key!(api_key)
    @authenticator = ApiKeyAuthenticator.new(account_url, api_key)
  end

  # Returns a token - either the one being used (if using
  # token auth) or a fresh one (if using cookie auth).
  def token
    @authenticator.token
  end

  def user_id
    @authenticator.user_id
  end

  def profile
    @authenticator.profile
  end

  def rpc_headers
    { headers: @authenticator.headers }
  end

  private

  def rpc_request(rpc_client)
    response = yield(rpc_client)

    if response.error
      error_message = "code: #{response.error.code}, message: #{response.error.msg}, meta: #{response.error.meta}"
      raise ResourceError.new(error_message)
    end

    response
  end
end