Spaces:
Sleeping
Sleeping
| """ | |
| API Key Tester for SkyGuardian AI | |
| ================================== | |
| Test your API keys before using the main application. | |
| Usage: | |
| python test_api.py | |
| """ | |
| import requests | |
| from datetime import datetime, timedelta | |
| def test_skyscanner_rapidapi(api_key): | |
| """Test Skyscanner API via RapidAPI""" | |
| print("\nπ Testing Skyscanner API (RapidAPI)...") | |
| try: | |
| headers = { | |
| "X-RapidAPI-Key": api_key, | |
| "X-RapidAPI-Host": "skyscanner-api.p.rapidapi.com" | |
| } | |
| url = "https://skyscanner-api.p.rapidapi.com/v3/flights/live/search/create" | |
| departure_date = datetime.now() + timedelta(days=30) | |
| payload = { | |
| "query": { | |
| "market": "SG", | |
| "locale": "en-GB", | |
| "currency": "USD", | |
| "queryLegs": [{ | |
| "originPlaceId": {"iata": "SIN"}, | |
| "destinationPlaceId": {"iata": "BKK"}, | |
| "date": { | |
| "year": departure_date.year, | |
| "month": departure_date.month, | |
| "day": departure_date.day | |
| } | |
| }], | |
| "adults": 1, | |
| "cabinClass": "CABIN_CLASS_ECONOMY" | |
| } | |
| } | |
| response = requests.post(url, json=payload, headers=headers, timeout=15) | |
| if response.status_code == 200: | |
| print("β SUCCESS: Skyscanner API is working!") | |
| data = response.json() | |
| if "content" in data: | |
| print(f" Response received: {len(str(data))} bytes") | |
| return True | |
| else: | |
| print("β οΈ API responded but data format unexpected") | |
| return False | |
| elif response.status_code == 401: | |
| print("β FAILED: Invalid API key") | |
| print(" Get your key at: https://rapidapi.com/skyscanner/api/skyscanner-api") | |
| return False | |
| elif response.status_code == 429: | |
| print("β οΈ RATE LIMIT: Too many requests") | |
| print(" Wait a few minutes or upgrade your plan") | |
| return False | |
| else: | |
| print(f"β FAILED: HTTP {response.status_code}") | |
| print(f" Response: {response.text[:200]}") | |
| return False | |
| except requests.exceptions.Timeout: | |
| print("β±οΈ TIMEOUT: Request took too long") | |
| return False | |
| except Exception as e: | |
| print(f"β ERROR: {str(e)}") | |
| return False | |
| def test_kiwi_api(api_key): | |
| """Test Kiwi.com Tequila API""" | |
| print("\nπ Testing Kiwi.com Tequila API...") | |
| try: | |
| headers = {"apikey": api_key} | |
| url = "https://api.tequila.kiwi.com/v2/search" | |
| departure_date = (datetime.now() + timedelta(days=30)).strftime("%d/%m/%Y") | |
| params = { | |
| "fly_from": "SIN", | |
| "fly_to": "BKK", | |
| "date_from": departure_date, | |
| "date_to": departure_date, | |
| "adults": 1, | |
| "curr": "USD", | |
| "limit": 1 | |
| } | |
| response = requests.get(url, headers=headers, params=params, timeout=15) | |
| if response.status_code == 200: | |
| print("β SUCCESS: Kiwi API is working!") | |
| data = response.json() | |
| if "data" in data: | |
| print(f" Flights found: {len(data['data'])}") | |
| return True | |
| else: | |
| print("β οΈ API responded but no flight data") | |
| return False | |
| elif response.status_code == 401: | |
| print("β FAILED: Invalid API key") | |
| print(" Get your key at: https://tequila.kiwi.com") | |
| return False | |
| elif response.status_code == 429: | |
| print("β οΈ RATE LIMIT: Too many requests") | |
| return False | |
| else: | |
| print(f"β FAILED: HTTP {response.status_code}") | |
| print(f" Response: {response.text[:200]}") | |
| return False | |
| except Exception as e: | |
| print(f"β ERROR: {str(e)}") | |
| return False | |
| def test_aviationstack_api(api_key): | |
| """Test AviationStack API""" | |
| print("\nπ Testing AviationStack API...") | |
| try: | |
| url = "http://api.aviationstack.com/v1/routes" | |
| params = { | |
| "access_key": api_key, | |
| "dep_iata": "SIN", | |
| "arr_iata": "BKK" | |
| } | |
| response = requests.get(url, params=params, timeout=15) | |
| if response.status_code == 200: | |
| data = response.json() | |
| if data.get("data"): | |
| print("β SUCCESS: AviationStack API is working!") | |
| print(f" Routes found: {len(data['data'])}") | |
| print(" β οΈ Note: AviationStack provides route data, not real-time pricing") | |
| return True | |
| else: | |
| print("β οΈ API responded but no route data") | |
| return False | |
| elif response.status_code == 401: | |
| print("β FAILED: Invalid API key") | |
| print(" Get your key at: https://aviationstack.com") | |
| return False | |
| else: | |
| print(f"β FAILED: HTTP {response.status_code}") | |
| return False | |
| except Exception as e: | |
| print(f"β ERROR: {str(e)}") | |
| return False | |
| def main(): | |
| """Main test function""" | |
| print("=" * 60) | |
| print("SkyGuardian AI - API Key Tester") | |
| print("=" * 60) | |
| print("\nThis tool helps verify your API keys work correctly.") | |
| print("\nWhich API would you like to test?") | |
| print("1. Skyscanner (RapidAPI) - Recommended") | |
| print("2. Kiwi.com Tequila API") | |
| print("3. AviationStack API") | |
| print("4. Test All") | |
| choice = input("\nEnter choice (1-4): ").strip() | |
| results = {} | |
| if choice in ["1", "4"]: | |
| api_key = input("\nEnter your RapidAPI key (for Skyscanner): ").strip() | |
| if api_key: | |
| results["Skyscanner"] = test_skyscanner_rapidapi(api_key) | |
| if choice in ["2", "4"]: | |
| api_key = input("\nEnter your Kiwi API key: ").strip() | |
| if api_key: | |
| results["Kiwi"] = test_kiwi_api(api_key) | |
| if choice in ["3", "4"]: | |
| api_key = input("\nEnter your AviationStack API key: ").strip() | |
| if api_key: | |
| results["AviationStack"] = test_aviationstack_api(api_key) | |
| # Summary | |
| print("\n" + "=" * 60) | |
| print("TEST SUMMARY") | |
| print("=" * 60) | |
| if results: | |
| for provider, success in results.items(): | |
| status = "β PASSED" if success else "β FAILED" | |
| print(f"{provider}: {status}") | |
| if any(results.values()): | |
| print("\nπ You're ready to use SkyGuardian AI!") | |
| print(" Run: python app.py") | |
| else: | |
| print("\nβ οΈ No APIs passed. Check your keys and try again.") | |
| else: | |
| print("No tests run.") | |
| print("=" * 60) | |
| if __name__ == "__main__": | |
| main() | |