Class: Altertable::Lakehouse::Adapters::NetHttpAdapter

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

Returns a new instance of NetHttpAdapter.

Parameters:

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


138
139
140
141
142
143
# File 'lib/altertable/lakehouse/adapters.rb', line 138

def initialize(base_url:, timeout:, open_timeout:, headers: {})
  super
  require "net/http"
  require "uri"
  @uri = URI.parse(@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:



153
154
155
# File 'lib/altertable/lakehouse/adapters.rb', line 153

def delete(path, body: nil, params: {}, headers: {}, &block) # rubocop:disable Lint/UnusedMethodArgument
  request(Net::HTTP::Delete, path, params: params, headers: headers, &block)
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:



145
146
147
# File 'lib/altertable/lakehouse/adapters.rb', line 145

def get(path, body: nil, params: {}, headers: {}, &block) # rubocop:disable Lint/UnusedMethodArgument
  request(Net::HTTP::Get, path, params: params, headers: headers, &block)
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:



149
150
151
# File 'lib/altertable/lakehouse/adapters.rb', line 149

def post(path, body: nil, params: {}, headers: {}, &block)
  request(Net::HTTP::Post, path, body: body, params: params, headers: headers, &block)
end

#request(klass, path, body: nil, params: {}, headers: {}, &block) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/altertable/lakehouse/adapters.rb', line 159

def request(klass, path, body: nil, params: {}, headers: {}, &block)
  # Construct full URI for request
  uri = URI.join(@uri, path)
  uri.query = URI.encode_www_form(params) unless params.nil? || params.empty?

  req = klass.new(uri)
  @headers.merge(headers).each { |k, v| req[k] = v }
  req.body = body if body

  # Net::HTTP start
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: @open_timeout, read_timeout: @timeout) do |http|
    if block_given?
      http.request(req) do |response|
        # Stream the body if block is given
        if response.is_a?(Net::HTTPSuccess)
          response.read_body do |chunk|
            block.call(chunk, response.content_length)
          end
        end
        # Return wrapped response (body might be empty if consumed?)
        # If we consumed the body with read_body, response.body is nil.
        # But our Response struct expects body. For streaming, we might not need body in the Response if block handled it.
        return Response.new(response.code.to_i, response.body, response.to_hash) 
      end
    else
      resp = http.request(req)
      Response.new(resp.code.to_i, resp.body, resp.to_hash)
    end
  end
rescue SocketError, Net::OpenTimeout, Net::ReadTimeout => e
  raise Altertable::Lakehouse::NetworkError, e.message
end