Class: A2A::Middleware::FetchTaskOrRaise

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/middleware/fetch_task.rb

Overview

Loads a task from the store and places it on ‘env`. Raises `A2A::TaskNotFoundError` if the task does not exist.

The task ID is read from the request — by default from ‘request.id`. Use `id_field:` to read from a different field, and `from:` to read from a nested object on the request.

Usage:

on "GetTask" do
  use A2A::Middleware::FetchTaskOrRaise, store: sqlite_store
  respond_with -> (env) {
    task = env["a2a.task"]
  }
end

on "GetTaskPushNotificationConfig" do
  use A2A::Middleware::FetchTaskOrRaise, store: sqlite_store, id_field: :task_id
  respond_with -> (env) { ... }
end

Instance Method Summary collapse

Constructor Details

#initialize(app, store:, id_field: :id, from: nil) ⇒ FetchTaskOrRaise

Returns a new instance of FetchTaskOrRaise.



66
67
68
69
70
71
# File 'lib/a2a/middleware/fetch_task.rb', line 66

def initialize(app, store:, id_field: :id, from: nil)
  @app      = app
  @store    = store
  @id_field = id_field
  @from     = from
end

Instance Method Details

#call(env) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/a2a/middleware/fetch_task.rb', line 73

def call(env)
  request = env["a2a.request"]
  source  = @from ? request.public_send(@from) : request
  id      = source.public_send(@id_field)

  task = @store.get(id)
  raise A2A::TaskNotFoundError.new(id) unless task

  env["a2a.task"] = task

  @app.call(env)
end