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: {}) ⇒ HttpxAdapter

Returns a new instance of HttpxAdapter.

Parameters:

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


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/altertable/lakehouse/adapters.rb', line 83

def initialize(base_url:, timeout:, open_timeout:, headers: {})
  super
  require "httpx"
  # Configure retries plugin if available or implement manual retries?
  # Httpx has built-in retries via plugin.
  @client = HTTPX.plugin(:retries).with(
    timeout: { operation_timeout: @timeout, connect_timeout: @open_timeout },
    headers: @headers,
    base_url: @base_url
  )
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:



122
123
124
125
# File 'lib/altertable/lakehouse/adapters.rb', line 122

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:



95
96
97
98
# File 'lib/altertable/lakehouse/adapters.rb', line 95

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:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/altertable/lakehouse/adapters.rb', line 100

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:



129
130
131
132
133
134
# File 'lib/altertable/lakehouse/adapters.rb', line 129

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