Class: InfluxDB2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/influxdb2/client/client.rb

Overview

The client is the entry point to HTTP API defined in github.com/influxdata/influxdb/blob/master/http/swagger.yml.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, token, options = nil) ⇒ Client

Instantiate a new InfluxDB client.

Examples:

Instantiate a client.

InfluxDBClient::Client.new(url: 'https://localhost:8086', token: 'my-token')

Parameters:

  • options (Hash) (defaults to: nil)

    The options to be used by the client.

  • url (String)

    InfluxDB URL to connect to (ex. localhost:8086).

  • token (String)

    Access Token used for authenticating/authorizing the InfluxDB request sent by client.

Options Hash (options):

  • :bucket (String)

    the default destination bucket for writes

  • :org (String)

    the default organization bucket for writes

  • :precision (WritePrecision)

    the default precision for the unix timestamps within

  • :open_timeout (Integer)

    Number of seconds to wait for the connection to open

  • :write_timeout (Integer)

    Number of seconds to wait for one block of data to be written

  • :read_timeout (Integer)

    Number of seconds to wait for one block of data to be read

  • :max_redirect_count (Integer)

    Maximal number of followed HTTP redirects

  • :redirect_forward_authorization (bool)

    Pass Authorization header to different domain during HTTP redirect.

  • :use_ssl (bool)

    Turn on/off SSL for HTTP communication

  • :verify_mode (Integer)

    Sets the flags for the certification verification at beginning of SSL/TLS session. Could be one of `OpenSSL::SSL::VERIFY_NONE` or `OpenSSL::SSL::VERIFY_PEER`. For more info see - docs.ruby-lang.org/en/3.0.0/Net/HTTP.html#verify_mode.

  • :logger (Logger)

    Logger used for logging. Disable logging by set to false.

  • :debugging (bool)

    Enable debugging for HTTP request/response.

  • :tags (Hash)

    Default tags which will be added to each point written by api.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/influxdb2/client/client.rb', line 54

def initialize(url, token, options = nil)
  @auto_closeable = []
  @options = options ? options.dup : {}
  @options[:url] = url if url.is_a? String
  @options[:token] = token if token.is_a? String
  @options[:logger] = @options[:logger].nil? ? DefaultApi.create_logger : @options[:logger]
  @options[:debugging] = @options[:debugging].nil? ? false : @options[:debugging]
  @closed = false

  at_exit { close! }
end

Instance Attribute Details

#optionsHash (readonly)

Returns options The configuration options.

Returns:

  • (Hash)

    options The configuration options.



27
28
29
# File 'lib/influxdb2/client/client.rb', line 27

def options
  @options
end

Class Method Details

.use(*args) ⇒ Object

Instantiate a new InfluxDB client with code block. The client will be passed as an argument and will be automatically closed when the block terminates.

It takes same args as #initialize.

Examples:

Instantiate a client.

InfluxDBClient::Client.use(url: 'https://localhost:8086', token: 'my-token') do |client|
  ping = client.ping
  puts ping.version
end


76
77
78
79
80
81
# File 'lib/influxdb2/client/client.rb', line 76

def self.use(*args)
  client = Client.new(*args)
  yield client
ensure
  client.close!
end

Instance Method Details

#close!true

Close all connections into InfluxDB 2.

Returns:

  • (true)

    Always true.



131
132
133
134
135
# File 'lib/influxdb2/client/client.rb', line 131

def close!
  @closed = true
  @auto_closeable.each(&:close!)
  true
end

#create_delete_apiDeleteApi

Get the Delete API to delete time series data from InfluxDB.

Returns:



102
103
104
# File 'lib/influxdb2/client/client.rb', line 102

def create_delete_api
  DeleteApi.new(options: @options)
end

#create_invokable_scripts_apiInvokableScriptsApi

Create an InvokableScripts API instance.

Returns:



109
110
111
# File 'lib/influxdb2/client/client.rb', line 109

def create_invokable_scripts_api
  InvokableScriptsApi.new(options: @options)
end

#create_query_apiQueryApi

Get the Query client.

Returns:

  • (QueryApi)

    New instance of QueryApi.



95
96
97
# File 'lib/influxdb2/client/client.rb', line 95

def create_query_api
  QueryApi.new(options: @options)
end

#create_write_api(write_options: InfluxDB2::SYNCHRONOUS, point_settings: InfluxDB2::DEFAULT_POINT_SETTINGS) ⇒ WriteApi

Write time series data into InfluxDB thought WriteApi.

Returns:

  • (WriteApi)

    New instance of WriteApi.



86
87
88
89
90
# File 'lib/influxdb2/client/client.rb', line 86

def create_write_api(write_options: InfluxDB2::SYNCHRONOUS, point_settings: InfluxDB2::DEFAULT_POINT_SETTINGS)
  write_api = WriteApi.new(options: @options, write_options: write_options, point_settings: point_settings)
  @auto_closeable.push(write_api)
  write_api
end

#healthHealthCheck

Deprecated.

Use `ping` instead

Get the health of an instance.

Returns:



117
118
119
# File 'lib/influxdb2/client/client.rb', line 117

def health
  HealthApi.new(options: @options).health
end

#pingPing

Checks the status of InfluxDB instance and version of InfluxDB.

Returns:



124
125
126
# File 'lib/influxdb2/client/client.rb', line 124

def ping
  PingApi.new(options: @options).ping
end