CMD Pro API Architecture
16.1. Architecture Overview Diagram
16.2. Request Lifecycle
API at a Glance
16.3. Component Descriptions
The Primary Provider is the default data source for CMD Pro. It is responsible for fetching live commodity prices, historical data, and exchange rates from the most reliable and cost-effective API available. The provider is selected based on criteria including data accuracy, API reliability, cost, and geographic relevance to the Pakistani market.
- API Key Management: Secure storage and validation of API credentials
- Endpoint Builder: Constructs API endpoints with proper parameters
- Rate Limiter: Respects API rate limits to prevent throttling
- Data Transformer: Converts raw API responses to standardized internal format
- Error Handling: Comprehensive error handling with descriptive messages
The Secondary Provider acts as the first line of defense against API failures. It is configured as a fallback that is automatically engaged when the Primary Provider experiences downtime, rate limiting, or data corruption. The Secondary Provider offers similar data coverage but may have different API endpoints, response formats, and rate limits.
- Automatic Failover: Seamless transition from Primary to Secondary provider
- Data Consistency: Ensures data consistency between providers
- Provider Status Monitoring: Tracks provider health and availability
- Rate Limiting: Different rate limits may apply
The Demo Provider generates realistic-looking commodity price data without requiring an external API connection. It serves multiple purposes: Demonstration Mode (preview the dashboard during sales demos), Development Mode (work on the plugin without API keys), and Testing Mode (deterministic data for unit and integration testing).
- Synthetic Price Generator: Realistic price movements using random walk and mean reversion
- Trend Simulator: Upward, downward, and sideways market trends
- Volatility Simulator: Configurable price volatility
- Historical Data Replay: Pre-recorded data for consistent testing
- Deterministic Mode: Reproducible data for testing
The Local Cache component is a multi-layered caching system that stores API responses at various levels to optimize performance and reliability. It ensures that data is available immediately for dashboard rendering without waiting for external API calls, and provides a fallback when external APIs are unavailable.
- Layer 1: Memory Cache โ Redis/Memcached (Fastest) โ Active session data
- Layer 2: Object Cache โ WordPress Object Cache (Very Fast) โ Frequently accessed data
- Layer 3: Database Cache โ MySQL
wp_cmdp_api_cache(Persistent) โ Historical cache - Layer 4: File Cache โ Filesystem (Persistent) โ Fallback when DB is unavailable
The Dashboard component is the final layer in the API architecture, responsible for presenting the processed data to users in an intuitive, professional, and actionable format. It consumes data from the local cache and renders live price displays, interactive charts, ticker feeds, and market insights.
- Current Prices: Primary Provider โ Cache (Refresh: 5 minutes)
- Historical Data: Primary Provider โ Cache (Refresh: Every 15 minutes)
- Exchange Rates: Currency API โ Cache (Refresh: Every hour)
- Alerts: Database
wp_cmdp_alerts(Real-time on page load) - Market Insights: Calculated from cache data (On each refresh)
16.4. Provider Comparison Matrix
| Provider | Commodity Coverage | Data Quality | Cost | Reliability | Availability | Avg. Response | Rate Limits | Recommended Use |
|---|---|---|---|---|---|---|---|---|
| Investing.com | Broad (global) | โญ High | Free/Premium | โญ High | 99.9% | 220 ms | Moderate | Primary Provider |
| Alpha Vantage | Broad (global) | โญ High | Free (limited) | โญ High | 99.5% | 250 ms | Strict | Secondary Provider |
| Polygon.io | Broad (global) | โญโญ Very High | Paid | โญโญ Very High | 99.95% | 180 ms | Flexible | Tertiary Provider |
| Bloomberg | Ultra Broad | โญโญโญ Excellent | Very Expensive | โญโญโญ Excellent | 99.99% | 120 ms | Flexible | Enterprise (Future) |
| Reuters | Ultra Broad | โญโญโญ Excellent | Very Expensive | โญโญโญ Excellent | 99.99% | 130 ms | Flexible | Enterprise (Future) |
| Demo Provider | Limited (3 commodities) | โก Synthetic | Free | 100% (synthetic) | 100% | 1 ms | None | Testing / Demos |
16.5. Cache Strategy
CMD Pro implements a multi-tiered caching strategy to optimize performance and minimize external API calls. Each data type has a configurable cache lifetime based on its volatility and importance.
| Data Type | Cache Lifetime | Storage Layer | Refresh Strategy |
|---|---|---|---|
| Live Prices | 5 minutes | Transient + Object Cache | Background cron job |
| Historical Data | 15 minutes | Database Cache | On-demand + cron |
| Exchange Rates | 1 hour | Transient Cache | Scheduled hourly |
| Market News | 30 minutes | Object Cache | Pull on refresh |
| API Responses | Configurable (5-60 min) | Database Cache (wp_cmdp_api_cache) |
TTL-based invalidation |
16.6. API Security
| Security Layer | Implementation |
|---|---|
| API Key Storage | Encrypted using WordPress wp_encrypt(); stored in options table |
| Key Transmission | All API requests use HTTPS (SSL/TLS) |
| Key Validation | Keys are validated before use (test call to API) |
| Key Rotation | Support for multiple keys and easy key renewal |
| Request Signing | Required for some APIs (OAuth, HMAC) |
| Data Minimization | Only request necessary data fields |
| Error Obfuscation | Error messages sanitized before display |
| Audit Trail | All API requests logged in wp_cmdp_logs |
| Compliance | โ OWASP API Top 10 โ GDPR Ready โ HTTPS โ Nonce Protection |
| Security Score | โ โ โ โ โ |
16.7. Error Handling Flow
16.8. API Cost Management
16.9. Future Enhancements
API Architecture Conclusion
This comprehensive API architecture provides a resilient, scalable, and cost-effective data ingestion pipeline for the CMD Pro plugin. With multiple provider fallback, intelligent caching, and graceful degradation, the architecture ensures uninterrupted access to real-time commodity market intelligence while maintaining performance and reliability across all operating conditions.