class Idcf::Cli::Lib::Api

api execute class

Attributes

client[RW]
raw[RW]

Public Class Methods

command_param_str(link) click to toggle source

command str

@param link @return String

# File lib/idcf/cli/lib/api.rb, line 15
def command_param_str(link)
  list = []
  link.url_param_names.each do |name|
    list << "<#{name}>"
  end

  if !link.query_param_names.size.zero? || !link.properties.size.zero?
    list << (param_required?(link) ? '<params>' : '[params]')
  end

  list.join(' ')
end
new(links: [], client: nil) click to toggle source

initialize

@param links [Array] in Idcf::JsonHyperSchema::Expands::LinkinfoBase @param client [Faraday]

# File lib/idcf/cli/lib/api.rb, line 42
def initialize(links: [], client: nil)
  @links  = links
  @client = client
end
param_required?(link) click to toggle source
# File lib/idcf/cli/lib/api.rb, line 28
def param_required?(link)
  param_names = link.properties.keys
  get_names   = link.query_param_names
  link.required.each do |name|
    return true if param_names.include?(name) || get_names.include?(name)
  end
  false
end

Public Instance Methods

do(command, *args) click to toggle source

do

@param command [String] @param args [Array] @return Hash @raise

# File lib/idcf/cli/lib/api.rb, line 69
def do(command, *args)
  args ||= []
  link = find_link(command)
  cli_error("Not found #{command} Api") if link.nil?

  between_param(link, args)
  @raw = do_api(link, args)
  result_log(@raw)

  return @raw.body if @raw.success?

  raise Idcf::Cli::Error::ApiError.new(@raw), ''
end
result_api(command, args, api_result, resource_id = nil) click to toggle source

result_api

@param command [String] @param args [Array] @param api_result [Hash] @param resource_id

# File lib/idcf/cli/lib/api.rb, line 89
def result_api(command, args, api_result, resource_id = nil)
  link = find_link(command)
  cli_error("Not found #{command} Api") if link.nil?
  r_api_title = link.result_api_title
  return nil if r_api_title.empty?
  r_api_params = link.result_api_params(args, api_result, resource_id)
  self.do(r_api_title, *r_api_params)
end

Protected Instance Methods

between_max(min, link) click to toggle source
# File lib/idcf/cli/lib/api.rb, line 122
def between_max(min, link)
  offset     = 0
  required_f = self.class.param_required?(link)
  offset = 1 if !required_f && (link.properties.present? || link.query_param_names.present?)

  min + offset
end
between_param(link, args) click to toggle source

between link param

@param link [Idcf::JsonHyperSchema::Expands::LinkInfoBase] @param args @raise

# File lib/idcf/cli/lib/api.rb, line 112
def between_param(link, args)
  offset = (self.class.param_required?(link) ? 1 : 0)
  min    = link.url_param_names.size + offset
  max    = between_max(min, link)

  msg = format('Argument: %<arg>s', arg: self.class.command_param_str(link))
  msg = "#{msg}\n#{Idcf::Cli::Lib::Document.make_document_desc(link)}"
  cli_error msg unless args.size.between?(min, max)
end
cli_error(msg) click to toggle source
# File lib/idcf/cli/lib/api.rb, line 161
def cli_error(msg)
  raise Idcf::Cli::Error::CliError, msg
end
client_send(method, uri, request_params) click to toggle source
# File lib/idcf/cli/lib/api.rb, line 145
def client_send(method, uri, request_params)
  case method.to_s
  when 'get', 'delete'
    @client.__send__(method, uri, nil) do |req|
      if request_params.present?
        req.headers[:content_type]    = 'application/json'
        body                          = JSON.generate(request_params)
        req.headers['Content-Length'] = body.size.to_s
        req.body                      = body
      end
    end
  else
    @client.__send__(method, uri, request_params)
  end
end
do_api(link, args) click to toggle source
# File lib/idcf/cli/lib/api.rb, line 130
def do_api(link, args)
  method         = link.method.downcase
  uri            = link.make_uri_for_cli(args)
  request_params = link.make_params_for_cli(args)

  log = Idcf::Cli::Lib::Util::CliLogger
  log.info("request_headers: #{@client.headers}")
  log.info("arguments: #{args.join(' ')}")
  log.info("uri: #{uri}")
  log.info("method: #{method}")
  log.info("request_params: #{request_params}")

  client_send(method, uri, request_params)
end
result_log(result) click to toggle source
# File lib/idcf/cli/lib/api.rb, line 100
def result_log(result)
  log = Idcf::Cli::Lib::Util::CliLogger
  log.info("result_headers: #{result.headers}")
  log.info("result_status: #{result.status}")
  log.info("result_body: #{result.body}")
end