File size: 12,376 Bytes
9d92c17 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# Auto Provider Loader (APL) - Usage Guide
**Version:** 1.0
**Last Updated:** 2025-11-16
**Status:** PRODUCTION READY β
---
## Overview
The Auto Provider Loader (APL) is a **real-data-only** system that automatically discovers, validates, and integrates cryptocurrency data providers (both HTTP APIs and Hugging Face models) into your application.
### Key Features
- π **Automatic Discovery** - Scans JSON resources for provider definitions
- β
**Real Validation** - Tests each provider with actual API calls (NO MOCKS)
- π§ **Smart Integration** - Automatically adds valid providers to config
- π **Comprehensive Reports** - Generates detailed validation reports
- β‘ **Performance Optimized** - Parallel validation with configurable timeouts
- π‘οΈ **Auth Handling** - Detects and handles API key requirements
---
## Architecture
### Components
1. **provider_validator.py** - Core validation engine
- Validates HTTP JSON APIs
- Validates HTTP RPC endpoints
- Validates Hugging Face models
- Handles authentication requirements
2. **auto_provider_loader.py** - Discovery and orchestration
- Scans resource files
- Coordinates validation
- Integrates valid providers
- Generates reports
### Provider Types Supported
| Type | Description | Example |
|------|-------------|---------|
| `HTTP_JSON` | REST APIs returning JSON | CoinGecko, CoinPaprika |
| `HTTP_RPC` | JSON-RPC endpoints | Ethereum nodes, BSC RPC |
| `WEBSOCKET` | WebSocket connections | Alchemy WS, real-time feeds |
| `HF_MODEL` | Hugging Face models | Sentiment analysis models |
---
## Quick Start
### 1. Basic Usage
Run the APL to discover and validate all providers:
```bash
cd /workspace
python3 auto_provider_loader.py
```
This will:
- Scan `api-resources/*.json` for provider definitions
- Scan `providers_config*.json` for existing providers
- Discover HF models from `backend/services/`
- Validate each provider with real API calls
- Generate comprehensive reports
- Update `providers_config_extended.json` with valid providers
### 2. Understanding Output
```
================================================================================
π AUTO PROVIDER LOADER (APL) - REAL DATA ONLY
================================================================================
π‘ PHASE 1: DISCOVERY
Found 339 HTTP provider candidates
Found 4 HF model candidates
π¬ PHASE 2: VALIDATION
β
Valid providers
β Invalid providers
β οΈ Conditionally available (requires auth)
π PHASE 3: COMPUTING STATISTICS
π§ PHASE 4: INTEGRATION
π PHASE 5: GENERATING REPORTS
```
### 3. Generated Files
After running APL, you'll find:
- `PROVIDER_AUTO_DISCOVERY_REPORT.md` - Human-readable report
- `PROVIDER_AUTO_DISCOVERY_REPORT.json` - Machine-readable detailed results
- `providers_config_extended.backup.{timestamp}.json` - Config backup
- `providers_config_extended.json` - Updated with new valid providers
---
## Validation Logic
### HTTP Providers
For each HTTP provider, APL:
1. **Checks URL structure**
- Detects placeholder variables (`{API_KEY}`, `{PROJECT_ID}`)
- Identifies WebSocket endpoints (`ws://`, `wss://`)
2. **Determines endpoint type**
- JSON REST API β GET request to test endpoint
- JSON-RPC β POST request with `eth_blockNumber` method
3. **Makes real test call**
- 8-second timeout
- Handles redirects
- Validates response format
4. **Classifies result**
- β
`VALID` - Responds with 200 OK and valid data
- β `INVALID` - Connection fails, timeout, or error response
- β οΈ `CONDITIONALLY_AVAILABLE` - Requires API key (401/403)
- βοΈ `SKIPPED` - WebSocket (requires separate validation)
### Hugging Face Models
For each HF model, APL:
1. **Queries HF Hub API**
- Checks if model exists: `GET https://huggingface.co/api/models/{model_id}`
- Does NOT download or load the full model (saves time/resources)
2. **Validates accessibility**
- β
`VALID` - Model found and publicly accessible
- β οΈ `CONDITIONALLY_AVAILABLE` - Requires HF_TOKEN
- β `INVALID` - Model not found (404) or other error
---
## Configuration
### Environment Variables
APL respects these environment variables:
| Variable | Purpose | Default |
|----------|---------|---------|
| `HF_TOKEN` | Hugging Face API token | None |
| `ETHERSCAN_API_KEY` | Etherscan API key | None |
| `BSCSCAN_API_KEY` | BSCScan API key | None |
| `INFURA_PROJECT_ID` | Infura project ID | None |
| `ALCHEMY_API_KEY` | Alchemy API key | None |
### Validation Timeout
Default timeout is 8 seconds. To customize:
```python
from auto_provider_loader import AutoProviderLoader
apl = AutoProviderLoader()
apl.validator.timeout = 15.0 # 15 seconds
await apl.run()
```
---
## Adding New Provider Sources
### 1. Add to JSON Resources
Create or update a JSON file in `api-resources/`:
```json
{
"registry": {
"my_providers": [
{
"id": "my_api",
"name": "My API",
"category": "market_data",
"base_url": "https://api.example.com/v1",
"endpoints": {
"prices": "/prices"
},
"auth": {
"type": "none"
}
}
]
}
}
```
### 2. Re-run APL
```bash
python3 auto_provider_loader.py
```
APL will automatically discover and validate your new provider.
---
## Integration with Existing Code
### Using Validated Providers
After APL runs, valid providers are in `providers_config_extended.json`:
```python
import json
# Load validated providers
with open('providers_config_extended.json', 'r') as f:
config = json.load(f)
# Get all valid providers
valid_providers = config['providers']
# Use a specific provider
coingecko = valid_providers['coingecko']
print(f"Provider: {coingecko['name']}")
print(f"Category: {coingecko['category']}")
print(f"Response time: {coingecko['response_time_ms']}ms")
```
### Filtering by Category
```python
# Get all market data providers
market_providers = {
pid: data for pid, data in valid_providers.items()
if data.get('category') == 'market_data'
}
```
---
## Conditional Providers
Providers marked as `CONDITIONALLY_AVAILABLE` require API keys:
### 1. Check Requirements
See `PROVIDER_AUTO_DISCOVERY_REPORT.md` for required env vars:
```markdown
### Conditionally Available Providers (90)
- **Etherscan** (`etherscan_primary`)
- Required: `ETHERSCAN_PRIMARY_API_KEY` environment variable
- Reason: HTTP 401 - Requires authentication
```
### 2. Set Environment Variables
```bash
export ETHERSCAN_API_KEY="your_key_here"
export BSCSCAN_API_KEY="your_key_here"
```
### 3. Re-run Validation
```bash
python3 auto_provider_loader.py
```
Previously conditional providers will now validate as VALID if keys are correct.
---
## Performance Tuning
### Parallel Validation
HTTP providers are validated in batches of 10 to balance speed and resource usage:
```python
# In auto_provider_loader.py
batch_size = 10 # Adjust based on your needs
```
Larger batches = faster but more network load
Smaller batches = slower but more conservative
### Timeout Adjustment
For slow or distant APIs:
```python
validator = ProviderValidator(timeout=15.0) # 15 seconds
```
---
## Troubleshooting
### Issue: Many providers marked INVALID
**Possible causes:**
- Network connectivity issues
- Rate limiting (try again later)
- Providers genuinely down
**Solution:** Check individual error reasons in report
### Issue: All providers CONDITIONALLY_AVAILABLE
**Cause:** Most providers require API keys
**Solution:** Set required environment variables
### Issue: HF models all INVALID
**Causes:**
- No internet connection to HuggingFace
- Models moved or renamed
- Rate limiting from HF Hub
**Solution:** Check HF Hub status, verify model IDs
### Issue: Validation takes too long
**Solutions:**
- Reduce batch size
- Decrease timeout
- Filter providers before validation
---
## Advanced Usage
### Validating Specific Providers
```python
from provider_validator import ProviderValidator
import asyncio
async def validate_one():
validator = ProviderValidator()
result = await validator.validate_http_provider(
"coingecko",
{
"name": "CoinGecko",
"category": "market_data",
"base_url": "https://api.coingecko.com/api/v3",
"endpoints": {"ping": "/ping"}
}
)
print(f"Status: {result.status}")
print(f"Response time: {result.response_time_ms}ms")
asyncio.run(validate_one())
```
### Custom Discovery Logic
```python
from auto_provider_loader import AutoProviderLoader
class CustomAPL(AutoProviderLoader):
def discover_http_providers(self):
# Your custom logic
providers = super().discover_http_providers()
# Filter or augment
return [p for p in providers if p['data'].get('free') == True]
apl = CustomAPL()
await apl.run()
```
---
## API Reference
### ProviderValidator
```python
class ProviderValidator:
def __init__(self, timeout: float = 10.0)
async def validate_http_provider(
provider_id: str,
provider_data: Dict[str, Any]
) -> ValidationResult
async def validate_hf_model(
model_id: str,
model_name: str,
pipeline_tag: str = "sentiment-analysis"
) -> ValidationResult
def get_summary() -> Dict[str, Any]
```
### AutoProviderLoader
```python
class AutoProviderLoader:
def __init__(self, workspace_root: str = "/workspace")
def discover_http_providers() -> List[Dict[str, Any]]
def discover_hf_models() -> List[Dict[str, Any]]
async def validate_all_http_providers(providers: List)
async def validate_all_hf_models(models: List)
def integrate_valid_providers() -> Dict[str, Any]
def generate_reports()
async def run() # Main entry point
```
---
## Best Practices
1. **Regular Re-validation**
- Run APL weekly to catch provider changes
- Providers can go offline or change endpoints
2. **Monitor Conditional Providers**
- Set up API keys for high-value providers
- Track which providers need auth
3. **Review Reports**
- Check invalid providers for patterns
- Update configs based on error reasons
4. **Backup Configs**
- APL creates automatic backups
- Keep manual backups before major changes
5. **Test Integration**
- After APL runs, test your application
- Verify new providers work in your context
---
## Zero Mock/Fake Data Guarantee
**APL NEVER uses mock or fake data.**
- All validations are REAL API calls
- All response times are ACTUAL measurements
- All status classifications based on REAL responses
- Invalid providers are GENUINELY unreachable
- Valid providers are GENUINELY functional
This guarantee ensures:
- Production-ready validation results
- Accurate performance metrics
- Trustworthy provider recommendations
- No surprises in production
---
## Support
### Documentation
- `PROVIDER_AUTO_DISCOVERY_REPORT.md` - Latest validation results
- `APL_FINAL_SUMMARY.md` - Implementation summary
- This guide - Usage instructions
### Common Questions
**Q: Can I use APL in CI/CD?**
A: Yes! Run `python3 auto_provider_loader.py` in your pipeline.
**Q: How often should I run APL?**
A: Weekly for production, daily for development.
**Q: Can I add custom provider types?**
A: Yes, extend `ProviderValidator` class with new validation methods.
**Q: Does APL support GraphQL APIs?**
A: Not yet, but you can extend it by adding GraphQL validation logic.
---
## Version History
### v1.0 (2025-11-16)
- Initial release
- HTTP JSON validation
- HTTP RPC validation
- HF model validation (API-based, lightweight)
- Automatic discovery from JSON resources
- Comprehensive reporting
- Zero mock data guarantee
---
*Auto Provider Loader - Real Data Only, Always.*
|