Class: ChConnect::HttpTransport Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ch_connect/http_transport.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

HTTP transport layer for ClickHouse communication.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HttpTransport

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new HTTP transport.

Parameters:

  • config (Config)

    configuration instance



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ch_connect/http_transport.rb', line 13

def initialize(config)
  @config = config
  @base_url = "#{config.scheme}://#{config.host}:#{config.port}"
  @http_client = HTTPX.plugin(:persistent, close_on_fork: true)
    .plugin(:retries, max_retries: config.max_retries, retry_change_requests: true)
    .with(
      timeout: {
        connect_timeout: config.connection_timeout,
        read_timeout: config.read_timeout,
        write_timeout: config.write_timeout,
        keep_alive_timeout: config.keep_alive_timeout
      },
      pool_options: {
        max_connections_per_origin: config.pool_size,
        pool_timeout: config.pool_timeout
      }
    )

  @default_headers = {
    "Accept-Encoding" => "gzip",
    "X-ClickHouse-User" => config.username,
    "X-ClickHouse-Key" => config.password,
    "X-ClickHouse-Format" => "Native"
  }
end

Instance Method Details

#execute(sql, options = {}) ⇒ TransportResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Executes a SQL query via HTTP.

Parameters:

  • sql (String)

    SQL query to execute

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

    query options

Options Hash (options):

  • :params (Hash)

    query parameters

Returns:

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ch_connect/http_transport.rb', line 46

def execute(sql, options = {})
  query_params = {database: @config.database}.merge(options[:params] || {})
  response = @http_client.post(@base_url, params: query_params, body: sql, headers: @default_headers)

  raise QueryError, response.error.message if response.error

  summary = JSON.parse(response.headers["x-clickhouse-summary"], symbolize_names: true)

  raise QueryError, response.body.to_s unless response.status == 200

  TransportResult.new(body: response.body, summary: summary)
end