Class: A2A::Server::Bindings::Rest

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

Overview

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.



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

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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/server/bindings/rest.rb', line 26

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

  result = @app.call(env)

  # Check if the result is an error object
  if result.is_a?(A2A::Error)
    return error_response(result.http_status, result.message, result.error_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

  success_response(result)
end