Class: InfluxDB2::InvokableScriptsApi

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

Overview

Use API invokable scripts to create custom InfluxDB API endpoints that query, process, and shape data.

API invokable scripts let you assign scripts to API endpoints and then execute them as standard REST operations in InfluxDB Cloud.

Constant Summary

Constants inherited from DefaultApi

DefaultApi::DEFAULT_REDIRECT_COUNT, DefaultApi::DEFAULT_TIMEOUT, DefaultApi::HEADER_CONTENT_TYPE

Instance Method Summary collapse

Methods inherited from DefaultApi

create_logger, #log

Constructor Details

#initialize(options:) ⇒ InvokableScriptsApi

Returns a new instance of InvokableScriptsApi.

Parameters:

  • options (Hash)

    The options to be used by the client.



30
31
32
# File 'lib/influxdb2/client/invokable_scripts_api.rb', line 30

def initialize(options:)
  super(options: options)
end

Instance Method Details

#create_script(script_create_request) ⇒ Script

Create a script.

Parameters:

Returns:

  • (Script)

    The created script.



39
40
41
42
43
44
45
# File 'lib/influxdb2/client/invokable_scripts_api.rb', line 39

def create_script(script_create_request)
  uri = _parse_uri('/api/v2/scripts')

  response = _request_json(script_create_request.to_body.to_json, uri, headers: { 'Accept' => 'application/json' })

  _to_object(response, Script)
end

#delete_script(script_id) ⇒ Object

Delete a script.

Parameters:

  • script_id (String)

    The ID of the script to delete. (required)



64
65
66
67
68
69
# File 'lib/influxdb2/client/invokable_scripts_api.rb', line 64

def delete_script(script_id)
  uri = _parse_uri_script(script_id)

  _request_json('', uri, headers: { 'Accept' => 'application/json' },
                         method: Net::HTTP::Delete)
end

#find_scripts(opts = {}) ⇒ Script

List scripts.

Parameters:

  • opts (Hash) (defaults to: {})

    the optional parameters

Options Hash (opts):

  • :limit (Integer)

    The number of scripts to return.

  • :offset (Integer)

    The offset for pagination.

Returns:



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/influxdb2/client/invokable_scripts_api.rb', line 78

def find_scripts(opts = {})
  limit = !opts[:limit].nil? ? opts[:limit] : []
  offset = !opts[:offset].nil? ? opts[:offset] : []
  uri = _parse_uri('/api/v2/scripts')
  uri.query = URI.encode_www_form(limit: limit, offset: offset)

  response = _request_json('', uri, headers: { 'Accept' => 'application/json' },
                                    method: Net::HTTP::Get)

  _to_object(response, Scripts).scripts
end

#invoke_script(script_id, params: nil) ⇒ Array

Invoke synchronously a script and return result as a [FluxTable].

Parameters:

  • script_id (String)

    The ID of the script to invoke. (required)

  • params (Enumerable) (defaults to: nil)

    represent key/value pairs parameters to be injected into script

Returns:

  • (Array)

    list of FluxTables which are matched the query



106
107
108
109
110
111
112
113
# File 'lib/influxdb2/client/invokable_scripts_api.rb', line 106

def invoke_script(script_id, params: nil)
  response = invoke_script_raw(script_id, params: params)
  parser = InfluxDB2::FluxCsvParser.new(response, stream: false,
                                                  response_mode: InfluxDB2::FluxResponseMode::ONLY_NAMES)

  parser.parse
  parser.tables
end

#invoke_script_raw(script_id, params: nil) ⇒ String

Invoke synchronously a script and return result as a [String].

Parameters:

  • script_id (String)

    The ID of the script to invoke. (required)

  • params (Enumerable) (defaults to: nil)

    represent key/value pairs parameters to be injected into script

Returns:

  • (String)

    result of query



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

def invoke_script_raw(script_id, params: nil)
  _invoke_script(script_id, params: params).read_body
end

#invoke_script_stream(script_id, params: nil) ⇒ Object

Invoke synchronously a script and return result as a stream of FluxRecord.

Parameters:

  • script_id (String)

    The ID of the script to invoke. (required)

  • params (Enumerable) (defaults to: nil)

    represent key/value pairs parameters to be injected into script

Returns:

  • stream of Flux Records



121
122
123
124
125
# File 'lib/influxdb2/client/invokable_scripts_api.rb', line 121

def invoke_script_stream(script_id, params: nil)
  response = _invoke_script(script_id, params: params)

  InfluxDB2::FluxCsvParser.new(response, stream: true, response_mode: InfluxDB2::FluxResponseMode::ONLY_NAMES)
end

#update_script(script_id, update_request) ⇒ Script

Update a script.

Parameters:

  • script_id (String)

    The ID of the script to update. (required)

  • update_request (ScriptUpdateRequest)

    Script updates to apply (required)

Returns:

  • (Script)

    The updated script.



53
54
55
56
57
58
59
# File 'lib/influxdb2/client/invokable_scripts_api.rb', line 53

def update_script(script_id, update_request)
  uri = _parse_uri_script(script_id)

  response = _request_json(update_request.to_body.to_json, uri, headers: { 'Accept' => 'application/json' },
                                                                method: Net::HTTP::Patch)
  _to_object(response, Script)
end