module Trino::Client

Trino client for Ruby

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Trino client for Ruby

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Trino client for Ruby

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Trino client for Ruby

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Trino client for Ruby

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Trino client for Ruby

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Constants

HEADERS
HTTP11_CTL_CHARSET
HTTP11_CTL_CHARSET_REGEXP
HTTP11_SEPARATOR
HTTP11_TOKEN_CHARSET
HTTP11_TOKEN_REGEXP
Models
VERSION

Public Class Methods

faraday_client(options) click to toggle source
# File lib/trino/client/faraday_client.rb, line 58
def self.faraday_client(options)
  server = options[:server]
  unless server
    raise ArgumentError, ":server option is required"
  end

  ssl = faraday_ssl_options(options)

  if options[:password] && !ssl
    raise ArgumentError, "Protocol must be https when passing a password"
  end

  url = "#{ssl ? "https" : "http"}://#{server}"
  proxy = options[:http_proxy] || options[:proxy]  # :proxy is obsoleted

  faraday_options = {url: url, proxy: "#{proxy}"}
  faraday_options[:ssl] = ssl if ssl

  faraday = Faraday.new(faraday_options) do |faraday|
    if options[:user] && options[:password]
      faraday.basic_auth(options[:user], options[:password])
    end
    if options[:follow_redirect]
      faraday.use FaradayMiddleware::FollowRedirects
    end
    if options[:gzip]
      faraday.use FaradayMiddleware::Gzip
    end
    faraday.response :logger if options[:http_debug]
    faraday.adapter Faraday.default_adapter
  end

  faraday.headers.merge!(HEADERS)
  faraday.headers.merge!(optional_headers(options))

  return faraday
end
new(*args) click to toggle source
# File lib/trino/client/client.rb, line 75
def self.new(*args)
  Client.new(*args)
end

Private Class Methods

encode_client_info(info) click to toggle source
# File lib/trino/client/faraday_client.rb, line 228
def self.encode_client_info(info)
  if info.is_a?(String)
    info
  else
    JSON.dump(info)
  end
end
encode_client_tags(tags) click to toggle source
# File lib/trino/client/faraday_client.rb, line 236
def self.encode_client_tags(tags)
  Array(tags).join(",")
end
encode_properties(properties) click to toggle source
# File lib/trino/client/faraday_client.rb, line 213
def self.encode_properties(properties)
  properties.map do |k, v|
    token = k.to_s
    field_value = v.to_s  # TODO LWS encoding is not implemented
    unless k =~ HTTP11_TOKEN_REGEXP
      raise Faraday::ClientError, "Key of properties can't include HTTP/1.1 control characters or separators (#{HTTP11_SEPARATOR.map {|c| c =~ /\s/ ? c.dump : c }.join(' ')})"
    end
    if field_value =~ HTTP11_CTL_CHARSET_REGEXP
      raise Faraday::ClientError, "Value of properties can't include HTTP/1.1 control characters"
    end
    field_value = CGI.escape(field_value)
    "#{token}=#{field_value}"
  end
end
faraday_ssl_options(options) click to toggle source
# File lib/trino/client/faraday_client.rb, line 96
def self.faraday_ssl_options(options)
  ssl = options[:ssl]

  case ssl
  when true
    ssl = {verify: true}

  when Hash
    verify = ssl.fetch(:verify, true)
    case verify
    when true
      # detailed SSL options. pass through to faraday
    when nil, false
      ssl = {verify: false}
    else
      raise ArgumentError, "Can't convert #{verify.class} of :verify option of :ssl option to true or false"
    end

  when nil, false
    ssl = false

  else
    raise ArgumentError, "Can't convert #{ssl.class} of :ssl option to true, false, or Hash"
  end

  return ssl
end
optional_headers(options) click to toggle source
# File lib/trino/client/faraday_client.rb, line 124
def self.optional_headers(options)
  usePrestoHeader = false
  if v = options[:model_version] && v < 351
    usePrestoHeader = true
  end

  headers = {}
  if v = options[:user]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_USER] = v
    else
      headers[TrinoHeaders::TRINO_USER] = v
    end
  end
  if v = options[:source]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_SOURCE] = v
    else
      headers[TrinoHeaders::TRINO_SOURCE] = v
    end
  end
  if v = options[:catalog]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_CATALOG] = v
    else
      headers[TrinoHeaders::TRINO_CATALOG] = v
    end
  end
  if v = options[:schema]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_SCHEMA] = v
    else
      headers[TrinoHeaders::TRINO_SCHEMA] = v
    end
  end
  if v = options[:time_zone]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_TIME_ZONE] = v
    else
      headers[TrinoHeaders::TRINO_TIME_ZONE] = v
    end
  end
  if v = options[:language]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_LANGUAGE] = v
    else
      headers[TrinoHeaders::TRINO_LANGUAGE] = v
    end
  end
  if v = options[:properties]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_SESSION] = encode_properties(v)
    else
      headers[TrinoHeaders::TRINO_SESSION] = encode_properties(v)
    end
  end
  if v = options[:client_info]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_CLIENT_INFO] = encode_client_info(v)
    else
      headers[TrinoHeaders::TRINO_CLIENT_INFO] = encode_client_info(v)
    end
  end
  if v = options[:client_tags]
    if usePrestoHeader
      headers[PrestoHeaders::PRESTO_CLIENT_TAGS] = encode_client_tags(v)
    else
      headers[TrinoHeaders::TRINO_CLIENT_TAGS] = encode_client_tags(v)
    end
  end
  if options[:enable_x_msgpack]
    # option name is enable_"x"_msgpack because "Accept: application/x-msgpack" header is
    # not officially supported by Trino. We can use this option only if a proxy server
    # decodes & encodes response body. Once this option is supported by Trino, option
    # name should be enable_msgpack, which might be slightly different behavior.
    headers['Accept'] = 'application/x-msgpack,application/json'
  end
  if v = options[:http_headers]
    headers.merge!(v)
  end
  headers
end