django-esi 8.1.0__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.
- django_esi-8.1.0.dist-info/METADATA +93 -0
- django_esi-8.1.0.dist-info/RECORD +100 -0
- django_esi-8.1.0.dist-info/WHEEL +4 -0
- django_esi-8.1.0.dist-info/licenses/LICENSE +674 -0
- esi/__init__.py +7 -0
- esi/admin.py +42 -0
- esi/aiopenapi3/client.py +79 -0
- esi/aiopenapi3/plugins.py +224 -0
- esi/app_settings.py +112 -0
- esi/apps.py +11 -0
- esi/checks.py +56 -0
- esi/clients.py +657 -0
- esi/decorators.py +271 -0
- esi/errors.py +22 -0
- esi/exceptions.py +51 -0
- esi/helpers.py +63 -0
- esi/locale/cs_CZ/LC_MESSAGES/django.mo +0 -0
- esi/locale/cs_CZ/LC_MESSAGES/django.po +53 -0
- esi/locale/de/LC_MESSAGES/django.mo +0 -0
- esi/locale/de/LC_MESSAGES/django.po +58 -0
- esi/locale/en/LC_MESSAGES/django.mo +0 -0
- esi/locale/en/LC_MESSAGES/django.po +54 -0
- esi/locale/es/LC_MESSAGES/django.mo +0 -0
- esi/locale/es/LC_MESSAGES/django.po +59 -0
- esi/locale/fr_FR/LC_MESSAGES/django.mo +0 -0
- esi/locale/fr_FR/LC_MESSAGES/django.po +59 -0
- esi/locale/it_IT/LC_MESSAGES/django.mo +0 -0
- esi/locale/it_IT/LC_MESSAGES/django.po +59 -0
- esi/locale/ja/LC_MESSAGES/django.mo +0 -0
- esi/locale/ja/LC_MESSAGES/django.po +58 -0
- esi/locale/ko_KR/LC_MESSAGES/django.mo +0 -0
- esi/locale/ko_KR/LC_MESSAGES/django.po +58 -0
- esi/locale/nl_NL/LC_MESSAGES/django.mo +0 -0
- esi/locale/nl_NL/LC_MESSAGES/django.po +53 -0
- esi/locale/pl_PL/LC_MESSAGES/django.mo +0 -0
- esi/locale/pl_PL/LC_MESSAGES/django.po +53 -0
- esi/locale/ru/LC_MESSAGES/django.mo +0 -0
- esi/locale/ru/LC_MESSAGES/django.po +61 -0
- esi/locale/sk/LC_MESSAGES/django.mo +0 -0
- esi/locale/sk/LC_MESSAGES/django.po +55 -0
- esi/locale/uk/LC_MESSAGES/django.mo +0 -0
- esi/locale/uk/LC_MESSAGES/django.po +57 -0
- esi/locale/zh_Hans/LC_MESSAGES/django.mo +0 -0
- esi/locale/zh_Hans/LC_MESSAGES/django.po +58 -0
- esi/management/commands/__init__.py +0 -0
- esi/management/commands/esi_clear_spec_cache.py +21 -0
- esi/management/commands/generate_esi_stubs.py +661 -0
- esi/management/commands/migrate_to_ssov2.py +188 -0
- esi/managers.py +303 -0
- esi/managers.pyi +85 -0
- esi/migrations/0001_initial.py +55 -0
- esi/migrations/0002_scopes_20161208.py +56 -0
- esi/migrations/0003_hide_tokens_from_admin_site.py +23 -0
- esi/migrations/0004_remove_unique_access_token.py +18 -0
- esi/migrations/0005_remove_token_length_limit.py +23 -0
- esi/migrations/0006_remove_url_length_limit.py +18 -0
- esi/migrations/0007_fix_mysql_8_migration.py +18 -0
- esi/migrations/0008_nullable_refresh_token.py +18 -0
- esi/migrations/0009_set_old_tokens_to_sso_v1.py +18 -0
- esi/migrations/0010_set_new_tokens_to_sso_v2.py +18 -0
- esi/migrations/0011_add_token_indices.py +28 -0
- esi/migrations/0012_fix_token_type_choices.py +18 -0
- esi/migrations/0013_squashed_0012_fix_token_type_choices.py +57 -0
- esi/migrations/__init__.py +0 -0
- esi/models.py +349 -0
- esi/openapi_clients.py +1225 -0
- esi/rate_limiting.py +107 -0
- esi/signals.py +21 -0
- esi/static/esi/img/EVE_SSO_Login_Buttons_Large_Black.png +0 -0
- esi/static/esi/img/EVE_SSO_Login_Buttons_Large_White.png +0 -0
- esi/static/esi/img/EVE_SSO_Login_Buttons_Small_Black.png +0 -0
- esi/static/esi/img/EVE_SSO_Login_Buttons_Small_White.png +0 -0
- esi/stubs.py +2 -0
- esi/stubs.pyi +6807 -0
- esi/tasks.py +78 -0
- esi/templates/esi/select_token.html +116 -0
- esi/templatetags/__init__.py +0 -0
- esi/templatetags/scope_tags.py +8 -0
- esi/tests/__init__.py +134 -0
- esi/tests/client_authed_pilot.py +63 -0
- esi/tests/client_public_pilot.py +53 -0
- esi/tests/factories.py +47 -0
- esi/tests/factories_2.py +60 -0
- esi/tests/jwt_factory.py +135 -0
- esi/tests/test_checks.py +48 -0
- esi/tests/test_clients.py +1019 -0
- esi/tests/test_decorators.py +578 -0
- esi/tests/test_management_command.py +307 -0
- esi/tests/test_managers.py +673 -0
- esi/tests/test_models.py +403 -0
- esi/tests/test_openapi.json +854 -0
- esi/tests/test_openapi.py +1017 -0
- esi/tests/test_swagger.json +489 -0
- esi/tests/test_swagger_full.json +51112 -0
- esi/tests/test_tasks.py +116 -0
- esi/tests/test_templatetags.py +22 -0
- esi/tests/test_views.py +331 -0
- esi/tests/threading_pilot.py +69 -0
- esi/urls.py +9 -0
- esi/views.py +129 -0
esi/rate_limiting.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import time
|
|
3
|
+
from django.core.cache import cache
|
|
4
|
+
from esi.exceptions import ESIBucketLimitException
|
|
5
|
+
|
|
6
|
+
logger = logging.getLogger(__name__)
|
|
7
|
+
|
|
8
|
+
seconds_per_unit = {"s": 1, "m": 60, "h": 3600, "d": 86400}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def interval_to_seconds(s):
|
|
12
|
+
return int(s[:-1]) * seconds_per_unit[s[-1]]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ESIRateLimitBucket:
|
|
16
|
+
MARKET_DATA_HISTORY = ("market_data_history", 300, 60)
|
|
17
|
+
CHARACTER_CORPORATION_HISTORY = ("character_corporation_history", 300, 60)
|
|
18
|
+
|
|
19
|
+
def __init__(self, slug, limit, window):
|
|
20
|
+
self.slug = slug
|
|
21
|
+
self.limit = limit
|
|
22
|
+
self.window = window
|
|
23
|
+
|
|
24
|
+
@classmethod
|
|
25
|
+
def choices(cls):
|
|
26
|
+
return [(bucket.slug, bucket.slug.replace("_", " ").title()) for bucket in cls]
|
|
27
|
+
|
|
28
|
+
def __str__(self):
|
|
29
|
+
return f"Rate Limit: {self.slug} - {self.limit} in {self.window}Seconds"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ESIRateLimiter:
|
|
33
|
+
def __init__(self) -> None:
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
def _slug_to_key(self, slug) -> str:
|
|
37
|
+
return f"esi:bucket:{slug}"
|
|
38
|
+
|
|
39
|
+
def init_bucket(self, bucket: ESIRateLimitBucket) -> None:
|
|
40
|
+
# Set our bucket up if it doesn't already exist
|
|
41
|
+
cache.set(
|
|
42
|
+
self._slug_to_key(bucket.slug),
|
|
43
|
+
bucket.limit,
|
|
44
|
+
timeout=bucket.window,
|
|
45
|
+
nx=True # Don't re-create if it does exist
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
def get_bucket(self, bucket: ESIRateLimitBucket) -> int:
|
|
49
|
+
# get the value from the bucket
|
|
50
|
+
return int(
|
|
51
|
+
cache.get(
|
|
52
|
+
self._slug_to_key(bucket.slug),
|
|
53
|
+
1 # When not found return 1
|
|
54
|
+
)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def get_timeout(self, bucket: ESIRateLimitBucket) -> int:
|
|
58
|
+
current_bucket = self.get_bucket(bucket)
|
|
59
|
+
if current_bucket <= 0:
|
|
60
|
+
timeout = cache.ttl(self._slug_to_key(bucket.slug)) + 1
|
|
61
|
+
msg = (
|
|
62
|
+
f"Rate limit for bucket '{bucket.slug}' exceeded: "
|
|
63
|
+
f"{current_bucket}/{bucket.limit} in last {bucket.window}s. "
|
|
64
|
+
f"Wait {timeout}s."
|
|
65
|
+
)
|
|
66
|
+
logger.warning(msg)
|
|
67
|
+
return timeout # return the time left till reset
|
|
68
|
+
else:
|
|
69
|
+
return 0 # we are good.
|
|
70
|
+
|
|
71
|
+
def decr_bucket(self, bucket: ESIRateLimitBucket, delta: int = 1) -> int:
|
|
72
|
+
# decrease the bucket value by <delta> from the bucket
|
|
73
|
+
return cache.decr(
|
|
74
|
+
self._slug_to_key(bucket.slug),
|
|
75
|
+
delta
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def set_bucket(self, bucket: ESIRateLimitBucket, new_limit: int = 1) -> int:
|
|
79
|
+
# decrease the bucket value by <delta> from the bucket
|
|
80
|
+
return cache.set(
|
|
81
|
+
self._slug_to_key(bucket.slug),
|
|
82
|
+
int(new_limit),
|
|
83
|
+
timeout=bucket.window
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
def check_bucket(self, bucket: ESIRateLimitBucket):
|
|
87
|
+
self.init_bucket(bucket)
|
|
88
|
+
# get the value
|
|
89
|
+
bucket_val = self.get_bucket(bucket)
|
|
90
|
+
if bucket_val <= 0:
|
|
91
|
+
timeout = self.get_timeout(bucket)
|
|
92
|
+
if timeout > 0:
|
|
93
|
+
raise ESIBucketLimitException(bucket, timeout)
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
def check_decr_bucket(self, bucket: ESIRateLimitBucket, raise_on_limit: bool = True):
|
|
97
|
+
try:
|
|
98
|
+
self.check_bucket(bucket)
|
|
99
|
+
self.decr_bucket(bucket)
|
|
100
|
+
except ESIBucketLimitException as ex:
|
|
101
|
+
if raise_on_limit:
|
|
102
|
+
raise ex
|
|
103
|
+
else:
|
|
104
|
+
time.sleep(ex.reset)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
ESIRateLimits = ESIRateLimiter()
|
esi/signals.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from django.dispatch import Signal
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
Basic signal sent after an ESI requests
|
|
5
|
+
from django.dispatch import receiver
|
|
6
|
+
from esi.signals import esi_request_statistics
|
|
7
|
+
|
|
8
|
+
@receiver(esi_request_statistics)
|
|
9
|
+
def esi_callback(sender, operation, status_code, headers, latency, bucket, **kwargs):
|
|
10
|
+
# do stuff
|
|
11
|
+
pass
|
|
12
|
+
"""
|
|
13
|
+
esi_request_statistics = Signal(
|
|
14
|
+
# providing_args=[
|
|
15
|
+
# "operation",
|
|
16
|
+
# "status_code",
|
|
17
|
+
# "headers",
|
|
18
|
+
# "latency",
|
|
19
|
+
# "bucket"
|
|
20
|
+
# ]
|
|
21
|
+
)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
esi/stubs.py
ADDED