from typing import Annotated, Required, TypedDictfrom polyflux.core.shared.types import doc, exampleclass Outbound(TypedDict): """Base outgoing message. Container for data being sent. Message types can be customized. """ target: Annotated[ Required[str], doc( "The destination identifier for the outbound data. " "This field specifies where the data should be sent and serves as the primary " "addressing mechanism for the exchange() method in the SupportsExchange protocol. " "The format depends on the specific protocol implementation." ), example("https://api.example.com/endpoint", "HTTP/HTTPS URL endpoint"), example("sensors/temperature/room1", "MQTT topic for IoT messaging"), example("postgresql://user:pass@localhost:5432/mydb", "PostgreSQL connection string"), ]
The destination identifier for the outbound data. This field specifies where the data should be sent and serves as the primary addressing mechanism for the exchange() method. The format depends on the specific protocol implementation.
from typing import TypedDictclass Inbound(TypedDict): """Base incoming message. Container for received data. Message types can be customized. """ pass # Base class has no required fields
from typing import Any, TypeGuardfrom polyflux.core.shared.contracts.protocol_data_unit import is_inbounddef is_inbound(obj: Any) -> TypeGuard[Inbound]: """Check if object is incoming message. Checks if object is a dictionary. Args: obj: The object to check for Inbound type. Returns: True if the object is a dict (potential Inbound), False otherwise. """ return isinstance(obj, dict)
from polyflux.core.shared.contracts.protocol_data_unit import is_inbound# Test with valid inbound datainbound_data = {"status_code": 200, "data": b"response"}if is_inbound(inbound_data): print("This could be an inbound container")else: print("Not an inbound container")# Output: This could be an inbound container# Test with non-dict datainvalid_data = "not a dict"if is_inbound(invalid_data): print("This could be an inbound container")else: print("Not an inbound container")# Output: Not an inbound container
from typing import Any, TypeGuardfrom polyflux.core.shared.contracts.protocol_data_unit import is_outbounddef is_outbound(obj: Any) -> TypeGuard[Outbound]: """Check if object is outgoing message. Checks if object is a dictionary. Args: obj: The object to check for Outbound type. Returns: True if the object is a dict (potential Outbound), False otherwise. """ return isinstance(obj, dict)
from polyflux.core.shared.contracts.protocol_data_unit import is_outbound# Test with valid outbound dataoutbound_data = {"target": "https://api.example.com", "method": "POST"}if is_outbound(outbound_data): print("This could be an outbound container")else: print("Not an outbound container")# Output: This could be an outbound container# Test with non-dict datainvalid_data = ["not", "a", "dict"]if is_outbound(invalid_data): print("This could be an outbound container")else: print("Not an outbound container")# Output: Not an outbound container
Combine type guards with protocol-specific validation:
Copy
from polyflux.core.shared.contracts.protocol_data_unit import is_inbound, is_outbounddef validate_http_outbound(obj: Any) -> bool: """Validate if object is a valid HTTP outbound PDU.""" if not is_outbound(obj): return False # Check required HTTP fields return ( "method" in obj and isinstance(obj["method"], str) and "target" in obj and isinstance(obj["target"], str) )def validate_http_inbound(obj: Any) -> bool: """Validate if object is a valid HTTP inbound PDU.""" if not is_inbound(obj): return False # Check required HTTP fields return ( "status_code" in obj and isinstance(obj["status_code"], int) and "data" in obj and isinstance(obj["data"], bytes) )# Usagerequest_data = { "target": "https://api.example.com", "method": "GET"}if validate_http_outbound(request_data): print("Valid HTTP outbound PDU")else: print("Invalid HTTP outbound PDU")