31 - Utils Functionalities
Overview
The utils directory contains shared utility functions used across the OpenAlgo platform for authentication, logging, configuration, and common operations.
Utils Directory Structure
utils/
├── auth_utils.py # Broker auth handoff, token revoke, password rules
├── session.py # Protected-route session validity
├── security_middleware.py # Pre-Flask IP ban enforcement
├── logging.py # Centralized logging setup and filters
├── traffic_logger.py # HTTP traffic logging middleware
├── ip_helper.py # Trusted client-IP resolution
├── httpx_client.py # Shared pooled HTTP client
├── socketio_error_handler.py # Socket.IO error suppression
├── latency_monitor.py # Per-endpoint latency tracking
├── health_monitor.py # Background health monitoring daemon
├── api_analyzer.py # Analyzer dashboard statistics
├── event_bus.py # In-process EventBus and the bus singleton
├── db_sessions.py # Scoped-session registry and teardown
├── mpp_slab.py # Market price protection slabs
├── number_formatter.py # Indian number formatting
├── constants.py # Exchanges, products, price types, required fields
├── config.py # Config helpers
├── env_config.py # Environment-derived settings
├── env_check.py # Startup environment validation
├── symbol_utils.py # Symbol parsing helpers
├── trading_calendar.py # Market session and holiday helpers
├── version.py # Single source of truth for VERSION
├── plugin_loader.py # Broker plugin and capability discovery
├── mcp_tool_registry.py # MCP tool definitions and scopes
├── oauth_codes.py # Remote MCP authorization codes
├── oauth_keys.py # Remote MCP signing keys
├── oauth_tokens.py # Remote MCP access and refresh tokens
├── email_utils.py # SMTP sending
├── email_debug.py # SMTP diagnostics
└── ngrok_manager.py # Ngrok tunnel lifecycleKey Utilities
1. Authentication Utilities (auth_utils.py)
2. Session Management (session.py)
3. IP Helper (ip_helper.py)
4. HTTP Client (httpx_client.py)
5. Logging (logging.py)
6. Market Price Protection (mpp_slab.py)
7. Number Formatter (number_formatter.py)
8. Constants (constants.py)
9. Environment Validation (env_check.py)
10. Latency Monitor (latency_monitor.py)
11. Plugin Loader (plugin_loader.py)
12. Ngrok Manager (ngrok_manager.py)
13. Email Utilities (email_utils.py)
14. API Analyzer (api_analyzer.py)
Usage Examples
Using Logger
Using Session Decorator
Using HTTP Client
Using Constants
Key Files Reference
File
Purpose
auth_utils.py
Authentication helpers
session.py
Session management
logging.py
Logging configuration
httpx_client.py
HTTP client
constants.py
Order constants
config.py
Config helpers
ip_helper.py
IP resolution
latency_monitor.py
Performance tracking
Last updated