Spaces:
Configuration error
Configuration error
| #!/usr/bin/env python3 | |
| """Test script to generate and test API keys""" | |
| import requests | |
| import json | |
| BASE_URL = 'http://localhost:5000' | |
| print("=" * 60) | |
| print("Castor Price Forecasting API - Test Script") | |
| print("=" * 60) | |
| # Test 1: Health check | |
| print("\n1. Testing API Health...") | |
| try: | |
| response = requests.get(f'{BASE_URL}/api/health') | |
| print(f" Status: {response.status_code}") | |
| print(f" Response: {json.dumps(response.json(), indent=2)}") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| # Test 2: Generate API Key | |
| print("\n2. Generating API Key...") | |
| try: | |
| response = requests.post(f'{BASE_URL}/api/keys/generate', | |
| json={'name': 'castor-forecast-app'}) | |
| print(f" Status: {response.status_code}") | |
| result = response.json() | |
| print(f" Response: {json.dumps(result, indent=2)}") | |
| if result.get('status') == 'success': | |
| api_key = result.get('api_key') | |
| print(f"\n ✅ API Key Generated Successfully!") | |
| print(f" Your API Key: {api_key}") | |
| # Test 3: Use the API Key | |
| print("\n3. Testing ARIMA Forecast with API Key...") | |
| headers = {'X-API-Key': api_key} | |
| payload = { | |
| 'product': 'Castor', | |
| 'start_date': '2025-12-01', | |
| 'end_date': '2026-01-31' | |
| } | |
| response = requests.post(f'{BASE_URL}/api/forecast/arima', | |
| json=payload, headers=headers) | |
| print(f" Status: {response.status_code}") | |
| result = response.json() | |
| print(f" Response Status: {result.get('status')}") | |
| print(f" Product: {result.get('product')}") | |
| print(f" Model: {result.get('model')}") | |
| print(f" Forecast points: {len(result.get('forecast', {}))}") | |
| # Test 4: List all API keys | |
| print("\n4. Listing All API Keys...") | |
| response = requests.get(f'{BASE_URL}/api/keys/list') | |
| print(f" Status: {response.status_code}") | |
| result = response.json() | |
| print(f" Total Keys: {len(result.get('keys', []))}") | |
| for key_info in result.get('keys', []): | |
| print(f" - {key_info['name']} (Created: {key_info['created'][:10]})") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| print("\n" + "=" * 60) | |
| print("API Setup Complete!") | |
| print("=" * 60) | |
| print("\nUsage Example:") | |
| print('curl -X POST http://localhost:5000/api/forecast/arima \\') | |
| print(' -H "X-API-Key: YOUR_API_KEY" \\') | |
| print(' -H "Content-Type: application/json" \\') | |
| print(' -d \'{"product":"Castor","start_date":"2025-12-01","end_date":"2026-12-31"}\'') | |