Class: Agentic::Extension::ProtocolHandler
- Inherits:
-
Object
- Object
- Agentic::Extension::ProtocolHandler
- Defined in:
- lib/agentic/extension/protocol_handler.rb
Overview
The ProtocolHandler standardizes connections to external systems through consistent interface definitions. It enables integration with various APIs, data sources, and services while providing a uniform access pattern regardless of the underlying protocol or system specifics.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ProtocolHandler
constructor
Initialize a new ProtocolHandler.
-
#list_protocols ⇒ Array<Symbol>
List all registered protocols.
-
#protocol_config(protocol_name) ⇒ Hash?
Get protocol configuration.
-
#protocol_registered?(protocol_name) ⇒ Boolean
Check if a protocol is registered.
-
#register_protocol(protocol_name, implementation, config = {}) ⇒ Boolean
Register a protocol implementation.
-
#send_request(protocol_name, endpoint, options = {}) ⇒ Hash?
Send a request using a registered protocol.
-
#update_protocol_config(protocol_name, config) ⇒ Boolean
Update protocol configuration.
Constructor Details
#initialize(options = {}) ⇒ ProtocolHandler
Initialize a new ProtocolHandler
15 16 17 18 19 |
# File 'lib/agentic/extension/protocol_handler.rb', line 15 def initialize( = {}) @logger = [:logger] || Agentic.logger @default_headers = [:default_headers] || {} @protocols = {} end |
Instance Method Details
#list_protocols ⇒ Array<Symbol>
List all registered protocols
100 101 102 |
# File 'lib/agentic/extension/protocol_handler.rb', line 100 def list_protocols @protocols.keys end |
#protocol_config(protocol_name) ⇒ Hash?
Get protocol configuration
71 72 73 74 75 |
# File 'lib/agentic/extension/protocol_handler.rb', line 71 def protocol_config(protocol_name) return nil unless @protocols.key?(protocol_name) @protocols[protocol_name][:config] end |
#protocol_registered?(protocol_name) ⇒ Boolean
Check if a protocol is registered
93 94 95 |
# File 'lib/agentic/extension/protocol_handler.rb', line 93 def protocol_registered?(protocol_name) @protocols.key?(protocol_name) end |
#register_protocol(protocol_name, implementation, config = {}) ⇒ Boolean
Register a protocol implementation
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/agentic/extension/protocol_handler.rb', line 27 def register_protocol(protocol_name, implementation, config = {}) if !valid_protocol?(implementation) @logger.error("Protocol implementation for '#{protocol_name}' is invalid") return false end @protocols[protocol_name] = { implementation: implementation, config: config } @logger.info("Protocol '#{protocol_name}' registered successfully") true end |
#send_request(protocol_name, endpoint, options = {}) ⇒ Hash?
Send a request using a registered protocol
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/agentic/extension/protocol_handler.rb', line 48 def send_request(protocol_name, endpoint, = {}) unless @protocols.key?(protocol_name) @logger.error("Protocol '#{protocol_name}' is not registered") return nil end protocol = @protocols[protocol_name] [:headers] = @default_headers.merge([:headers] || {}) begin response = protocol[:implementation].send_request(endpoint, .merge(protocol[:config])) @logger.debug("Request sent using '#{protocol_name}' protocol to '#{endpoint}'") response rescue => e @logger.error("Failed to send request using '#{protocol_name}' protocol: #{e.}") nil end end |
#update_protocol_config(protocol_name, config) ⇒ Boolean
Update protocol configuration
82 83 84 85 86 87 |
# File 'lib/agentic/extension/protocol_handler.rb', line 82 def update_protocol_config(protocol_name, config) return false unless @protocols.key?(protocol_name) @protocols[protocol_name][:config].merge!(config) true end |