Skip to content

Commit e67f577

Browse files
committed
Initial commit: Set up project structure and configuration
0 parents  commit e67f577

27 files changed

+1085
-0
lines changed

.github/workflows/publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Publish to PyPI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Build Docker image
17+
run: docker build -t django-api-versioning-package .
18+
19+
- name: Publish to PyPI
20+
run: |
21+
docker run --rm -e TASK=publish -e PYPI_API_TOKEN=${{ secrets.PYPI_API_TOKEN }} django-api-versioning-package

.github/workflows/tests.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Run tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
19+
- name: Build Docker image
20+
run: docker build -t django-api-versioning-test .
21+
22+
- name: Run tests in Docker
23+
run: docker run --rm django-api-versioning-test

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Byte-compiled / binary
2+
__pycache__/
3+
*.py[cod]
4+
*.so
5+
6+
# Virtual environments
7+
venv/
8+
.env/
9+
.env
10+
11+
# Editor files
12+
.idea/
13+
.vscode/
14+
15+
# Distribution / Packaging
16+
build/
17+
dist/
18+
*.egg-info/
19+
20+
# Pre-commit cache
21+
.pre-commit-cache/
22+
23+
# Pytest & Coverage
24+
.coverage
25+
pytest_cache/

.pre-commit-config.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
repos:
2+
3+
# Black for code formatting
4+
- repo: https://github.com/psf/black
5+
rev: 23.3.0 # version of black
6+
hooks:
7+
- id: black
8+
language_version: python3.9
9+
files: \.py$
10+
11+
# isort for sorting imports
12+
- repo: https://github.com/pre-commit/mirrors-isort
13+
rev: v5.10.1 # version of isort
14+
hooks:
15+
- id: isort
16+
language_version: python3.9
17+
files: \.py$
18+
19+
# Flake8 for linting
20+
- repo: https://github.com/PyCQA/flake8
21+
rev: 5.0.4 # version of flake8
22+
hooks:
23+
- id: flake8
24+
language_version: python3.9
25+
files: \.py$
26+
27+
# mypy for type checking
28+
- repo: https://github.com/pre-commit/mirrors-mypy
29+
rev: v0.991 # version of mypy
30+
hooks:
31+
- id: mypy
32+
language_version: python3.9
33+
files: \.py$
34+
35+
# # Pytest for testing
36+
# - repo: https://github.com/pre-commit/mirrors-pytest
37+
# rev: v6.0.0 # version of pytest
38+
# hooks:
39+
# - id: pytest
40+
# name: "Run tests with pytest"
41+
# entry: pytest
42+
# language: system # using the system's pytest
43+
# types: [python]
44+
# files: \.py$

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM python:3.9-slim
2+
3+
RUN apt-get update && apt-get install -y git
4+
5+
WORKDIR /app
6+
7+
COPY requirements.txt requirements-dev.txt ./
8+
RUN pip install --no-cache-dir -r requirements-dev.txt
9+
10+
COPY .pre-commit-config.yaml .
11+
RUN git init . && pre-commit install-hooks
12+
13+
COPY . .
14+
ENV PYTHONPATH=/app
15+
16+
RUN chmod +x ./entrypoint.sh
17+
ENTRYPOINT ["./entrypoint.sh"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [year] [Full name]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

MANIFEST.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include README.md
2+
include LICENSE
3+
include .pre-commit-config.yaml
4+
recursive-include django_api_versioning *.py
5+
recursive-include tests *.py
6+
include requirements.txt
7+
include requirements-dev.txt
8+
include pytest.ini

README.md

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
2+
# Django API Versioning
3+
4+
[![PyPI version](https://badge.fury.io/py/django-api-versioning.svg)](https://badge.fury.io/py/django-api-versioning)
5+
[![Build Status](https://github.com/mojtaba-arvin/django-api-versioning/actions/workflows/tests.yml/badge.svg)](https://github.com/mojtaba-arvin/django-api-versioning/actions)
6+
[![codecov](https://codecov.io/gh/mojtaba-arvin/django-api-versioning/branch/main/graph/badge.svg)](https://codecov.io/gh/mojtaba-arvin/django-api-versioning)
7+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8+
9+
**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.
10+
11+
## Features
12+
13+
- **Easy Versioning**: Define API versions using simple decorators.
14+
- **Backward Compatibility**: Automatically register routes for all versions up to the specified version.
15+
- **Automatic Registration:** Views are **automatically** registered for each version specified, so there is no need to manually register each version in your `urls.py`.
16+
- **Customizable Settings**: Configure API base path, minimum and maximum versions, and more.
17+
- **Type Checking**: Full support for type hints and static type checking with `mypy`.
18+
- **Testing Ready**: Includes comprehensive test suite and pre-commit hooks for code quality.
19+
20+
## Installation
21+
22+
You can install Django API Versioning via pip:
23+
24+
```bash
25+
pip install django-api-versioning
26+
```
27+
28+
## Quick Start
29+
30+
1. ### Add to Django Settings:
31+
32+
```python
33+
INSTALLED_APPS = [
34+
...
35+
'django_api_versioning',
36+
...
37+
]
38+
```
39+
40+
2. ### Define API Settings:
41+
42+
```python
43+
44+
API_BASE_PATH = "api/v{version}/"
45+
API_MIN_VERSION = 1
46+
API_MAX_VERSION = 3
47+
```
48+
49+
3. ### Register API urls:
50+
51+
if you don't use any `ROOT_URLCONF` in settings you can use this:
52+
53+
```python
54+
ROOT_URLCONF = 'django_api_versioning.urls'
55+
```
56+
57+
or you have already have a `ROOT_URLCONF` in settings, you only need to import them into your root `urls.py`:
58+
59+
```python
60+
from django.urls import path, include
61+
from django_api_versioning.urls import urlpatterns as api_urlpatterns
62+
63+
urlpatterns = [
64+
# other paths here
65+
66+
# use empty `route` param and use `API_BASE_PATH` in settings as prefix
67+
path('', include(api_urlpatterns)),
68+
]
69+
70+
```
71+
72+
3. ### Use the Decorator:
73+
74+
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.
75+
76+
77+
#### Example for Function-Based Views (FBVs):
78+
79+
```python
80+
from django_api_versioning.decorators import endpoint
81+
from django.http import HttpResponse
82+
83+
@endpoint("users", version=2, app_name='account_app', view_name="users_list_api")
84+
def users_view(request):
85+
return HttpResponse("API Version 2 Users")
86+
```
87+
88+
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`.
89+
90+
#### Example for Class-Based Views (CBVs):
91+
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:
92+
93+
```python
94+
95+
from django_api_versioning.decorators import endpoint
96+
from django.http import JsonResponse
97+
from django.views import View
98+
99+
@endpoint("users", version=2, app_name='account_app', view_name="users_list_api")
100+
class UsersView(View):
101+
102+
def get(self, request):
103+
return JsonResponse({"message": "API Version 2 Users"})
104+
105+
```
106+
107+
#### Integration with Django Rest Framework (DRF):
108+
109+
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:
110+
111+
112+
```python
113+
from rest_framework.views import APIView
114+
from rest_framework.response import Response
115+
from django_api_versioning.decorators import endpoint
116+
117+
@endpoint("users", version=2, app_name='account_app', view_name="users_list_api")
118+
class UsersAPIView(APIView):
119+
120+
def get(self, request):
121+
return Response({"message": "API Version 2 Users"})
122+
```
123+
124+
#### URL Generation Based on Versioning:
125+
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:
126+
127+
* `/api/v1/account_app/users`
128+
* `/api/v2/account_app/users`
129+
130+
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.
131+
132+
#### Additional Configuration Options:
133+
134+
**Without `app_name`:** If you don't pass `app_name` in the decorator, like this:
135+
```python
136+
@endpoint("users", version=2, view_name="users_list_api")
137+
```
138+
139+
The generated URLs will be:
140+
141+
* `/api/v1/users`
142+
* `/api/v2/users`
143+
144+
145+
**Without `version`:** If you don't pass `version` in the decorator, like this:
146+
147+
```python
148+
@endpoint("users", view_name="users_list_api")
149+
```
150+
151+
API versioning will be disabled (`API_BASE_PATH` as prefix will be removed) for that view. The only URL generated will be:
152+
153+
* `/users`
154+
155+
**Setting `backward=False`:** By default, the `backward` parameter is set to `True`, which ensures backward compatibility. If you explicitly set `backward=False`, like this:
156+
157+
```python
158+
@endpoint("users", version=2, backward=False, view_name="users_list_api")
159+
```
160+
161+
The generated URL will be only version 2:
162+
163+
* `api/v2/users`
164+
165+
4. Run the Server:
166+
167+
```bash
168+
python manage.py runserver
169+
```
170+
171+
## Notes
172+
### 1. `API_BASE_PATH` in settings Must Include ‍‍`{version}`:
173+
The `API_BASE_PATH` should always include `{version}` to ensure proper API versioning. This is important for correctly mapping API routes to different versions.
174+
175+
### 2. Using `app_name` in the `endpoint` decorator:
176+
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.
177+
178+
### 3. Views with Version Less Than `API_MIN_VERSION` Are Automatically Ignored:
179+
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.
180+
181+
### 4. URLs for Versions Between `API_MIN_VERSION` <= `version` <= `API_MAX_VERSION`:
182+
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.
183+
184+
### `endpoint` Decorator Function Definition
185+
186+
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.
187+
188+
```python
189+
def endpoint(
190+
postfix: str,
191+
version: Optional[int] = None,
192+
backward: bool = True,
193+
app_name: Optional[str] = None,
194+
view_name: Optional[str] = None,
195+
) -> Callable:
196+
"""
197+
Decorator to register API views with versioning support.
198+
199+
- Uses `API_MIN_VERSION` and `API_MAX_VERSION` from Django settings.
200+
- Supports backward compatibility by registering multiple versions if needed.
201+
- Ensures that no version lower than `API_MIN_VERSION` is registered.
202+
203+
Args:
204+
postfix (str): The endpoint suffix (e.g., "users" → "api/v1/users").
205+
version (Optional[int]): The version of the API. Defaults to None (unversioned).
206+
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.
207+
app_name (Optional[str]): The app name to be prefixed to the route.
208+
view_name (Optional[str]): The custom view name for Django.
209+
210+
Returns:
211+
Callable: The decorated view function.
212+
213+
Raises:
214+
VersionTypeError: If the provided `version` is not an integer.
215+
VersionRangeError: If `API_MIN_VERSION` or `API_MAX_VERSION` are not properly set.
216+
"""
217+
```
218+
219+
220+
## Contributing
221+
222+
Feel free to open an issue or submit a pull request with any improvements or bug fixes. We appreciate contributions to enhance this package!
223+
224+
## License
225+
226+
This package is open-source and available under the MIT license.

django_api_versioning/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)