Class: PatientHttp::Sidekiq::RequestExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_http/sidekiq/request_executor.rb

Overview

Helper methods for executing HTTP requests asynchronously.

Class Method Summary collapse

Class Method Details

.execute(request, callback:, sidekiq_job: nil, task_handler: nil, synchronous: false, callback_args: nil, raise_error_responses: false, request_id: nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute the request directly on the async processor.

This method enqueues the request directly to the async processor. Used internally by RequestWorker.

When the request completes, the callback's on_complete method is called with a Response object. If an error occurs (network error, timeout, or non-2xx response if raise_error_responses is true), the on_error method is called with an Error object.

Parameters:

  • request (Request)

    the HTTP request to execute

  • callback (Class, String)

    Callback service class with on_complete and on_error instance methods, or its fully qualified class name.

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

    Sidekiq job hash with "class" and "args" keys. If not provided, uses PatientHttp::Sidekiq::Context.current_job. This requires the PatientHttp::Sidekiq::Context::Middleware to be added to the Sidekiq server middleware chain.

  • task_handler (PatientHttp::TaskHandler, nil) (defaults to: nil)

    A prebuilt task handler. When provided, the sidekiq_job parameter is ignored and no handler is built from it. Used for direct execution on the local processor.

  • synchronous (Boolean) (defaults to: false)

    If true, runs the request inline (for testing).

  • callback_args (#to_h, nil) (defaults to: nil)

    Arguments to pass to callback via the Response/Error object. Must respond to to_h and contain only JSON-native types (nil, true, false, String, Integer, Float, Array, Hash). All hash keys will be converted to strings for serialization. Access via response.callback_args or error.callback_args using symbol or string keys.

  • raise_error_responses (Boolean) (defaults to: false)

    If true, treats non-2xx responses as errors and calls on_error instead of on_complete. Defaults to false.

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

    Unique request ID for tracking. If nil, a new UUID will be generated.

Returns:

  • (String)

    the request ID



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/patient_http/sidekiq/request_executor.rb', line 39

def execute(
  request,
  callback:,
  sidekiq_job: nil,
  task_handler: nil,
  synchronous: false,
  callback_args: nil,
  raise_error_responses: false,
  request_id: nil
)
  task_handler ||= TaskHandler.new(validate_sidekiq_job(sidekiq_job))
  config = PatientHttp::Sidekiq.configuration

  task = PatientHttp::RequestTask.new(
    request: request,
    task_handler: task_handler,
    callback: callback,
    callback_args: callback_args,
    raise_error_responses: raise_error_responses,
    id: request_id,
    default_max_redirects: config.max_redirects
  )

  # Run the request inline if Sidekiq::Testing.inline! is enabled
  if synchronous || async_disabled?
    PatientHttp::SynchronousExecutor.new(
      task,
      config: config,
      on_complete: ->(response) { PatientHttp::Sidekiq.invoke_completion_callbacks(response) },
      on_error: ->(error) { PatientHttp::Sidekiq.invoke_error_callbacks(error) }
    ).call
    return task.id
  end

  # Check if processor is running
  processor = PatientHttp::Sidekiq.processor
  unless processor&.running?
    raise PatientHttp::NotRunningError.new("Cannot enqueue request: processor is not running")
  end

  processor.enqueue(task)

  task.id
end