generated from guilatrova/python-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pokeapi_throttle.py
154 lines (133 loc) · 3.63 KB
/
pokeapi_throttle.py
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
from __future__ import annotations
import asyncio
import time
import typing as t
from datetime import timedelta
from http import HTTPStatus
from gracy import (
BaseEndpoint,
GracefulRetry,
GracefulThrottle,
Gracy,
GracyConfig,
LogEvent,
LogLevel,
ThrottleRule,
graceful,
)
from rich import print
RETRY = GracefulRetry(
delay=0, # Force throttling to work
max_attempts=3,
retry_on=None,
log_after=LogEvent(LogLevel.WARNING),
log_exhausted=LogEvent(LogLevel.CRITICAL),
behavior="pass",
)
THROTTLE_RULE = ThrottleRule(r".*", 4, timedelta(seconds=2))
class PokeApiEndpoint(BaseEndpoint):
GET_POKEMON = "/pokemon/{NAME}"
GET_GENERATION = "/generation/{ID}"
class GracefulPokeAPI(Gracy[PokeApiEndpoint]):
class Config:
BASE_URL = "https://pokeapi.co/api/v2/"
SETTINGS = GracyConfig(
strict_status_code={HTTPStatus.OK},
retry=RETRY,
parser={
"default": lambda r: r.json(),
HTTPStatus.NOT_FOUND: None,
},
throttling=GracefulThrottle(
rules=THROTTLE_RULE,
log_limit_reached=LogEvent(LogLevel.ERROR),
log_wait_over=LogEvent(LogLevel.WARNING),
),
)
@graceful(
parser={"default": lambda r: r.json()["order"], HTTPStatus.NOT_FOUND: None}
)
async def get_pokemon(self, name: str):
val = t.cast(
t.Optional[str], await self.get(PokeApiEndpoint.GET_POKEMON, {"NAME": name})
)
if val:
print(f"{name} is #{val} in the pokedex")
else:
print(f"{name} was not found")
async def get_generation(self, gen: int):
return await self.get(PokeApiEndpoint.GET_GENERATION, {"ID": str(gen)})
pokeapi = GracefulPokeAPI()
async def main():
pokemon_names = [
"bulbasaur",
"charmander",
"squirtle",
"pikachu",
"jigglypuff",
"mewtwo",
"gyarados",
"dragonite",
"mew",
"chikorita",
"cyndaquil",
"totodile",
"pichu",
"togepi",
"ampharos",
"typhlosion",
"feraligatr",
"espeon",
"umbreon",
"lugia",
"ho-oh",
"treecko",
"torchic",
"mudkip",
"gardevoir",
"sceptile",
"blaziken",
"swampert",
"rayquaza",
"latias",
"latios",
"lucario",
"garchomp",
"darkrai",
"giratina", # (1) this fails, so good to test retry
"arceus",
"snivy",
"tepig",
"oshawott",
"zekrom",
"reshiram",
"victini",
"chespin",
"fennekin",
"froakie",
"xerneas",
"yveltal",
"zygarde", # (2) this fails, so good to test retry
"decidueye",
"incineroar",
]
# pokemon_names = pokemon_names[:10]
print(
f"Will query {len(pokemon_names)} pokemons concurrently - {str(THROTTLE_RULE)}"
)
try:
start = time.time()
pokemon_reqs = [
asyncio.create_task(pokeapi.get_pokemon(name)) for name in pokemon_names
]
gen_reqs = [
asyncio.create_task(pokeapi.get_generation(gen)) for gen in range(1, 4)
]
await asyncio.gather(*pokemon_reqs, *gen_reqs)
elapsed = time.time() - start
print(f"All requests took {timedelta(seconds=elapsed)}s to finish")
finally:
pokeapi.report_status("rich")
pokeapi.report_status("list")
pokeapi._throttle_controller.debug_print() # type: ignore
asyncio.run(main())