Module: PatientHttp::RequestHelper

Extended by:
RequestHelper
Includes:
HttpMethodHelpers
Included in:
RequestHelper
Defined in:
lib/patient_http/request_helper.rb

Overview

Mixin that provides a compact API for scheduling async HTTP requests.

Include this module in your class to get instance-level and class-level helpers for building requests and dispatching them through a registered handler.

This module allows you to use the same interface for making HTTP requests while swapping out the underlying queueing mechanism for handling responses asynchronously. By registering a custom handler, you can integrate with any job queue system (Sidekiq, Solid Queue, etc.) without changing your application code that makes HTTP requests. This decouples your request interface from your async processing infrastructure.

The common workflow is:

  1. Register a global request handler with register_handler.

  2. Include this module in a class.

  3. Optionally configure defaults with request_template.

  4. Call ‘async_get`, `async_post`, `async_put`, `async_patch`, `async_delete`, or `async_request`.

Examples:

Register a handler

PatientHttp.register_handler do |request:, callback:, callback_args: nil, raise_error_responses: nil|
  # Dispatch the request through your app-specific task/enqueue operation
  # and return the request id
end

Include in a class and enqueue requests

class ApiClient
  include PatientHttp::RequestHelper

  request_template base_url: "https://api.example.com", headers: {"Authorization" => "Bearer token"}

  def fetch_user(user_id)
    async_get("/users/#{user_id}", callback: UserCallback, callback_args: {"user_id" => user_id})
  end
end

Defined Under Namespace

Modules: ClassMethods, HttpMethodHelpers

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HttpMethodHelpers

#async_delete, #async_get, #async_patch, #async_post, #async_put

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Hooks helper behavior into the including class.

Extends the class with ClassMethods and initializes template storage.

Parameters:

  • base (Class)

    class including this module



48
49
50
51
# File 'lib/patient_http/request_helper.rb', line 48

def included(base)
  base.extend(ClassMethods)
  base.instance_variable_set(:@patient_http_request_template, nil)
end

Instance Method Details

#async_request(method, url, callback:, headers: nil, body: nil, json: nil, params: nil, timeout: nil, raise_error_responses: nil, callback_args: nil) ⇒ Object

Dispatches an asynchronous HTTP request from an instance context.

This delegates to PatientHttp::RequestHelper::ClassMethods#async_request on the including class.

Parameters:

  • method (Symbol)

    HTTP method (‘:get`, `:post`, `:put`, `:patch`, `:delete`)

  • url (String)

    absolute URL or path (when using a request template)

  • callback (Class, String)

    callback class to handle the response

  • headers (Hash, nil) (defaults to: nil)

    request headers

  • body (String, nil) (defaults to: nil)

    raw request body

  • json (Hash, Array, nil) (defaults to: nil)

    JSON payload encoded by the request layer

  • params (Hash, nil) (defaults to: nil)

    query parameters

  • timeout (Numeric, nil) (defaults to: nil)

    timeout in seconds for this request

  • raise_error_responses (Boolean, nil) (defaults to: nil)

    when true, non-success responses are reported as errors

  • callback_args (Hash, nil) (defaults to: nil)

    JSON-compatible callback arguments

Returns:

  • (Object)

    return value from the registered request handler



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/patient_http/request_helper.rb', line 202

def async_request(
  method,
  url,
  callback:,
  headers: nil,
  body: nil,
  json: nil,
  params: nil,
  timeout: nil,
  raise_error_responses: nil,
  callback_args: nil
)
  self.class.async_request(
    method,
    url,
    callback: callback,
    headers: headers,
    body: body,
    json: json,
    params: params,
    timeout: timeout,
    raise_error_responses: raise_error_responses,
    callback_args: callback_args
  )
end