Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/assem-ch/django-fast-api
Few hacks to speed up defining apis based on django rest framwork, inspired from fastapi. Please give it a try and let me know your feedback.
https://github.com/assem-ch/django-fast-api
django django-rest-framework fastapi swagger
Last synced: 3 months ago
JSON representation
Few hacks to speed up defining apis based on django rest framwork, inspired from fastapi. Please give it a try and let me know your feedback.
- Host: GitHub
- URL: https://github.com/assem-ch/django-fast-api
- Owner: assem-ch
- License: bsd-3-clause
- Created: 2022-03-31T17:05:00.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-08-27T23:26:02.000Z (5 months ago)
- Last Synced: 2024-09-29T02:21:52.798Z (4 months ago)
- Topics: django, django-rest-framework, fastapi, swagger
- Language: Python
- Homepage:
- Size: 31.3 KB
- Stars: 17
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Django Fast API
=================Few hacks to speed up defining APIs based on django rest framwork, inspired from fastapi
**First version tested on: python 3.9 and django 4.0**
Features:
---------
- [x] Function based view
- [x] Easy to use decorator
- [x] Accepts validation of input and output using DRF serializers
- [x] Accept CamelCased input and group all rest input methods in same dict :`req`
- [x] Jsonify and camelcase any type of output: `str`, `dict`, `QuerySet`, `Model`
- [x] AutoSchema docs using drf-spectacular
- [x] Error handler that works with DRF
- [ ] Better way to pass the `request` object inside views
- [ ] Convert DRF serializers into a type annotation classes for better processing by IDEsQuick start
-----------1. Install the lib::
`$ pip install django-fast-api`
1. Add "drf_spectacular" to your ``INSTALLED_APPS`` setting like this::
```python
INSTALLED_APPS = [
...
"drf_spectacular",
...
]
```
2. Include the swagger documentation in your project ``urls.py`` like this::
```python
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerViewpath('api-schema/', SpectacularAPIView.as_view(), name='schema'),
path('api-doc/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
```
3. Add open api schema class and exception handler to "REST_FRAMEWORK" settings::
```pythonREST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
'EXCEPTION_HANDLER': 'fast_api.error_handling.exception_handler'
}```
4. Examples of usage in views::
```pythonfrom fast_api.decorators import APIRouter
router = APIRouter()
from . import serializers, models@router.api('public/health_check')
def health_check(req):
return "ok"@router.api('sample')
def sample_view():
return {
'key' : 'value'
}
@router.api('sample/error')
def error_view():
assert False, "This is the error message user will get"# with input & output validation
from rest_framework import serializers
from test_api.models import Countryclass CreateCountryRequest(serializers.ModelSerializer):
class Meta:
model = Country
fields = ['name']
class GetCountryRequest(serializers.Serializer):
id = serializers.IntegerField()
class CountryResponse(serializers.ModelSerializer):
class Meta:
model = Country
fields = ['name']
@router.api('country/create')
def create_company(req: serializers.CreateCountryRequest) -> serializers.CountryResponse:
return models.Country.objects.create(**req.args)
@router.api('country/get')
def create_company(req: GetCountryRequest) -> CountryResponse:
return models.Country.objects.get(id=req.args.id)
```* req is a django request
* you will find all endpoint args in req.argsIssues and Feedback
====================If you found an issue or you have a feedback , don't hesitate to point to it as a github issue.