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

Returns a new instance of FaradayAdapter.

Parameters:

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


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

def initialize(base_url:, timeout:, open_timeout:, headers: {}, **options)
  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.proxy = options[:proxy] if options.key?(:proxy)
    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:



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

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:



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

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:



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

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:



79
80
81
# File 'lib/altertable/lakehouse/adapters.rb', line 79

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