djresttoolkit 0.2.0__tar.gz → 0.3.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: djresttoolkit
3
- Version: 0.2.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
@@ -4,7 +4,7 @@
4
4
 
5
5
  [project]
6
6
  name = "djresttoolkit"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "A collection of Django and DRF utilities to simplify API development."
9
9
  readme = { file = "README.md", content-type = "text/markdown" }
10
10
  license = { file = "LICENSE" }
@@ -0,0 +1,3 @@
1
+ from ._exceptions import exception_handler
2
+
3
+ __all__ = ["exception_handler"]
@@ -0,0 +1,3 @@
1
+ from ._exception_handler import exception_handler
2
+
3
+ __all__ = ["exception_handler"]
@@ -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