django-esi 7.0.1__py3-none-any.whl → 8.0.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of django-esi might be problematic. Click here for more details.
- {django_esi-7.0.1.dist-info → django_esi-8.0.0a1.dist-info}/METADATA +4 -2
- {django_esi-7.0.1.dist-info → django_esi-8.0.0a1.dist-info}/RECORD +25 -16
- esi/__init__.py +2 -1
- esi/admin.py +4 -3
- esi/aiopenapi3/plugins.py +78 -0
- esi/app_settings.py +14 -1
- esi/checks.py +1 -1
- esi/clients.py +13 -14
- esi/decorators.py +35 -3
- esi/exceptions.py +18 -0
- esi/helpers.py +25 -0
- esi/management/commands/generate_esi_stubs.py +211 -0
- esi/managers.py +18 -17
- esi/managers.pyi +82 -0
- esi/models.py +9 -16
- esi/openapi_clients.py +845 -0
- esi/rate_limiting.py +78 -0
- esi/stubs.py +2 -0
- esi/stubs.pyi +3913 -0
- esi/tasks.py +1 -2
- esi/tests/jwt_factory.py +3 -4
- esi/urls.py +0 -1
- esi/views.py +4 -3
- {django_esi-7.0.1.dist-info → django_esi-8.0.0a1.dist-info}/WHEEL +0 -0
- {django_esi-7.0.1.dist-info → django_esi-8.0.0a1.dist-info}/licenses/LICENSE +0 -0
esi/rate_limiting.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from django.core.cache import cache
|
|
3
|
+
from esi.exceptions import ESIBucketLimitException
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ESIRateLimitBucket:
|
|
7
|
+
MARKET_DATA_HISTORY = ("market_data_history", 300, 60)
|
|
8
|
+
CHARACTER_CORPORATION_HISTORY = ("character_corporation_history", 300, 60)
|
|
9
|
+
|
|
10
|
+
def __init__(self, slug, limit, window):
|
|
11
|
+
self.slug = slug
|
|
12
|
+
self.limit = limit
|
|
13
|
+
self.window = window
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def choices(cls):
|
|
17
|
+
return [(bucket.slug, bucket.slug.replace("_", " ").title()) for bucket in cls]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ESIRateLimiter:
|
|
21
|
+
def __init__(self) -> None:
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
def _slug_to_key(self, slug) -> str:
|
|
25
|
+
return f"esi:bucket:{slug}"
|
|
26
|
+
|
|
27
|
+
def init_bucket(self, bucket: ESIRateLimitBucket) -> None:
|
|
28
|
+
# Set our bucket up if it doesn't already exist
|
|
29
|
+
cache.set(
|
|
30
|
+
self._slug_to_key(bucket.slug),
|
|
31
|
+
bucket.limit,
|
|
32
|
+
timeout=bucket.window,
|
|
33
|
+
nx=True # Don't re-create if it does exist
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
def get_bucket(self, bucket: ESIRateLimitBucket) -> int:
|
|
37
|
+
# get the value from the bucket
|
|
38
|
+
return cache.get(
|
|
39
|
+
self._slug_to_key(bucket.slug),
|
|
40
|
+
1 # When not found return 1
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
def get_timeout(self, bucket: ESIRateLimitBucket, raise_on_limit: bool = True) -> int:
|
|
44
|
+
curent_bucket = self.get_bucket(bucket)
|
|
45
|
+
if curent_bucket <= 0:
|
|
46
|
+
timeout = cache.ttl(self._slug_to_key(bucket.slug)) + 1
|
|
47
|
+
msg = (
|
|
48
|
+
f"Rate limit for bucket '{bucket.slug}' exceeded: "
|
|
49
|
+
f"{curent_bucket}/{bucket.limit} in last {bucket.window}s. "
|
|
50
|
+
f"Wait {timeout}s."
|
|
51
|
+
)
|
|
52
|
+
if raise_on_limit:
|
|
53
|
+
raise ESIBucketLimitException(msg) # Throw error
|
|
54
|
+
else:
|
|
55
|
+
return timeout # return the time left till reset
|
|
56
|
+
else:
|
|
57
|
+
return 0 # we are good.
|
|
58
|
+
|
|
59
|
+
def decr_bucket(self, bucket: ESIRateLimitBucket) -> int:
|
|
60
|
+
# decrease the bucket value by 1 from the bucket
|
|
61
|
+
return cache.decr(
|
|
62
|
+
self._slug_to_key(bucket.slug)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
def check_bucket(self, bucket: ESIRateLimitBucket, raise_on_limit: bool = True):
|
|
66
|
+
ESIRateLimits.init_bucket(bucket)
|
|
67
|
+
# get the value
|
|
68
|
+
bucket_val = ESIRateLimits.get_bucket(bucket)
|
|
69
|
+
if bucket_val <= 0:
|
|
70
|
+
timeout = ESIRateLimits.get_timeout(bucket, raise_on_limit=raise_on_limit)
|
|
71
|
+
if timeout > 0:
|
|
72
|
+
time.sleep(timeout)
|
|
73
|
+
return
|
|
74
|
+
# reduce our bucket by 1
|
|
75
|
+
ESIRateLimits.decr_bucket(bucket)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
ESIRateLimits = ESIRateLimiter()
|
esi/stubs.py
ADDED