class Daino::Clients::Base

Attributes

api_endpoint[R]
api_key[R]

Public Class Methods

new(api_endpoint:, api_key:) click to toggle source
# File lib/daino/clients/base.rb, line 12
def initialize(api_endpoint:, api_key:)
  @api_endpoint = URI(api_endpoint)
  @api_key = api_key
end

Private Instance Methods

base_url() click to toggle source
# File lib/daino/clients/base.rb, line 19
def base_url
  "#{api_endpoint.scheme}://#{api_endpoint.hostname}:#{api_endpoint.port}"
end
delete(path, params = {}, &block) click to toggle source
# File lib/daino/clients/base.rb, line 95
def delete(path, params = {}, &block)
  url = url_for(path)
  url.query = URI.encode_www_form(params) unless params.empty?

  delete = Net::HTTP::Delete.new(url)
  delete.add_field "Authorization", "Bearer #{api_key}"
  request(delete, &block)
end
get(path, params = {}, &block) click to toggle source
# File lib/daino/clients/base.rb, line 73
def get(path, params = {}, &block)
  url = url_for(path)
  url.query = URI.encode_www_form(params) unless params.empty?

  get = Net::HTTP::Get.new(url)
  get.add_field "Authorization", "Bearer #{api_key}"
  request(get, &block)
end
http_options() click to toggle source
# File lib/daino/clients/base.rb, line 43
def http_options
  if proxy = ENV["HTTP_PROXY"] || ENV["http_proxy"]
    uri = URI(proxy)
    {
      proxy_address: uri.hostname,
      proxy_port: uri.port,
      proxy_from_env: false,
    }
  else
    {}
  end
end
https_options() click to toggle source
# File lib/daino/clients/base.rb, line 27
def https_options
  return nil if api_endpoint.scheme != "https"

  if proxy = ENV["HTTPS_PROXY"] || ENV["https_proxy"]
    uri = URI(proxy)
    {
      proxy_address: uri.hostname,
      proxy_port: uri.port,
      proxy_from_env: false,
      use_ssl: true,
    }
  else
    { use_ssl: true }
  end
end
parse_body(body) click to toggle source
# File lib/daino/clients/base.rb, line 56
def parse_body(body)
  JSON.parse body.to_s
rescue JSON::ParserError => _e
  body.to_s
end
post(path, data, params = {}, &block) click to toggle source
# File lib/daino/clients/base.rb, line 82
def post(path, data, params = {}, &block)
  url = url_for(path)
  url.query = URI.encode_www_form(params) unless params.empty?

  post = Net::HTTP::Post.new(url)
  post.body = data.is_a?(Hash) ? data.to_json : data.to_s

  post.add_field "Content-Type", "application/json"
  post.add_field "Authorization", "Bearer #{api_key}"

  request(post, &block)
end
request(req) { |json| ... } click to toggle source
# File lib/daino/clients/base.rb, line 62
def request(req)
  Net::HTTP.start(api_endpoint.hostname, api_endpoint.port, https_options || http_options) do |http|
    response = http.request(req)
    json = parse_body(response.body)

    raise(Error, "Unsupported response code returned: #{response.code} (#{json&.dig('message')})") unless response.code.start_with? "20"

    yield json
  end
end
url_for(path) click to toggle source
# File lib/daino/clients/base.rb, line 23
def url_for(path)
  URI(base_url + path)
end