Skip to content

Commit 50c26db

Browse files
committed
Update README to explain API version resolution behavior (latest version used)
1 parent f038833 commit 50c26db

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

README.md

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ def users_view(request):
8585

8686
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`.
8787

88+
```bash
89+
api/v1/account_app/users [name='users_list_api']
90+
api/v2/account_app/users [name='users_list_api']
91+
```
92+
8893
#### Example for Class-Based Views (CBVs):
8994

9095
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:
@@ -109,22 +114,26 @@ If you have already installed [Django Rest Framework](https://www.django-rest-fr
109114

110115
```python
111116
from rest_framework.views import APIView
117+
from rest_framework.permissions import AllowAny
112118
from rest_framework.response import Response
113119
from django_api_versioning.decorators import endpoint
114120

115121
@endpoint("users", version=2, app_name='account_app', view_name="users_list_api")
116122
class UsersAPIView(APIView):
123+
permission_classes = [AllowAny]
117124

118125
def get(self, request):
119126
return Response({"message": "API Version 2 Users"})
120127
```
121128

122129
#### URL Generation Based on Versioning:
123130

124-
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:
131+
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:
125132

126-
- `/api/v1/account_app/users`
127-
- `/api/v2/account_app/users`
133+
```bash
134+
api/v1/account_app/users [name='users_list_api']
135+
api/v2/account_app/users [name='users_list_api']
136+
```
128137

129138
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.
130139

@@ -138,8 +147,10 @@ The `API_MIN_VERSION` setting ensures that users can access the API using differ
138147

139148
The generated URLs will be:
140149

141-
- `/api/v1/users`
142-
- `/api/v2/users`
150+
```bash
151+
api/v1/users [name='users_list_api']
152+
api/v2/users [name='users_list_api']
153+
```
143154

144155
**Without `version`:** If you don't pass `version` in the decorator, like this:
145156

@@ -149,7 +160,9 @@ The generated URLs will be:
149160

150161
API versioning will be disabled (`API_BASE_PATH` as prefix will be removed) for that view. The only URL generated will be:
151162

152-
- `/users`
163+
```bash
164+
users [name='users_list_api']
165+
```
153166

154167
**Setting `backward=False`:** By default, the `backward` parameter is set to `True`, which ensures backward compatibility. If you explicitly set `backward=False`, like this:
155168

@@ -159,7 +172,9 @@ API versioning will be disabled (`API_BASE_PATH` as prefix will be removed) for
159172

160173
The generated URL will be only version 2:
161174

162-
- `api/v2/users`
175+
```bash
176+
api/v2/users [name='users_list_api']
177+
```
163178

164179
4. Run the Server:
165180

@@ -169,23 +184,56 @@ python manage.py runserver
169184

170185
## Notes
171186

172-
### 1. `API_BASE_PATH` in settings Must Include ‍‍`{version}`:
187+
#### 1. `API_BASE_PATH` in settings Must Include ‍‍`{version}`:
173188

174189
The `API_BASE_PATH` should always include `{version}` to ensure proper API versioning. This is important for correctly mapping API routes to different versions.
175190

176-
### 2. Using `app_name` in the `endpoint` decorator:
191+
#### 2. Using `app_name` in the `endpoint` decorator:
177192

178193
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.
179194

180-
### 3. Views with Version Less Than `API_MIN_VERSION` Are Automatically Ignored:
195+
#### 3. Behavior When Resolving a Route:
196+
197+
When resolving the route using Django's `reverse()` function or any other method to resolve the URL, the latest version (highest version number) of the API will be returned. In this example, route for version 3 would be resolved:
198+
199+
```python
200+
from django_api_versioning.decorators import endpoint
201+
from django.http import JsonResponse
202+
from django.views import View
203+
from django.urls import reverse
204+
205+
206+
@endpoint("users", version=3, app_name='account_app', view_name="users_list_api")
207+
class UsersView(View):
208+
209+
def get(self, request):
210+
211+
return JsonResponse({"path of users_list_api view is": reverse('users_list_api')})
212+
```
213+
214+
response body:
215+
216+
```json
217+
{ "path of users_list_api view is": "api/v3/account_app/users" }
218+
```
219+
220+
The generated URLs will be:
221+
222+
```bash
223+
api/v1/account_app/users [name='users_list_api']
224+
api/v2/account_app/users [name='users_list_api']
225+
api/v3/account_app/users [name='users_list_api']
226+
```
227+
228+
#### 4. Views with Version Less Than `API_MIN_VERSION` Are Automatically Ignored:
181229

182230
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.
183231

184-
### 4. URLs for Versions Between `API_MIN_VERSION` <= `version` <= `API_MAX_VERSION`:
232+
#### 5. URLs for Versions Between `API_MIN_VERSION` <= `version` <= `API_MAX_VERSION`:
185233

186234
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.
187235

188-
### `endpoint` Decorator Function Definition
236+
## `endpoint` Decorator Function Definition
189237

190238
The `endpoint` decorator is designed to register API views with versioning support in a Django application. It provides flexibility in managing versioned endpoints and ensures backward compatibility with previous versions of the API.
191239

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="django-api-versioning",
8-
version="0.1.3",
8+
version="0.1.4",
99
author="Mojtaba Arvin",
1010
author_email="ArvinDevDay@gmail.com",
1111
description= (

0 commit comments

Comments
 (0)