Class: Railbox::Queue::HttpQueue

Inherits:
BaseQueue show all
Defined in:
lib/railbox/queue/http_queue.rb

Overview

HttpQueue is responsible for enqueuing HTTP request actions into the transactional outbox.

Usage example:

Railbox::HttpQueue.enqueue(
url: "https://example.com/api",
method: :post,
body: { data: 1 },
headers: { "Authorization" => "Bearer ..." },
query: { foo: "bar" },
meta: { correlation_id: "123" }
)

This creates a Railbox::TransactionalOutboxMutator record, which will be processed later by a background worker, providing reliable, traceable, and decoupled execution of HTTP requests.

A ValidationError will be raised if the url, method, or body are invalid.

Constant Summary collapse

OPTIONS =
i[query meta group].freeze
METHODS =
i[get post put patch delete].freeze

Class Method Summary collapse

Class Method Details

.enqueue(url:, method: :post, body: {}, headers: {}, **opts) ⇒ Boolean

Enqueues an HTTP request action for asynchronous processing via the transactional outbox.

Parameters:

  • The HTTP endpoint URL (must begin with http:// or https://).

  • (defaults to: :post)

    HTTP method to use (:get, :post, :put, :patch, :delete). Defaults to :post.

  • (defaults to: {})

    Request payload (must be a Hash). Defaults to empty hash.

  • (defaults to: {})

    Optional HTTP headers.

  • Additional options: query, meta, group.

Returns:

  • true if enqueuing is successful.

Raises:

  • If the url, method, or body are invalid.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/railbox/queue/http_queue.rb', line 35

def enqueue(url:, method: :post, body: {}, headers: {}, **opts)
  opts.deep_symbolize_keys!.slice!(*OPTIONS)
  validate_options(url, method, body)

  to_queue(
    action_type: 'http_request',
    action_data: {url: url, method_name: method},
    body:        body,
    headers:     headers,
    status:      'in_progress',
    **opts
  )

  true
end