django-api-versioning 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,10 +20,16 @@ def endpoint(
20
20
  - Ensures that no version lower than `API_MIN_VERSION` is registered.
21
21
 
22
22
  Args:
23
- postfix (str): The endpoint suffix (e.g., "users" → "api/v1/users").
24
- version (Optional[int]): The version of the API. Defaults to None (unversioned).
25
- backward (bool): If True, registers routes for all versions from `API_MIN_VERSION` up to the current version, which is less than or equal to `API_MAX_VERSION`. Defaults to True.
26
- app_name (Optional[str]): The app name to be prefixed to the route.
23
+ postfix (str): The endpoint suffix (e.g., "users" →
24
+ "api/v1/users").
25
+ version (Optional[int]): The version of the API. Defaults
26
+ to None (unversioned).
27
+ backward (bool): If True, registers routes for all versions
28
+ from `API_MIN_VERSION` up to the current
29
+ version, which is less than or equal to
30
+ `API_MAX_VERSION`. Defaults to True.
31
+ app_name (Optional[str]): The app name to be prefixed to
32
+ the route.
27
33
  view_name (Optional[str]): The custom view name for Django.
28
34
 
29
35
  Returns:
@@ -2,7 +2,7 @@ import logging
2
2
  from django.conf import settings
3
3
  from django.core.exceptions import ImproperlyConfigured
4
4
  from dataclasses import dataclass
5
- from typing import Optional
5
+ from typing import Optional, Any
6
6
  from .exceptions import VersionTypeError, VersionRangeError, VersioningError
7
7
 
8
8
  # Initialize logger
@@ -10,14 +10,14 @@ logger = logging.getLogger(__name__)
10
10
 
11
11
  @dataclass
12
12
  class APISettings:
13
-
13
+
14
14
  API_BASE_PATH: str = "api/v{version}/"
15
15
  API_MAX_VERSION: int = 1
16
16
  API_MIN_VERSION: int = 1
17
17
  ROOT_URLCONF: str = 'django_api_versioning.urls'
18
18
 
19
19
  @staticmethod
20
- def get_setting(name: str, default: Optional[any] = None) -> Optional[any]:
20
+ def get_setting(name: str, default: Optional[Any] = None) -> Any:
21
21
  """
22
22
  Reads the setting from Django settings and provides a default if not found.
23
23
  """
@@ -30,9 +30,7 @@ class APISettings:
30
30
 
31
31
  # Ensure that API_BASE_PATH ends with a "/"
32
32
  if not self.API_BASE_PATH.endswith("/"):
33
- logger.warning(
34
- "API_BASE_PATH should end with a '/'. Adding '/' automatically."
35
- )
33
+ logger.warning("API_BASE_PATH should end with a '/'. Adding '/' automatically.")
36
34
  self.API_BASE_PATH += "/"
37
35
 
38
36
  # Validate version settings
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django-api-versioning
3
- Version: 0.1.0
4
- Summary: A powerful and flexible library for managing API versioning in Django projects.
3
+ Version: 0.1.2
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
7
7
  Author-email: ArvinDevDay@gmail.com
@@ -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
@@ -31,7 +31,6 @@ Requires-Dist: pre-commit>=2.0.0; extra == "dev"
31
31
  Requires-Dist: twine>=4.0.0; extra == "dev"
32
32
  Requires-Dist: build>=0.8.0; extra == "dev"
33
33
 
34
-
35
34
  # Django API Versioning
36
35
 
37
36
  [![PyPI version](https://badge.fury.io/py/django-api-versioning.svg)](https://badge.fury.io/py/django-api-versioning)
@@ -39,7 +38,7 @@ Requires-Dist: build>=0.8.0; extra == "dev"
39
38
  [![codecov](https://codecov.io/gh/mojtaba-arvin/django-api-versioning/branch/main/graph/badge.svg)](https://codecov.io/gh/mojtaba-arvin/django-api-versioning)
40
39
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
41
40
 
42
- **Django API Versioning** is a powerful and flexible library for managing API versioning in Django projects. It allows you to easily define and manage different versions of your API endpoints using decorators, ensuring backward compatibility and clean code organization.
41
+ **Django API Versioning** is a powerful and flexible library for managing [API versioning in Django](https://github.com/mojtaba-arvin/django-api-versioning) projects. It allows you to easily define and manage different versions of your API endpoints using decorators, ensuring backward compatibility and clean code organization.
43
42
 
44
43
  ## Features
45
44
 
@@ -52,7 +51,7 @@ Requires-Dist: build>=0.8.0; extra == "dev"
52
51
 
53
52
  ## Installation
54
53
 
55
- You can install Django API Versioning via pip:
54
+ You can [install Django API Versioning](https://pypi.org/project/django-api-versioning/) via pip:
56
55
 
57
56
  ```bash
58
57
  pip install django-api-versioning
@@ -106,7 +105,6 @@ or you have already have a `ROOT_URLCONF` in settings, you only need to import t
106
105
 
107
106
  The `endpoint` decorator can be used in both function-based views (FBVs) and class-based views (CBVs). It's also fully compatible with `Django Rest Framework (DRF)`. The decorator allows you to define versioning for your API views and supports backward compatibility by default and you don't need to pass `backward=True` flag to the `endpoint` decorator.
108
107
 
109
-
110
108
  #### Example for Function-Based Views (FBVs):
111
109
 
112
110
  ```python
@@ -121,6 +119,7 @@ def users_view(request):
121
119
  In this example, the `users_view` function is decorated with the endpoint decorator. This specifies that the view is accessible under version `2` of the API and **supports backward compatibility**. The `backward=True` flag as default ensures that users can also access the previous version (version `1`) at `/api/v1/account_app/users`.
122
120
 
123
121
  #### Example for Class-Based Views (CBVs):
122
+
124
123
  For class-based views, you can apply the decorator to methods such as `get`, `post`, or any other HTTP method you need to handle. Here’s an example:
125
124
 
126
125
  ```python
@@ -141,7 +140,6 @@ class UsersView(View):
141
140
 
142
141
  If you have already installed [Django Rest Framework](https://www.django-rest-framework.org/#installation), the `endpoint` decorator can be easily applied to APIView or viewsets. Here’s an example with a DRF APIView:
143
142
 
144
-
145
143
  ```python
146
144
  from rest_framework.views import APIView
147
145
  from rest_framework.response import Response
@@ -155,25 +153,26 @@ class UsersAPIView(APIView):
155
153
  ```
156
154
 
157
155
  #### URL Generation Based on Versioning:
156
+
158
157
  Once the decorator is applied, the URLs for your API will be generated based on the version specified in the decorator. For example, if the `API_MIN_VERSION` in your settings.py is set to `1` and the version in the decorator is set to `2`, the following URLs will be available:
159
158
 
160
- * `/api/v1/account_app/users`
161
- * `/api/v2/account_app/users`
159
+ - `/api/v1/account_app/users`
160
+ - `/api/v2/account_app/users`
162
161
 
163
162
  The `API_MIN_VERSION` setting ensures that users can access the API using different versions, providing backward compatibility. You can adjust which versions are considered valid by modifying the `API_MIN_VERSION` and `version` numbers in the decorators.
164
163
 
165
164
  #### Additional Configuration Options:
166
165
 
167
166
  **Without `app_name`:** If you don't pass `app_name` in the decorator, like this:
167
+
168
168
  ```python
169
169
  @endpoint("users", version=2, view_name="users_list_api")
170
170
  ```
171
171
 
172
172
  The generated URLs will be:
173
173
 
174
- * `/api/v1/users`
175
- * `/api/v2/users`
176
-
174
+ - `/api/v1/users`
175
+ - `/api/v2/users`
177
176
 
178
177
  **Without `version`:** If you don't pass `version` in the decorator, like this:
179
178
 
@@ -183,7 +182,7 @@ The generated URLs will be:
183
182
 
184
183
  API versioning will be disabled (`API_BASE_PATH` as prefix will be removed) for that view. The only URL generated will be:
185
184
 
186
- * `/users`
185
+ - `/users`
187
186
 
188
187
  **Setting `backward=False`:** By default, the `backward` parameter is set to `True`, which ensures backward compatibility. If you explicitly set `backward=False`, like this:
189
188
 
@@ -193,7 +192,7 @@ API versioning will be disabled (`API_BASE_PATH` as prefix will be removed) for
193
192
 
194
193
  The generated URL will be only version 2:
195
194
 
196
- * `api/v2/users`
195
+ - `api/v2/users`
197
196
 
198
197
  4. Run the Server:
199
198
 
@@ -202,16 +201,21 @@ python manage.py runserver
202
201
  ```
203
202
 
204
203
  ## Notes
204
+
205
205
  ### 1. `API_BASE_PATH` in settings Must Include ‍‍`{version}`:
206
+
206
207
  The `API_BASE_PATH` should always include `{version}` to ensure proper API versioning. This is important for correctly mapping API routes to different versions.
207
208
 
208
209
  ### 2. Using `app_name` in the `endpoint` decorator:
210
+
209
211
  It's recommended to fill in the `app_name` in the `endpoint` decorator to make the API URLs **more unique and organized**. This ensures that the routes are scoped under the correct app, avoiding potential conflicts and making them easier to manage.
210
212
 
211
213
  ### 3. Views with Version Less Than `API_MIN_VERSION` Are Automatically Ignored:
214
+
212
215
  Any view whose `version` is less than the `API_MIN_VERSION` will be automatically ignored. This means clients will no longer have access to these older versions, **without the need to manually edit or remove code**. This is handled automatically by the package.
213
216
 
214
217
  ### 4. URLs for Versions Between `API_MIN_VERSION` <= `version` <= `API_MAX_VERSION`:
218
+
215
219
  Endpoints that have versions within the range defined by `API_MIN_VERSION` <= `version` <= `API_MAX_VERSION` will always have a corresponding URL generated. This ensures that only valid versions will be accessible, providing flexibility in version management.
216
220
 
217
221
  ### `endpoint` Decorator Function Definition
@@ -232,7 +236,7 @@ def endpoint(
232
236
  - Uses `API_MIN_VERSION` and `API_MAX_VERSION` from Django settings.
233
237
  - Supports backward compatibility by registering multiple versions if needed.
234
238
  - Ensures that no version lower than `API_MIN_VERSION` is registered.
235
-
239
+
236
240
  Args:
237
241
  postfix (str): The endpoint suffix (e.g., "users" → "api/v1/users").
238
242
  version (Optional[int]): The version of the API. Defaults to None (unversioned).
@@ -249,7 +253,6 @@ def endpoint(
249
253
  """
250
254
  ```
251
255
 
252
-
253
256
  ## Contributing
254
257
 
255
258
  Feel free to open an issue or submit a pull request with any improvements or bug fixes. We appreciate contributions to enhance this package!
@@ -1,8 +1,8 @@
1
1
  django_api_versioning/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- django_api_versioning/decorators.py,sha256=BC_7ZQwCDq3BjPdcNw-Efdh09kHv3uDjZEW1nGgcNog,3732
2
+ django_api_versioning/decorators.py,sha256=YHyIapxlaQ3IgZnkdjjJ7QXaZxsMFjq-2TY12a5pXAA,3888
3
3
  django_api_versioning/exceptions.py,sha256=MgCpaNBsD8laQoVIVK823_t1liQ82K_uqguuA60PxXQ,485
4
4
  django_api_versioning/registry.py,sha256=FCRTHGyl995U1kLlxtIBp3lb62v-6AbSK9k3orzZxWA,1140
5
- django_api_versioning/settings.py,sha256=rasiYFuKM0EUYqzrpIphHGPEYb209uz56pEKR59R8w4,3006
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
8
  tests/test_decorators.py,sha256=G2G3PR_QAPNyJuaw7H9BMARSOK8C6GJlHRAL3KmTuCY,3587
@@ -10,8 +10,8 @@ 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.0.dist-info/LICENSE,sha256=iDPJdze6sBlBBSoB-BIyT2iHfHDGUAaZG3nTFd6m2FQ,1070
14
- django_api_versioning-0.1.0.dist-info/METADATA,sha256=B5McJrgZzySQghhKtqCvLMfBABI_TjgIFX8zHAnidS4,10436
15
- django_api_versioning-0.1.0.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
16
- django_api_versioning-0.1.0.dist-info/top_level.txt,sha256=F4n1zaE6P--9OytuMrvCD50vn7NvIVWkIl6ie9fsFck,28
17
- django_api_versioning-0.1.0.dist-info/RECORD,,
13
+ django_api_versioning-0.1.2.dist-info/LICENSE,sha256=iDPJdze6sBlBBSoB-BIyT2iHfHDGUAaZG3nTFd6m2FQ,1070
14
+ django_api_versioning-0.1.2.dist-info/METADATA,sha256=RquKyi92c11LqbzxUNtIYCEH4ZINWc8g0vf2d4u0SR8,10679
15
+ django_api_versioning-0.1.2.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
16
+ django_api_versioning-0.1.2.dist-info/top_level.txt,sha256=F4n1zaE6P--9OytuMrvCD50vn7NvIVWkIl6ie9fsFck,28
17
+ django_api_versioning-0.1.2.dist-info/RECORD,,