-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathasync_usage.py
More file actions
39 lines (29 loc) · 1.11 KB
/
async_usage.py
File metadata and controls
39 lines (29 loc) · 1.11 KB
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
"""Async usage of cocapi.
Demonstrates:
- Async context manager (async with)
- Awaiting API calls
- Concurrent requests with asyncio.gather
"""
import asyncio
from cocapi import CocApi
async def main() -> None:
# Use async with — this enables async mode and manages the HTTP client
async with CocApi("YOUR_API_TOKEN") as api:
# Single request
clan = await api.clan_tag("#2PP")
print(f"Clan: {clan['name']}")
# Multiple requests concurrently
player_tags = ["#900PUCPV", "#J2CP8U0", "#L2VVRU0"]
tasks = [api.players(tag) for tag in player_tags]
players = await asyncio.gather(*tasks)
print("\nPlayers:")
for p in players:
if p.get("result") != "error":
print(f" {p['name']} - TH{p['townHallLevel']}, {p['trophies']} trophies")
# War log
log = await api.clan_war_log("#2PP")
for entry in log.get("items", [])[:3]:
result = entry.get("result", "?")
opponent = entry.get("opponent", {}).get("name", "?")
print(f"\nWar vs {opponent}: {result}")
asyncio.run(main())