Class: Altertable::Lakehouse::Adapters::NetHttpAdapter
- Defined in:
- lib/altertable/lakehouse/adapters.rb,
sig/altertable/lakehouse/adapters.rbs
Instance Method Summary collapse
-
#delete(path, body: nil, params: {}, headers: {}, &block) ⇒ Response
rubocop:disable Lint/UnusedMethodArgument.
-
#get(path, body: nil, params: {}, headers: {}, &block) ⇒ Response
rubocop:disable Lint/UnusedMethodArgument.
-
#initialize(base_url:, timeout:, open_timeout:, headers: {}, **options) ⇒ NetHttpAdapter
constructor
A new instance of NetHttpAdapter.
- #post(path, body: nil, params: {}, headers: {}, &block) ⇒ Response
- #request(klass, path, body: nil, params: {}, headers: {}, &block) ⇒ Object
Constructor Details
#initialize(base_url:, timeout:, open_timeout:, headers: {}, **options) ⇒ NetHttpAdapter
Returns a new instance of NetHttpAdapter.
142 143 144 145 146 147 |
# File 'lib/altertable/lakehouse/adapters.rb', line 142 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
157 158 159 |
# File 'lib/altertable/lakehouse/adapters.rb', line 157 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
149 150 151 |
# File 'lib/altertable/lakehouse/adapters.rb', line 149 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
153 154 155 |
# File 'lib/altertable/lakehouse/adapters.rb', line 153 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
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 191 192 193 194 195 196 |
# File 'lib/altertable/lakehouse/adapters.rb', line 163 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 start_args = [uri.host, uri.port] start_args << @options[:proxy] if @options.key?(:proxy) Net::HTTP.start(*start_args, 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. end |