networking - API Specification
Version: v1.1.9 | Status: Active | Last Updated: February 2026
Overview
The networking module provides HTTP client utilities with retry logic, timeout handling, and WebSocket support for network operations.
Classes
HTTPClient
Robust HTTP client with automatic retries and timeout support.
from codomyrmex.networking import HTTPClient
Constructor
HTTPClient(
timeout: int = 30,
max_retries: int = 3,
retry_backoff: float = 1.0,
headers: Optional[Dict[str, str]] = None
)
| Parameter | Type | Default | Description |
|---|---|---|---|
timeout | int | 30 | Request timeout in seconds |
max_retries | int | 3 | Maximum retry attempts |
retry_backoff | float | 1.0 | Backoff factor between retries |
headers | Dict[str, str] | None | Default headers for all requests |
Methods
get
def get(url: str, **kwargs) -> Response
Send GET request.
post
def post(url: str, data: Any = None, **kwargs) -> Response
Send POST request.
put
def put(url: str, data: Any = None, **kwargs) -> Response
Send PUT request.
delete
def delete(url: str, **kwargs) -> Response
Send DELETE request.
request
def request(method: str, url: str, **kwargs) -> Response
Send custom HTTP request.
| Parameter | Type | Description |
|---|---|---|
method | str | HTTP method (GET, POST, PUT, DELETE, etc.) |
url | str | Request URL |
**kwargs | - | Additional request options (headers, params, json, etc.) |
Returns: Response object
Raises: NetworkingError if request fails
Response
HTTP response dataclass.
@dataclass
class Response:
status_code: int
headers: Dict[str, Any]
content: bytes
text: str
json_data: Optional[Dict[str, Any]] = None
Methods
json
def json() -> Dict[str, Any]
Parse and return JSON response data.
WebSocketClient
WebSocket client for real-time communication.
from codomyrmex.networking import WebSocketClient
Constructor
WebSocketClient(
url: str,
on_message: Optional[Callable] = None,
on_error: Optional[Callable] = None,
on_close: Optional[Callable] = None
)
Methods
connect
def connect() -> bool
Establish WebSocket connection.
send
def send(message: str) -> bool
Send message through WebSocket.
close
def close() -> None
Close WebSocket connection.
Exceptions
NetworkingError
from codomyrmex.networking.http_client import NetworkingError
Raised by HTTPClient when a request or JSON decoding fails. Inherits from CodomyrmexError.
Usage Examples
Basic HTTP Requests
from codomyrmex.networking import HTTPClient
# Create client with defaults
client = HTTPClient(timeout=10, max_retries=3)
# GET request
response = client.get("https://api.example.com/data")
print(f"Status: {response.status_code}")
print(f"Data: {response.json()}")
# POST request with JSON
response = client.post(
"https://api.example.com/items",
json={"name": "item1", "value": 42}
)
Custom Headers
client = HTTPClient(
headers={
"Authorization": "Bearer token123",
"User-Agent": "Codomyrmex/1.0"
}
)
response = client.get("https://api.example.com/protected")
Error Handling
from codomyrmex.networking import HTTPClient
from codomyrmex.networking.http_client import NetworkingError
client = HTTPClient()
try:
response = client.get("https://unreachable.example.com")
except NetworkingError as e:
print(f"Request failed: {e}")
WebSocket Communication
from codomyrmex.networking import WebSocketClient
def on_message(message):
print(f"Received: {message}")
client = WebSocketClient(
"wss://ws.example.com/socket",
on_message=on_message
)
client.connect()
client.send("Hello, Server!")
client.close()
Integration
Dependencies
requests- HTTP client librarywebsocket-client- WebSocket support (optional)urllib3- Retry utilitiescodomyrmex.logging_monitoringfor loggingcodomyrmex.exceptionsfor error handling
Related Modules
Navigation
- Human Documentation: README.md
- Technical Documentation: AGENTS.md
- Functional Specification: SPEC.md
- Parent: codomyrmex