File size: 7,089 Bytes
75a1104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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()