File size: 1,346 Bytes
a9814b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import httpx
import asyncio

BASE_URL = "http://localhost:8000"
ACCESS_KEY = "test"


async def test_endpoints():
    async with httpx.AsyncClient(timeout=30.0) as client:
        # Test Current
        print("Testing /current...")
        res = await client.get(
            f"{BASE_URL}/current?access_key={ACCESS_KEY}&query=New York"
        )
        print(f"Status: {res.status_code}")
        if res.status_code == 200:
            print(f"Location: {res.json()['location']['name']}")
            print(f"Condition: {res.json()['current']['weather_descriptions'][0]}")

        # Test Forecast
        print("\nTesting /forecast...")
        res = await client.get(
            f"{BASE_URL}/forecast?access_key={ACCESS_KEY}&query=London&forecast_days=3"
        )
        print(f"Status: {res.status_code}")
        if res.status_code == 200:
            print(f"Forecast dates: {list(res.json()['forecast'].keys())}")

        # Test Autocomplete
        print("\nTesting /autocomplete...")
        res = await client.get(
            f"{BASE_URL}/autocomplete?access_key={ACCESS_KEY}&query=San Fran"
        )
        print(f"Status: {res.status_code}")
        if res.status_code == 200:
            print(f"Suggestions: {[loc['name'] for loc in res.json()['locations']]}")


if __name__ == "__main__":
    asyncio.run(test_endpoints())