djresttoolkit 0.2.0__py3-none-any.whl → 0.3.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.
- {djresttoolkit-0.2.0.dist-info → djresttoolkit-0.3.0.dist-info}/METADATA +1 -1
- {djresttoolkit-0.2.0.dist-info → djresttoolkit-0.3.0.dist-info}/RECORD +8 -5
- src/djresttoolkit/views/__init__.py +3 -0
- src/djresttoolkit/views/_exceptions/__init__.py +3 -0
- src/djresttoolkit/views/_exceptions/_exception_handler.py +64 -0
- {djresttoolkit-0.2.0.dist-info → djresttoolkit-0.3.0.dist-info}/WHEEL +0 -0
- {djresttoolkit-0.2.0.dist-info → djresttoolkit-0.3.0.dist-info}/entry_points.txt +0 -0
- {djresttoolkit-0.2.0.dist-info → djresttoolkit-0.3.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: djresttoolkit
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.3.0
|
4
4
|
Summary: A collection of Django and DRF utilities to simplify API development.
|
5
5
|
Project-URL: Homepage, https://github.com/shaileshpandit141/djresttoolkit
|
6
6
|
Project-URL: Documentation, https://shaileshpandit141.github.io/djresttoolkit
|
@@ -10,8 +10,11 @@ src/djresttoolkit/mail/_models.py,sha256=_41pH3xC0jP8SHSty2FkxvRh2_ddKk-4peT11OH
|
|
10
10
|
src/djresttoolkit/mail/_types.py,sha256=est1mrN80vB_4-j-2yuAr_l_7rNzO4AJlqpq74zO5ow,682
|
11
11
|
src/djresttoolkit/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
12
12
|
src/djresttoolkit/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
13
|
-
djresttoolkit
|
14
|
-
djresttoolkit
|
15
|
-
djresttoolkit
|
16
|
-
djresttoolkit-0.
|
17
|
-
djresttoolkit-0.
|
13
|
+
src/djresttoolkit/views/__init__.py,sha256=XrxBrs6sH4HmUzp41omcmy_y94pSaXAVn01ttQ022-4,76
|
14
|
+
src/djresttoolkit/views/_exceptions/__init__.py,sha256=DrCUxuPNyBR4WhzNutn5HDxLa--q51ykIxSG7_bFsOI,83
|
15
|
+
src/djresttoolkit/views/_exceptions/_exception_handler.py,sha256=lg6kfAABex-UbAsF78uX-M-ZnJ3u1vK1eIXytb-4KSw,2375
|
16
|
+
djresttoolkit-0.3.0.dist-info/METADATA,sha256=LKqn4ZSUVD-yo03t6klk-QHW1q_VwxpCJk6DfTSIDMo,4374
|
17
|
+
djresttoolkit-0.3.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
18
|
+
djresttoolkit-0.3.0.dist-info/entry_points.txt,sha256=YMhfTF-7mYppO8QqqWnvR_hyMWvoYxD6XI94_ViFu3k,60
|
19
|
+
djresttoolkit-0.3.0.dist-info/licenses/LICENSE,sha256=8-oZM3yuuTRjySMbVKX9YXYA7Y4M_KhQNBYXPFjeWUo,1074
|
20
|
+
djresttoolkit-0.3.0.dist-info/RECORD,,
|
@@ -0,0 +1,64 @@
|
|
1
|
+
from typing import Any, cast
|
2
|
+
|
3
|
+
from django.core.cache import cache
|
4
|
+
from django.utils import timezone
|
5
|
+
from rest_framework import status, views
|
6
|
+
from rest_framework.request import Request
|
7
|
+
from rest_framework.response import Response
|
8
|
+
from rest_framework.throttling import AnonRateThrottle
|
9
|
+
|
10
|
+
|
11
|
+
def exception_handler(exc: Exception, context: dict[str, Any]) -> Response | None:
|
12
|
+
"""
|
13
|
+
Custom exception handler that preserves DRF's default functionality
|
14
|
+
while adding custom throttling behavior.
|
15
|
+
"""
|
16
|
+
|
17
|
+
# Call DRF's default exception handler first
|
18
|
+
response: Response | None = views.exception_handler(exc, context)
|
19
|
+
|
20
|
+
request: Request | None = context.get("request")
|
21
|
+
view = context.get("view")
|
22
|
+
|
23
|
+
if request and view:
|
24
|
+
# Pick throttle classes from view or default to AnonRateThrottle
|
25
|
+
throttle_classes: list[type[AnonRateThrottle]] = getattr(
|
26
|
+
view, "throttle_classes", [AnonRateThrottle]
|
27
|
+
)
|
28
|
+
|
29
|
+
for throttle_class in throttle_classes:
|
30
|
+
throttle = throttle_class()
|
31
|
+
cache_key = throttle.get_cache_key(request, view)
|
32
|
+
if not cache_key:
|
33
|
+
continue
|
34
|
+
|
35
|
+
history: list[float] = cache.get(cache_key, [])
|
36
|
+
now = timezone.now().timestamp()
|
37
|
+
duration: float = cast(float, throttle.duration) # type: ignore[attr-defined]
|
38
|
+
|
39
|
+
# Keep only non-expired timestamps
|
40
|
+
history = [float(ts) for ts in history if now - float(ts) < duration]
|
41
|
+
|
42
|
+
# If throttle limit exceeded
|
43
|
+
if len(history) >= throttle.num_requests: # type: ignore[attr-defined]
|
44
|
+
retry_after: float = (
|
45
|
+
duration - (now - history[0]) if history else duration
|
46
|
+
)
|
47
|
+
|
48
|
+
return Response(
|
49
|
+
data={
|
50
|
+
"detail": "Too many requests. Please try again later.",
|
51
|
+
"retry_after": {
|
52
|
+
"time": round(retry_after, 2),
|
53
|
+
"unit": "seconds",
|
54
|
+
},
|
55
|
+
},
|
56
|
+
status=status.HTTP_429_TOO_MANY_REQUESTS,
|
57
|
+
)
|
58
|
+
|
59
|
+
# Otherwise add current timestamp
|
60
|
+
history.append(now)
|
61
|
+
cache.set(key=cache_key, value=history, timeout=duration)
|
62
|
+
|
63
|
+
# If DRF handled the exception, return that response
|
64
|
+
return response
|
File without changes
|
File without changes
|
File without changes
|