Class: A2A::Client::Middleware::REST::Request

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/a2a/client/middleware/rest/request.rb

Overview

Faraday request middleware that rewrites the request for the A2A HTTP+JSON/REST protocol binding.

Reads env.request.context to determine the HTTP verb and path. Interpolates path parameters from the request body (e.g. id=* placeholders) and prefixes the /rest mount point. For GET/DELETE operations, remaining params become query string parameters.

Sets Content-Type to application/a2a+json for POST requests.

Instance Method Summary collapse

Instance Method Details

#on_request(env) ⇒ Object



23
24
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
# File 'lib/a2a/client/middleware/rest/request.rb', line 23

def on_request(env)
  operation = env.request.context&.dig(:a2a_operation)
  return unless operation

  params = env.body || {}
  path = interpolate_path(operation.rest_path, params)
  remaining = remove_path_params(operation.rest_path, params)

  # Prefix /rest under the base path, so servers mounted at a
  # subpath (e.g. "/agent1") keep working.
  env.url.path = "#{env.url.path.chomp("/")}/rest#{path}"
  env.method = operation.rest_verb.to_sym

  if [:get, :delete].include?(env.method)
    # The URL is already built by the time request middleware runs,
    # so query params must be encoded into env.url directly —
    # env.params is not consulted after this point.
    env.url.query = URI.encode_www_form(remaining) unless remaining.empty?
    env.body = nil
  else
    # Serialize to JSON here rather than relying on Faraday's
    # built-in :json middleware, which only recognizes
    # application/json and application/vnd.*+json content types.
    env.body = remaining.is_a?(String) ? remaining : JSON.generate(remaining)
    env.request_headers["content-type"] = "application/a2a+json"
  end
end