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

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

Overview

Claims requests under its path prefix (default "/rest"); all other requests pass through untouched. For claimed requests, extracts the HTTP verb, path (with the prefix stripped), 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, path_prefix: "/rest") ⇒ Rest

Returns a new instance of Rest.



24
25
26
27
# File 'lib/a2a/server/bindings/rest.rb', line 24

def initialize(app, path_prefix: "/rest")
  @app = app
  @path_prefix = path_prefix
end

Instance Method Details

#call(env) ⇒ Object



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
59
60
61
62
63
64
# File 'lib/a2a/server/bindings/rest.rb', line 29

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

  return @app.call(env) unless claims?(path)

  env["a2a.verb"] = req.request_method.downcase
  env["a2a.path"] = path.delete_prefix(@path_prefix)

  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::Server::SSE::Stream.headers, stream]
  end

  success_response(result)
end