class Klaro::Client::RequestHandler

Constants

Http

Attributes

base_url[R]
token[R]

Public Class Methods

new(url) click to toggle source
# File lib/klaro/client/request_handler.rb, line 8
def initialize(url)
  @base_url = url.gsub(/\/api\/?$/, "")
  @token = nil
  @subdomain = nil
  @workspace = nil
end

Public Instance Methods

authenticate(user, password, workspace = nil) click to toggle source
# File lib/klaro/client/request_handler.rb, line 29
def authenticate(user, password, workspace = nil)
  @workspace = workspace
  @token = get_token(
    Http
      .headers({
        'Content-Type' => 'application/json'
      })
      .post("#{base_url}/api/auth/tokens/", payload(user, password))
  )
end
authenticated?() click to toggle source
# File lib/klaro/client/request_handler.rb, line 15
def authenticated?
  not(@token.nil?)
end
get(endpoint, raw = false) click to toggle source
# File lib/klaro/client/request_handler.rb, line 40
def get(endpoint, raw = false)
  url = "#{base_url}#{endpoint}"
  info("GET `#{url}`")
  response = http.get(url, ssl_context: http_ctx)
  raise Error::NoSuchBoardFound if response.status >= 300 || response.status <200

  raw ? response.to_s : JSON.parse(response.to_s)
end
post(endpoint, body) click to toggle source
# File lib/klaro/client/request_handler.rb, line 49
def post(endpoint, body)
  url = "#{base_url}#{endpoint}"
  info("POST `#{url}`")
  http.post(url, body.merge(ssl_context: http_ctx))
end
with_project(subdomain) click to toggle source
# File lib/klaro/client/request_handler.rb, line 24
def with_project(subdomain)
  @subdomain = subdomain
  self
end
with_token(token_type, access_token) click to toggle source
# File lib/klaro/client/request_handler.rb, line 19
def with_token(token_type, access_token)
  @token = "#{token_type} #{access_token}"
  self
end

Private Instance Methods

error(msg)
Alias for: info
get_token(response) click to toggle source
# File lib/klaro/client/request_handler.rb, line 56
def get_token(response)
  raise Error::AuthenticationFailed if response.status >= 300 || response.status <200

  response = JSON.parse(response.body)
  "#{response['token_type']} #{response['access_token']}"
end
http() click to toggle source
# File lib/klaro/client/request_handler.rb, line 77
def http
  Http.headers(http_headers)
end
http_ctx() click to toggle source
# File lib/klaro/client/request_handler.rb, line 73
def http_ctx
  OpenSSL::SSL::SSLContext.new
end
http_headers() click to toggle source
# File lib/klaro/client/request_handler.rb, line 81
def http_headers
  hs = {
    "Authorization" => @token,
    "Content-Type" => "application/json"
  }
  hs['Klaro-Project-Subdomain'] = @subdomain if @subdomain
  hs['X-Klaro-ViewAs'] = @workspace if @workspace
  hs
end
info(msg) click to toggle source
# File lib/klaro/client/request_handler.rb, line 91
def info(msg)
  puts msg
end
Also aliased as: error
payload(user, password) click to toggle source
# File lib/klaro/client/request_handler.rb, line 63
def payload(user, password)
  { json: {
      grant_type: "client_credentials",
      client_id: user,
      client_secret: password
    },
    ssl_context: http_ctx
  }
end