class Rooibos::Command::Http

Performs HTTP requests and sends the response as a message.

Applications fetch data from APIs. Users expect responsive interfaces while requests complete. Managing HTTP connections, timeouts, and threading manually is error-prone.

This command executes HTTP requests off the main thread. The runtime dispatches it and routes the response back to your update function as a Message::HttpResponse.

Use it to fetch API data, post forms, or interact with web services.

Prefer the Command.http factory method for convenience. The constructor supports flexible DWIM (Do What I Mean) arity.

Example

# Using the factory method (recommended)
Command.http(:get, "/api/users", :users)
Command.http(get: "/api/users", envelope: :users)
Command.http(:post, "/api/users", '{"name":"Jo"}', :created)

# Using the class directly
Http.new(:get, "/api/users", :users)

# Pattern-match on the response
def update(message, model)
  case message
  in { type: :http, envelope: :users, status: 200, body: }
    model.with(users: JSON.parse(body))
  in { type: :http, envelope: :users, error: }
    model.with(error:)
  end
end