Class: LLM::A2A::Transport::HTTP

Inherits:
Object
  • Object
show all
Includes:
Transport::Utils
Defined in:
lib/llm/a2a/transport/http.rb

Overview

The HTTP class provides the HTTP+JSON/REST protocol binding for the A2A protocol. It sends JSON payloads to A2A agent endpoints and handles both synchronous responses and Server-Sent Events for streaming operations.

This transport implements the HTTP+JSON/REST binding as defined in the A2A specification (Section 11).

Instance Method Summary collapse

Constructor Details

#initialize(url:, headers: {}, timeout: nil, transport: nil, protocol_version: "1.0") ⇒ HTTP

Returns a new instance of HTTP.

Parameters:

  • url (String)

    The base URL of the A2A agent

  • headers (Hash<String, String>) (defaults to: {})

    Extra HTTP headers

  • timeout (Integer, nil) (defaults to: nil)

    The timeout in seconds

  • transport (LLM::Transport, Class, nil) (defaults to: nil)

    Override transport

  • protocol_version (String) (defaults to: "1.0")

    The A2A protocol version header



22
23
24
25
26
27
# File 'lib/llm/a2a/transport/http.rb', line 22

def initialize(url:, headers: {}, timeout: nil, transport: nil, protocol_version: "1.0")
  @uri = URI.parse(url)
  @headers = headers
  @protocol_version = protocol_version
  @transport = resolve_transport(@uri, transport, timeout)
end

Instance Method Details

#get(path, accept: "application/json") ⇒ Hash

Sends a GET request.

Parameters:

  • path (String)

    The URL path

Returns:

  • (Hash)


33
34
35
36
37
# File 'lib/llm/a2a/transport/http.rb', line 33

def get(path, accept: "application/json")
  req = Net::HTTP::Get.new(request_path(path), headers(accept:))
  res = transport.request(req, owner: self)
  parse_response(res)
end

#post(path, body, content_type: "application/json", accept: "application/json") ⇒ Hash

Sends a POST request.

Parameters:

  • path (String)

    The URL path

  • body (Hash)

    The JSON body

Returns:

  • (Hash)


44
45
46
47
48
49
# File 'lib/llm/a2a/transport/http.rb', line 44

def post(path, body, content_type: "application/json", accept: "application/json")
  req = Net::HTTP::Post.new(request_path(path), headers(content_type:, accept:))
  req.body = LLM.json.dump(body)
  res = transport.request(req, owner: self)
  parse_response(res)
end

#delete(path, accept: "application/json") ⇒ Hash

Sends a DELETE request.

Parameters:

  • path (String)

    The URL path

Returns:

  • (Hash)


55
56
57
58
59
# File 'lib/llm/a2a/transport/http.rb', line 55

def delete(path, accept: "application/json")
  req = Net::HTTP::Delete.new(request_path(path), headers(accept:))
  res = transport.request(req, owner: self)
  parse_response(res)
end

#get_stream(path) {|event| ... } ⇒ void

This method returns an undefined value.

Sends a GET request with a streaming (SSE) response.

The block is called for each event parsed from the event stream.

Parameters:

  • path (String)

    The URL path

Yield Parameters:



68
69
70
71
# File 'lib/llm/a2a/transport/http.rb', line 68

def get_stream(path, &on_event)
  req = Net::HTTP::Get.new(request_path(path), headers(accept: "text/event-stream"))
  stream(req, &on_event)
end

#post_stream(path, body, content_type: "application/json") {|event| ... } ⇒ void

This method returns an undefined value.

Sends a POST request with a streaming (SSE) response.

The block is called for each event parsed from the event stream.

Parameters:

  • path (String)

    The URL path

  • body (Hash)

    The JSON body

Yield Parameters:



81
82
83
84
85
# File 'lib/llm/a2a/transport/http.rb', line 81

def post_stream(path, body, content_type: "application/json", &on_event)
  req = Net::HTTP::Post.new(request_path(path), headers(content_type:, accept: "text/event-stream"))
  req.body = LLM.json.dump(body)
  stream(req, &on_event)
end