Module: Chroma::APIOperations::Request::ClassMethods

Defined in:
lib/chroma/api_operations/request.rb

Instance Method Summary collapse

Instance Method Details

#execute_request(method, url, params = {}, options = {}) ⇒ Object

Execute an HTTP request and return a monad wrapping the response.

method - The HTTP method to use (e.g. ‘GET’, ‘POST’). Method must be a ‘Symbol`. url - The URL to send the request to. params - The query parameters or request body. Params needs to be in a form of a Hash. options - Additional options to pass to the request.

A ‘Dry::Monads::Result` monad wrapping the response, either a success or failure. The response is a `Chroma::APIOperations::Response` Data object.

Examples

result = execute_request(:get, "https://example.com", {name: "test request"})
if result.success?
  puts "Response status: #{result.success.status}"
  puts "Response body: #{result.success.body}"
else
  puts "Request failed with error: #{result.failure.error}"
end


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chroma/api_operations/request.rb', line 42

def execute_request(method, url, params = {}, options = {})
  uri = URI.parse(url)

  request = build_request(method, uri, params)

  use_ssl = uri.scheme == "https"
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl:) do |http|
    Chroma::Util.log_debug("Sending a request", {method:, uri:, params:})
    http.request(request)
  end

  build_response(response)
rescue => ex
  build_response(ex)
end