Class: Altertable::Adapters::HttpxAdapter

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

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, timeout:, headers: {}, proxy: nil) ⇒ HttpxAdapter

This adapter never auto-detects HTTP_PROXY/HTTPS_PROXY (httpx requires the :proxy plugin to be loaded explicitly). proxy: false and the default (unset) are therefore equivalent; pass a URI string to force a specific proxy.

Parameters:

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


56
57
58
59
60
61
62
63
64
65
# File 'lib/altertable/adapters.rb', line 56

def initialize(base_url:, timeout:, headers: {}, proxy: nil)
  super(base_url: base_url, timeout: timeout, headers: headers)
  require "httpx"
  client = proxy ? HTTPX.plugin(:proxy).plugin(:retries).with(proxy: { uri: proxy }) : HTTPX.plugin(:retries)
  @client = client.with(
    timeout: { operation_timeout: @timeout },
    headers: @headers,
    base_url: @base_url
  )
end

Instance Method Details

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

Parameters:

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

Returns:



67
68
69
70
71
72
# File 'lib/altertable/adapters.rb', line 67

def post(path, body: nil, params: {})
  resp = @client.post(path, body: body, params: params)
  wrap_response(resp)
rescue HTTPX::Error => e
  raise Altertable::NetworkError.new(e.message, e)
end

#wrap_response(resp) ⇒ Response

Parameters:

  • resp (Object)

Returns:



76
77
78
79
80
81
# File 'lib/altertable/adapters.rb', line 76

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