Debian Codegraph @

examples/get-town-by-coords.py

raw
1 """Get AEMET OpenData town data by coords example."""
2
3 import asyncio
4 import timeit
5
6 from _common import json_dumps
7 from _secrets import AEMET_COORDS, AEMET_OPTIONS
8 import aiohttp
9
10 from aemet_opendata.exceptions import AuthError
11 from aemet_opendata.interface import AEMET
12
13
14 async def main():
15 """AEMET OpenData client example."""
16
17 async with aiohttp.ClientSession() as aiohttp_session:
18 client = AEMET(aiohttp_session, AEMET_OPTIONS)
19
20 try:
21 get_town_start = timeit.default_timer()
22 town = await client.get_town_by_coordinates(
23 AEMET_COORDS[0], AEMET_COORDS[1]
24 )
25 get_town_end = timeit.default_timer()
26 print(json_dumps(town))
27 print(f"Get Town time: {get_town_end - get_town_start}")
28 except AuthError:
29 print("API authentication error.")
30
31
32 if __name__ == "__main__":
33 asyncio.run(main())