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.
BSD-3-CLAUSE License
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
req
str
, dict
, QuerySet
, Model
request
object inside viewsInstall the lib::
$ pip install django-fast-api
Add "drf_spectacular" to your INSTALLED_APPS
setting like this::
INSTALLED_APPS = [
...
"drf_spectacular",
...
]
urls.py
like this:: from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
path('api-schema/', SpectacularAPIView.as_view(), name='schema'),
path('api-doc/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
'EXCEPTION_HANDLER': 'fast_api.error_handling.exception_handler'
}
from 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 Country
class 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)
If you found an issue or you have a feedback , don't hesitate to point to it as a github issue.