class LL::WK::API::Connection::Base

Attributes

email[R]
password[R]
token[R]
token_issued[R]
url[R]

Public Class Methods

authenticate_payload(email, password) click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 8
def self.authenticate_payload(email, password)
  {
    user: {
      email: email,
      password: password
    }
  }.to_json
end
new(url:, email:, password:) click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 34
def initialize(url:, email:, password:)
  LL::WK::API.logger.debug("API Connection initialize(url: #{url}, email: #{email}, password: xxxx)")
  @url = url
  @email = email
  @password = password
end
trap_resp_code(code, klass = LL::WK::API::Connection::Error) click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 17
def self.trap_resp_code(code, klass = LL::WK::API::Connection::Error)
  case code
  when 403
    LL::WK::API.logger.debug('API Unautorized')
    raise LL::WK::API::Connection::AuthenticationError, 'Unauthorized'
  when 404
    LL::WK::API.logger.debug('API invalid endpoint')
    raise klass, 'Invalid endpoint'
  when 400..499
    LL::WK::API.logger.debug("API Client Error: #{code}")
    raise klass, 'Client error #{code}'
  when 500..599
    LL::WK::API.logger.debug("API Server Error: #{code}")
    raise klass, "Server Error #{code}"
  end
end

Public Instance Methods

auth() click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 41
def auth
  "Token token=\"#{@token}\", email=\"#{@email}\""
end
authenticate!(force = false) click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 45
def authenticate!(force = false)
  LL::WK::API.logger.debug("API authenitcate!(#{force})")
  return self unless token_expired? or force

  retries = 0
  begin
    self.class.authenticate(email, password, url) do |resp|
      self.class.trap_resp_code(resp.code, LL::WK::API::Connection::AuthenticationError)
      @token = resp.parsed_response['token']
      @token_issued = Time.now
    end
  rescue LL::WK::API::Connection::AuthenticationError => e
    retries += 1
    sleep retries
    retry if retries < 2
    raise e
  end
  self
end
from_api(endpoint, params, &block) click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 65
def from_api(endpoint, params, &block)
  # with_page modifies the context, so need to clone it otherwise you
  # get unexpected behaviour from the calling context
  p = params.clone
  LL::WK::API.logger.debug("API from_api ep: #{endpoint} params: #{params}")
  retries = 0
  return with_cursor(endpoint, p, &block) if LL::WK::API::Connection::SUPPORTS_CURSOR
  with_page(endpoint, p, &block)
rescue AuthenticationError => e
  authenticate!
  retries += 1
  retry if retries < 3
  raise e
end
page_count(resp) click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 80
def page_count(resp)
  resp['paging']&.[]('total')
end
retrieve_data(endpoint, params, &block) click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 125
def retrieve_data(endpoint, params, &block)
  retries = 0
  LL::WK::API.benchmark("http call #{endpoint}") do
    response_from_api(endpoint, params, &block)
  end
rescue LL::WK::API::Connection::Error => e
  authenticate!(true)
  retries += 1
  sleep 1
  retry if retries < 3
  raise e
end
token_expired?() click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 84
def token_expired?
  return true unless token
  true unless (Time.now - token_issued.to_i).to_i < 76400
end
with_cursor(endpoint, params) { |item| ... } click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 89
def with_cursor(endpoint, params)
  LL::WK::API.logger.debug("API call uses cursors")
  params[:cursor] ||= 0
  array = []
  while !params[:cursor].nil?
    retrieve_data(endpoint, params) do |resp|
      resp['data'].each do |item|
        array << item
        yield(item) if block_given?
      end
      params[:cursor] = resp[:cursor]
    end
  end
  array
end
with_page(endpoint, params) { |item| ... } click to toggle source
# File lib/ll/wk/api/connection/base.rb, line 105
def with_page(endpoint, params)
  LL::WK::API.logger.debug("API call uses pagination")
  params[:page] ||= 1
  pages_remain = params[:page] + 1
  array = []
  while params[:page] < pages_remain
    retrieve_data(endpoint, params) do |resp|
      resp['data'].each do |item|
        array << item
        yield(item) if block_given?
      end
      pages_remain = page_count(resp)
      LL::WK::API.logger.debug("finished with page #{params[:page]} of #{pages_remain}")
      params[:page] += 1
    end
  end
  LL::WK::API.logger.debug "found or worked on #{array.size} items"
  array
end