django-api-versioning 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  from functools import wraps
2
2
  from typing import Callable, Optional, List
3
+ from django.views import View
3
4
  from .settings import api_settings as settings
4
5
  from .registry import registry
5
6
  from .exceptions import InvalidVersionError, VersionRangeError, VersionTypeError
@@ -41,8 +42,13 @@ def endpoint(
41
42
  """
42
43
 
43
44
  def decorator(func: Callable) -> Callable:
45
+
44
46
  @wraps(func)
45
47
  def view(*args, **kwargs):
48
+ # Check if the view is a class-based view (CBV)
49
+ if isinstance(func, type) and issubclass(func, View):
50
+ # For class-based views, ensure it's called as a method
51
+ return func.as_view()(*args, **kwargs)
46
52
  return func(*args, **kwargs)
47
53
 
48
54
  # Read API versioning settings
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django-api-versioning
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: Django API versioning decorator provides a solution for managing multiple API versions within the Django framework, enabling versioning through URLs with backward compatibility and automatically registering routes.
5
5
  Home-page: https://github.com/mojtaba-arvin/django-api-versioning
6
6
  Author: Mojtaba Arvin
@@ -15,7 +15,7 @@ Classifier: Development Status :: 3 - Alpha
15
15
  Classifier: Intended Audience :: Developers
16
16
  Classifier: License :: OSI Approved :: MIT License
17
17
  Classifier: Operating System :: OS Independent
18
- Requires-Python: >=3.9
18
+ Requires-Python: >=3.6
19
19
  Description-Content-Type: text/markdown
20
20
  License-File: LICENSE
21
21
  Requires-Dist: Django>=3.2
@@ -1,17 +1,17 @@
1
1
  django_api_versioning/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- django_api_versioning/decorators.py,sha256=YHyIapxlaQ3IgZnkdjjJ7QXaZxsMFjq-2TY12a5pXAA,3888
2
+ django_api_versioning/decorators.py,sha256=5xWC5gIeDvkaH9UqmLv5eL0FxQ856451_QLMMi_7ex8,4180
3
3
  django_api_versioning/exceptions.py,sha256=MgCpaNBsD8laQoVIVK823_t1liQ82K_uqguuA60PxXQ,485
4
4
  django_api_versioning/registry.py,sha256=FCRTHGyl995U1kLlxtIBp3lb62v-6AbSK9k3orzZxWA,1140
5
5
  django_api_versioning/settings.py,sha256=8p57CIEQR65luxU7xo-eYdH-lSGGk3-BQFMVRRFG2pY,2975
6
6
  django_api_versioning/urls.py,sha256=B8UBYSXdyq_xTpWeN5zzH2SQzKiqZQMx3WFBrsu93X0,144
7
7
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- tests/test_decorators.py,sha256=G2G3PR_QAPNyJuaw7H9BMARSOK8C6GJlHRAL3KmTuCY,3587
8
+ tests/test_decorators.py,sha256=qtscdA3Sz7eZBwtjSXKBkfOZBcRk2ckm8i1E5t-ihUs,5104
9
9
  tests/test_exceptions.py,sha256=fsyfA7ouIMqz6Emexqo_oB7oVEZlhqptNmwH8KX37s8,863
10
10
  tests/test_registry.py,sha256=KQT6yWkfKZmcLShFkidHwfJfPi8yHN5i9xuzC3vqDso,2634
11
11
  tests/test_settings.py,sha256=SjieOTnwHdU0E6A2-qLE3iXG5osY3qacbAzJJ7tqD-0,5213
12
12
  tests/test_urls.py,sha256=DA8DbIEAYX812Re2PlTkL1OpkwCO2IZ1vW_QMh2d9nI,1207
13
- django_api_versioning-0.1.1.dist-info/LICENSE,sha256=iDPJdze6sBlBBSoB-BIyT2iHfHDGUAaZG3nTFd6m2FQ,1070
14
- django_api_versioning-0.1.1.dist-info/METADATA,sha256=vJvAL-_cghuGe-GbXQ1KyqjGE-xcfGcpifb-gRi5jRg,10679
15
- django_api_versioning-0.1.1.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
16
- django_api_versioning-0.1.1.dist-info/top_level.txt,sha256=F4n1zaE6P--9OytuMrvCD50vn7NvIVWkIl6ie9fsFck,28
17
- django_api_versioning-0.1.1.dist-info/RECORD,,
13
+ django_api_versioning-0.1.3.dist-info/LICENSE,sha256=iDPJdze6sBlBBSoB-BIyT2iHfHDGUAaZG3nTFd6m2FQ,1070
14
+ django_api_versioning-0.1.3.dist-info/METADATA,sha256=qtUKqfKFPH_YbzkBDfydKKnUtCU31gOISnAHX5VtyCY,10679
15
+ django_api_versioning-0.1.3.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
16
+ django_api_versioning-0.1.3.dist-info/top_level.txt,sha256=F4n1zaE6P--9OytuMrvCD50vn7NvIVWkIl6ie9fsFck,28
17
+ django_api_versioning-0.1.3.dist-info/RECORD,,
tests/test_decorators.py CHANGED
@@ -1,10 +1,13 @@
1
1
  import pytest
2
2
  from unittest.mock import patch
3
+ from django.http import JsonResponse
4
+ from django.views import View
3
5
  from django_api_versioning.settings import api_settings as settings
4
6
  from django_api_versioning.registry import registry
5
7
  from django_api_versioning.decorators import endpoint
6
8
  from django_api_versioning.exceptions import InvalidVersionError, VersionTypeError, VersionRangeError
7
9
 
10
+
8
11
  @pytest.fixture(autouse=True)
9
12
  def clear_registered_routes():
10
13
  """Clear the registry before each test to ensure isolation."""
@@ -86,3 +89,34 @@ def test_missing_api_version_settings():
86
89
  @endpoint("users", version=2)
87
90
  def test_view():
88
91
  pass
92
+
93
+ def test_class_based_view(mock_settings):
94
+ # Create a class-based view and decorate it with the `endpoint` decorator
95
+ @endpoint("users", version=2)
96
+ class UsersView(View):
97
+ def get(self, request):
98
+ return JsonResponse({"message": "API Version 2 Users"})
99
+
100
+ # Register the view and check if the route is correctly registered
101
+ registered_routes = [str(p.pattern) for p in registry.urlpatterns]
102
+ assert "api/v2/users" in registered_routes, f"Route for version 2 is missing: {registered_routes}"
103
+
104
+ def test_class_based_view_with_invalid_version(mock_settings):
105
+ # Test invalid version for class-based view
106
+ with pytest.raises(InvalidVersionError):
107
+ @endpoint("users", version=4)
108
+ class UsersView(View):
109
+ def get(self, request):
110
+ return JsonResponse({"message": "API Version 4 Users"})
111
+
112
+ def test_class_based_view_with_backward_compatibility(mock_settings):
113
+ # Test class-based view with backward compatibility
114
+ @endpoint("users", version=3)
115
+ class UsersView(View):
116
+ def get(self, request):
117
+ return JsonResponse({"message": "API Version 3 Users"})
118
+
119
+ registered_routes = [str(p.pattern) for p in registry.urlpatterns]
120
+ # Assert that versions 1, 2, and 3 are registered for backward compatibility
121
+ for version in range(1, 4):
122
+ assert f"api/v{version}/users" in registered_routes, f"Missing route for v{version}: {registered_routes}"