Class: A2A::Bindings::Rest

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/bindings/rest.rb

Overview

Rack middleware implementing the A2A HTTP+JSON/REST protocol binding.

Extracts the HTTP verb, path, and request body/params into env keys. Calls downstream. On return, wraps env into a REST response with content-type application/a2a+json.

Streaming operations: When the handler sets env to an SSE::Stream (which is a Protocol::HTTP::Body::Readable), Falcon streams it natively —no wrapping, no #each conversion, true async with backpressure.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rest

Returns a new instance of Rest.



21
22
23
# File 'lib/a2a/bindings/rest.rb', line 21

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/a2a/bindings/rest.rb', line 25

def call(env)
  req = Rack::Request.new(env)

  env["a2a.verb"] = req.request_method.downcase
  env["a2a.path"] = req.path_info

  params = {}
  if req.post? || req.put? || req.patch?
    begin
      params = JSON.parse(req.body.read) rescue {}
    end
  end

  # Merge query params for GET/DELETE
  params.merge!(req.params) if req.get? || req.delete?

  env["a2a.body"] = params

  @app.call(env)

  # Check if handler signalled a REST error
  if (err = env["a2a.error"])
    return error_response(err[:http_status] || 400, err[:message], err[:data])
  end

  # Check if handler set up a streaming response.
  # The stream is an SSE::Stream (Protocol::HTTP::Body::Readable).
  if (stream = env["a2a.stream"])
    return [200, A2A::SSE::Stream.headers, stream]
  end

  result = env["a2a.result"]
  success_response(result)
end