Class: A2A::Faraday::Middleware::REST::Request

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/a2a/faraday/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). 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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/a2a/faraday/middleware/rest/request.rb', line 22

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)

  env.url.path = path
  env.method = operation.rest_verb.to_sym

  if [:get, :delete].include?(env.method)
    env.params ||= {}
    remaining.each { |k, v| env.params[k.to_s] = v }
    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