class InfluxDB2::DefaultApi

default api

Constants

DEFAULT_REDIRECT_COUNT
DEFAULT_TIMEOUT
HEADER_CONTENT_TYPE

Public Class Methods

create_logger() click to toggle source
# File lib/influxdb2/client/default_api.rb, line 56
def self.create_logger
  Logger.new(STDOUT)
end
new(options:) click to toggle source

@param [Hash] options The options to be used by the client.

# File lib/influxdb2/client/default_api.rb, line 32
def initialize(options:)
  @options = options
  @max_redirect_count = @options[:max_redirect_count] || DEFAULT_REDIRECT_COUNT
end

Public Instance Methods

log(level, message) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 37
def log(level, message)
  return unless @options[:logger]

  log_level = case level
              when :debug then
                Logger::DEBUG
              when :warn then
                Logger::WARN
              when :error then
                Logger::ERROR
              when :fatal then
                Logger::FATAL
              else
                Logger::INFO
              end

  @options[:logger].add(log_level) { message }
end

Private Instance Methods

_check(key, value) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 140
def _check(key, value)
  raise ArgumentError, "The '#{key}' should be defined as argument or default option: #{@options}" if value.nil?
end
_check_arg_type(name, value, klass) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 136
def _check_arg_type(name, value, klass)
  raise TypeError, "expected a #{klass.name} for #{name}; got #{value.class.name}" unless value.is_a?(klass)
end
_get(uri, limit: @max_redirect_count, add_authorization: true, headers: {}) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 81
def _get(uri, limit: @max_redirect_count, add_authorization: true, headers: {})
  _request(nil, uri, limit: limit, add_authorization: add_authorization,
                     headers: headers.merge('Accept' => 'application/json'), request: Net::HTTP::Get)
end
_parse_uri(api_path) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 62
def _parse_uri(api_path)
  URI.parse(File.join(@options[:url], api_path))
end
_post(payload, uri, limit: @max_redirect_count, add_authorization: true, headers: {}) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 76
def _post(payload, uri, limit: @max_redirect_count, add_authorization: true, headers: {})
  _request(payload, uri, limit: limit, add_authorization: add_authorization,
                         headers: headers, request: Net::HTTP::Post)
end
_post_json(payload, uri, headers: {}) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 66
def _post_json(payload, uri, headers: {})
  _check_arg_type(:headers, headers, Hash)
  _post(payload, uri, headers: headers.merge(HEADER_CONTENT_TYPE => 'application/json'))
end
_post_text(payload, uri, headers: {}) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 71
def _post_text(payload, uri, headers: {})
  _check_arg_type(:headers, headers, Hash)
  _post(payload, uri, headers: headers.merge(HEADER_CONTENT_TYPE => 'text/plain'))
end
_prepare_http_client(uri) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 126
def _prepare_http_client(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = @options[:open_timeout] || DEFAULT_TIMEOUT
  http.write_timeout = @options[:write_timeout] || DEFAULT_TIMEOUT if Net::HTTP.method_defined? :write_timeout
  http.read_timeout = @options[:read_timeout] || DEFAULT_TIMEOUT
  http.use_ssl = @options[:use_ssl].nil? ? true : @options[:use_ssl]
  http.verify_mode = @options[:verify_mode] if @options[:verify_mode]
  http
end
_request(payload, uri, limit: @max_redirect_count, add_authorization: true, headers: {}, request: Net::HTTP::Post) click to toggle source
# File lib/influxdb2/client/default_api.rb, line 86
def _request(payload, uri, limit: @max_redirect_count, add_authorization: true, headers: {},
             request: Net::HTTP::Post)
  raise InfluxError.from_message("Too many HTTP redirects. Exceeded limit: #{@max_redirect_count}") if limit.zero?

  http = _prepare_http_client(uri)

  request = request.new(uri.request_uri)
  request['Authorization'] = "Token #{@options[:token]}" if add_authorization
  request['User-Agent'] = "influxdb-client-ruby/#{InfluxDB2::VERSION}"
  headers.each { |k, v| request[k] = v }

  request.body = payload

  begin
    response = http.request(request)
    case response
    when Net::HTTPSuccess then
      response
    when Net::HTTPRedirection then
      location = response['location']
      redirect_forward_authorization = @options[:redirect_forward_authorization] || false

      uri_redirect = URI.parse(location)
      uri_redirect.query = uri.query
      uri_redirect.path = File.join(uri_redirect.path, uri.path)

      redirect_forward_authorization ||= (uri_redirect.host == uri.host) && (uri_redirect.port == uri.port)

      _post(payload, uri_redirect, limit: limit - 1, add_authorization: redirect_forward_authorization,
                                   headers: headers)
    else
      raise InfluxError.from_response(response)
    end
  rescue *InfluxError::HTTP_ERRORS => error
    raise InfluxError.from_error(error)
  ensure
    http.finish if http.started?
  end
end