Class: A2A::Plugins::ExampleMiddleware

Inherits:
A2A::Plugin::MiddlewarePlugin show all
Defined in:
lib/a2a/plugins/example_middleware.rb

Instance Attribute Summary

Attributes inherited from A2A::Plugin::Base

#config, #logger

Instance Method Summary collapse

Methods inherited from A2A::Plugin::Base

dependencies, depends_on, inherited, #initialize, plugin_type

Constructor Details

This class inherits a constructor from A2A::Plugin::Base

Instance Method Details

#call(request, next_middleware) ⇒ Object

Process request through middleware

Parameters:

  • request (Hash)

    Request data

  • next_middleware (Proc)

    Next middleware in chain

Returns:

  • (Object)

    Response



21
22
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
50
51
# File 'lib/a2a/plugins/example_middleware.rb', line 21

def call(request, next_middleware)
  start_time = Time.now

  logger&.info("Example Middleware: Processing request #{request[:id]}")

  # Pre-processing
  request = preprocess_request(request)

  begin
    # Call next middleware in chain
    response = next_middleware.call(request)

    # Post-processing
    response = postprocess_response(response, request)

    # Log success
    duration = Time.now - start_time
    logger&.info("Example Middleware: Request completed in #{duration.round(3)}s")

    response
  rescue StandardError => e
    # Error handling
    duration = Time.now - start_time
    logger&.error("Example Middleware: Request failed after #{duration.round(3)}s: #{e.message}")

    # Execute error hooks
    A2A::Plugin.execute_hooks(A2A::Plugin::Events::REQUEST_ERROR, e, request)

    raise
  end
end

#cleanupObject (private)



73
74
75
# File 'lib/a2a/plugins/example_middleware.rb', line 73

def cleanup
  logger&.info("Example Middleware plugin cleaned up (processed #{@request_counter} requests, #{@error_counter} errors)")
end

#middleware_nameObject

Middleware name for identification



13
14
15
# File 'lib/a2a/plugins/example_middleware.rb', line 13

def middleware_name
  "example"
end

#postprocess_response(response, request) ⇒ Object (private)



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/a2a/plugins/example_middleware.rb', line 93

def postprocess_response(response, request)
  # Add processing time to response metadata
  if request[:processing] && request[:processing][:middleware_start]
    processing_time = Time.now.to_f - request[:processing][:middleware_start]

    if response.is_a?(Hash)
      response[:metadata] ||= {}
      response[:metadata][:processing_time] = processing_time
    end
  end

  response
end

#preprocess_request(request) ⇒ Object (private)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/a2a/plugins/example_middleware.rb', line 77

def preprocess_request(request)
  @request_counter += 1

  # Add request ID if not present
  request[:id] ||= SecureRandom.uuid

  # Add processing metadata
  request[:processing] ||= {}
  request[:processing][:middleware_start] = Time.now.to_f

  # Validate request structure
  validate_request_structure(request)

  request
end

#register_hooks(plugin_manager) ⇒ Object

Register hooks for this plugin



54
55
56
57
58
59
60
61
62
63
# File 'lib/a2a/plugins/example_middleware.rb', line 54

def register_hooks(plugin_manager)
  plugin_manager.add_hook(A2A::Plugin::Events::BEFORE_REQUEST, priority: 10) do |request|
    logger&.debug("Example Middleware: Adding request metadata")
    request[:middleware_metadata] ||= {}
    request[:middleware_metadata][:example] = {
      processed_at: Time.now.iso8601,
      version: "1.0.0"
    }
  end
end

#setupObject (private)



67
68
69
70
71
# File 'lib/a2a/plugins/example_middleware.rb', line 67

def setup
  @request_counter = 0
  @error_counter = 0
  logger&.info("Example Middleware plugin initialized")
end

#validate_request_structure(request) ⇒ Object (private)



107
108
109
110
111
112
113
114
115
# File 'lib/a2a/plugins/example_middleware.rb', line 107

def validate_request_structure(request)
  raise A2A::Errors::InvalidRequest, "Request must be a hash" unless request.is_a?(Hash)

  return if request[:method]

  raise A2A::Errors::InvalidRequest, "Request must have a method"

  # Additional validation can be added here
end