Class: Altertable::Lakehouse::Adapters::HttpxAdapter

Inherits:
Base
  • Object
show all
Defined in:
lib/altertable/lakehouse/adapters.rb,
sig/altertable/lakehouse/adapters.rbs

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, timeout:, open_timeout:, headers: {}, **options) ⇒ HttpxAdapter

Returns a new instance of HttpxAdapter.

Parameters:

  • base_url: (String)
  • timeout: (Integer, Float)
  • open_timeout: (Integer, Float)
  • headers: (::Hash[String, String]) (defaults to: {})
  • options (Object)


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/altertable/lakehouse/adapters.rb', line 85

def initialize(base_url:, timeout:, open_timeout:, headers: {}, **options)
  super
  require "httpx"
  # Configure retries plugin if available or implement manual retries?
  # Httpx has built-in retries via plugin.
  http_options = {
    timeout: { operation_timeout: @timeout, connect_timeout: @open_timeout },
    headers: @headers,
    base_url: @base_url
  }
  http_options[:proxy] = options[:proxy] if options.key?(:proxy)
  @client = HTTPX.plugin(:retries).with(**http_options)
end

Instance Method Details

#delete(path, body: nil, params: {}, headers: {}, &_block) ⇒ Response

rubocop:disable Lint/UnusedMethodArgument

Parameters:

  • path (String)
  • body: (String, nil) (defaults to: nil)
  • params: (::Hash[Symbol | String, untyped]) (defaults to: {})
  • headers: (::Hash[String, String]) (defaults to: {})

Returns:



126
127
128
129
# File 'lib/altertable/lakehouse/adapters.rb', line 126

def delete(path, body: nil, params: {}, headers: {}, &_block) # rubocop:disable Lint/UnusedMethodArgument
  resp = @client.with(headers: headers).delete(path, params: params)
  wrap_response(resp)
end

#get(path, body: nil, params: {}, headers: {}, &_block) ⇒ Response

rubocop:disable Lint/UnusedMethodArgument

Parameters:

  • path (String)
  • body: (String, nil) (defaults to: nil)
  • params: (::Hash[Symbol | String, untyped]) (defaults to: {})
  • headers: (::Hash[String, String]) (defaults to: {})

Returns:



99
100
101
102
# File 'lib/altertable/lakehouse/adapters.rb', line 99

def get(path, body: nil, params: {}, headers: {}, &_block) # rubocop:disable Lint/UnusedMethodArgument
  resp = @client.with(headers: headers).get(path, params: params)
  wrap_response(resp)
end

#post(path, body: nil, params: {}, headers: {}, &block) ⇒ Response

Parameters:

  • path (String)
  • body: (String, nil) (defaults to: nil)
  • params: (::Hash[Symbol | String, untyped]) (defaults to: {})
  • headers: (::Hash[String, String]) (defaults to: {})

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/altertable/lakehouse/adapters.rb', line 104

def post(path, body: nil, params: {}, headers: {}, &block)
  client = @client.with(headers: headers)
  if block_given?
    # Stream response body
    # HTTPX response streaming:
    response = client.request("POST", path, body: body, params: params, stream: true)
    
    # Check for error immediately
    if response.is_a?(HTTPX::ErrorResponse)
      raise Altertable::Lakehouse::NetworkError, response.error.message
    end

    response.body.each do |chunk|
      block.call(chunk, response.headers["content-length"])
    end
    wrap_response(response)
  else
    resp = client.post(path, body: body, params: params)
    wrap_response(resp)
  end
end

#wrap_response(resp) ⇒ Response

Parameters:

  • resp (Object)

Returns:



133
134
135
136
137
138
# File 'lib/altertable/lakehouse/adapters.rb', line 133

def wrap_response(resp)
  if resp.is_a?(HTTPX::ErrorResponse)
    raise Altertable::Lakehouse::NetworkError, resp.error.message
  end
  Response.new(resp.status, resp.to_s, resp.headers)
end