Class: Altertable::Lakehouse::Adapters::FaradayAdapter

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

Returns a new instance of FaradayAdapter.

Parameters:

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


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/altertable/lakehouse/adapters.rb', line 28

def initialize(base_url:, timeout:, open_timeout:, headers: {})
  super
  require "faraday"
  require "faraday/retry"
  require "faraday/net_http"
  
  @conn = Faraday.new(url: @base_url) do |f|
    @headers.each { |k, v| f.headers[k] = v }
    f.options.timeout = @timeout
    f.options.open_timeout = @open_timeout
    f.request :retry, max: 3, interval: 0.05, backoff_factor: 2
    f.adapter Faraday.default_adapter
  end
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:



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

def delete(path, body: nil, params: {}, headers: {}, &_block) # rubocop:disable Lint/UnusedMethodArgument
  resp = @conn.delete(path, params, headers)
  wrap_response(resp)
rescue Faraday::ConnectionFailed => e
  raise Altertable::Lakehouse::NetworkError, e.message
rescue Faraday::TimeoutError => e
  raise Altertable::Lakehouse::TimeoutError, e.message
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:



43
44
45
46
47
48
49
50
# File 'lib/altertable/lakehouse/adapters.rb', line 43

def get(path, body: nil, params: {}, headers: {}, &_block) # rubocop:disable Lint/UnusedMethodArgument
  resp = @conn.get(path, params, headers)
  wrap_response(resp)
rescue Faraday::ConnectionFailed => e
  raise Altertable::Lakehouse::NetworkError, e.message
rescue Faraday::TimeoutError => e
  raise Altertable::Lakehouse::TimeoutError, e.message
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:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/altertable/lakehouse/adapters.rb', line 52

def post(path, body: nil, params: {}, headers: {}, &block)
  resp = @conn.post(path) do |req|
    req.params = params if params
    req.headers = req.headers.merge(headers) unless headers.empty?
    req.body = body
    req.options.on_data = block if block_given?
  end
  wrap_response(resp)
rescue Faraday::ConnectionFailed => e
  raise Altertable::Lakehouse::NetworkError, e.message
rescue Faraday::TimeoutError => e
  raise Altertable::Lakehouse::TimeoutError, e.message
end

#wrap_response(resp) ⇒ Response

Parameters:

  • resp (Object)

Returns:



77
78
79
# File 'lib/altertable/lakehouse/adapters.rb', line 77

def wrap_response(resp)
  Response.new(resp.status, resp.body, resp.headers)
end