Skip to main content

Simple rest framework is an abstraction for building rest api with well written documentations

Project description

Simple Django Rest Framework - sdrf

Simple Django Rest Framework (sdrf) is django based app used to provide abstraction that combines both django rest framework with drf_yasg a swagger generator to implement good looking and well documented apis using djang

Quick Start

Install

  1. assuming you already have a django project that you need to add this app to you need to start with installing the package using
pip install sdrf
  1. install required apps to INSTALLED_APPS in django settings
INSTALLED_APPS = [
    ...
    "rest_framework",
    "drf_yasg",
    'sdrf',
]
  1. add swagger docs url to your project urls
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    ...
    path('',include('sdrf.urls'))
]
  1. check out if everything is okay by visting default swagger docs url at http://<your-project-url>/rest Empty Swagger UI example

Configure

now you have an rest app up and running you can configure it as you want. addtionally for django rest framework configuration and drf_yasg configuration you have our sdrf configs you can make with these default values

config variable name default value description
REST_APP_NAME Weather API the app name used in swagger docs view
REST_APP_VERSION v1 the api version used in swagger docs view
REST_APP_CREATOR {'name': '','email': '','url': ''} the contact information for communicating the rest app creator used in swagger docs view
REST_APP_BASE_URL rest/ the base url that the swagger docs will be viewd on and a prefix for default api endpoint

Usage

Build Single Method API Endpoints

  1. assuming you have an django app that already exsist create and an API endpoint class that inheiret APIEndPoint class for example see out hello world here
from sdrf.api_endpoint import APIEndpoint
from sdrf.endpoint_config import APIEndpointConfig


class HelloEndPoint(APIEndpoint):
    def configure(self, config: APIEndpointConfig) -> APIEndpointConfig:
        ...
        return config

    @staticmethod
    def execute(request: Request, *args, **kwargs) -> Response:
        name = request.query_params.get('name')
        return HttpResponse(f'Hello {name}')
  1. configure your api endpoint using configure method, in this method you have all sort of configuration that you can configure for your rest api even the auhentication and authorization configurations, http method, routing, and swagger view configs simplified with easy and nice looking config code
from sdrf.api_endpoint import APIEndpoint
from sdrf.endpoint_config import APIEndpointConfig


class HelloEndPoint(APIEndpoint):
    def configure(self, config: APIEndpointConfig) -> APIEndpointConfig:
        config.name = 'Hello Rest!'
        config.description= """
        First Well documented API endpoint that says hello to name that you send
        """
        config.endpoint= 'hello'
        config.http_method = 'GET'
        config.set_response(200,'hello [name]')
        config.add_parameter('name',self.DataType.STRING,self.ParameterTypes.QUERY_PARAM)
        config.add_tag('First API')
        return config

    @staticmethod
    def execute(request: Request, *args, **kwargs) -> Response:
        name = request.query_params.get('name')
        return HttpResponse(f'Hello {name}')
  1. finally add your api endpoint to your app urls using as_url static method
from .views import HelloEndPoint

urlpatterns = [
    HelloEndPoint.as_url()
]
  1. check your output and test your api in the swagger view

An example of swagger output of APIEndPoint

Build Model API EndPoint

just like you would do in normal django rest framework and make ViewSet you dont need to learn anything new to that but instead of inherting rest_framework.viewsets.ModelViewSetyou will inherit ourModelEndPoint`like that

from .models import Person,PersonSeralizer
from sdrf.model_endpoint import ModelEndPoint
...
class PersonEndPoint(ModelEndPoint):
    queryset = Person.objects.all()
    serializer_class = PersonSeralizer

then simply make it as urls in urls.py

from .views import PersonEndPoint

urlpatterns = [
    ...
    PersonEndPoint.as_urls(),
]

Model EndPoint Swagger output

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sdrf-0.1.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sdrf-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file sdrf-0.1.1.tar.gz.

File metadata

  • Download URL: sdrf-0.1.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for sdrf-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2765c71636201e517ef99340085a43b50e26b239b666b10a219af9c9cbb816be
MD5 e5268a2efc5620500635ef9f9d807e87
BLAKE2b-256 2a68762b58ce23737fbed2da50f41214f6a5c84478c1510c3a7a1a9192f0cf0b

See more details on using hashes here.

File details

Details for the file sdrf-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sdrf-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for sdrf-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 802557b7eefd4341e40f5eb97bb7b43b15f921a25e41aff2a2c8c0dfc91065b8
MD5 69df99e0983122b5431575641c10bd53
BLAKE2b-256 12182442b3a21e6b4d425c46427ad9e5d7a5e846ba4ad5871a13c4e46b3f46a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page