Skip to main content

Django Rest Structure

This app make your rest api application simple

Document

1. Install by running

pip install django-rest-structure


2. Add middleware to your MIDDLEWARE (or MIDDLEWARE_CLASSES) setting like this:

MIDDLEWARE = (
   'django_rest_structure.middlewares.middleware.RequestHandlerMiddleware',
   ...
)

3. Add exception handler to your REST_FRAMEWORK setting like this:

REST_FRAMEWORK = {
    ...
    'EXCEPTION_HANDLER': 'django_rest_structure.results.exception.exception_handler'
    ...
}
then add to YOUR-PROJECT/urls.py
handler404 = 'django_rest_structure.api.views.error_view_404'
handler500 = 'django_rest_structure.api.views.error_view_500'
handler403 = 'django_rest_structure.api.views.error_view_403'
handler400 = 'django_rest_structure.api.views.error_view_400'

4. Create your result messages like:

from django_rest_structure.results.codes import ResultMessageStructure
from django_rest_structure.results.exception import ResultMessages

class MyCustomResultMessages(ResultMessages):
    CUSTOM_ERROR = ResultMessageStructure(1, 'My Custom Error', False, 500)
    # `1` is your result code
    # `My Custom Error` is your result message
    # `False` define result is success or not
    # `500` is http response code

5. Create your custom exception like:

from django_rest_structure.results.exception import Err

class MyCustomError(MyCustomResultMessages, Err):
    def __init__(self, *args, **kwargs):
        super(MyCustomError, self).__init__(*args, **kwargs)

6. Now you should create your views like:

from django_rest_structure.api.views import BaseApiView

class MyView(BaseApiView):
    def post_method(self, request, *args, **kwargs):
        result = {}
        return ResponseStructure(ResultMessages.GET_SUCCESSFULLY, result)

sample 1:

request data:
{}
response data:
{
    "status": {
        "code": 200,
        "message": "Done",
        "is_success": true
    },
    "result": {}
}

7. Create your serializers like:

1. simple serializers

from django_rest_structure.api.views import BaseApiView
from django_rest_structure.api.serializers import BaseSerializer
from rest_framework import serializers

class MySerializer(BaseSerializer):
    my_field = serializers.CharField()

    def get_response(self):
        # the request available on self.request
        response_data = {
            'my_field': self.validated_data['my_field']
        }
        return ResponseStructure(ResultMessages.GET_SUCCESSFULLY, response_data)



class MyView(BaseApiView):
    def post_method(self, request, *args, **kwargs):
        return MySerializer(request.data, check_is_valid=True, request=request).get_response()

sample 1:

request data:
{
    "my_field": "test"
}
response data:
{
    "status": {
        "code": 200,
        "message": "Done",
        "is_success": true
    },
    "result": {
        "my_field": "test"
    }
}

2. serializer with custom validation

from django_rest_structure.api.views import BaseApiView
from django_rest_structure.api.serializers import BaseSerializer
from django_rest_structure.api.validations import StrFieldValidation
from rest_framework import serializers

def my_field_vld(value, **extra_params):
    return value.upper()


class MySerializer(BaseSerializer):
    # returned object from StrFieldValidation is available on validated_data
    my_field = serializers.CharField(validators=[StrFieldValidation(
        regex=r'^[A-Za-z]+$',
        error_message='just enter alphabet',
        extra_function=my_field_vld
    )])

    def get_response(self):
        # the request available on self.request
        response_data = {
            'my_field': self.validated_data['my_field']
        }
        return ResponseStructure(ResultMessages.GET_SUCCESSFULLY, response_data)


class MyView(BaseApiView):
    def post_method(self, request, *args, **kwargs):
        return MySerializer(request.data, check_is_valid=True, request=request).get_response()

sample 1:

request data:
{
    "my_field": "@"
}
response data:
{
    "status": {
        "code": 406,
        "message": "Entered Data Is Not Valid",
        "is_success": false
    },
    "result": {
        "my_field": [
            "enter just alphabet"
        ]
    }
}

sample 2:

request data:
{
    "my_field": "Test"
}
response data:
{
    "status": {
        "code": 200,
        "message": "Done",
        "is_success": true
    },
    "result": {
        "my_field": "TEST"
    }
}

3. list serializer

from django_rest_structure.api.views import BaseApiView
from django_rest_structure.api.serializers import MyListSerializer

class MySerializer(MyListSerializer):

    def get_response(self):
        # the request available on self.request
        my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
        response_data = self.paging_structure(
            *self.paging(my_list)
        )
        return ResponseStructure(ResultMessages.GET_SUCCESSFULLY, response_data)


class MyView(BaseApiView):
    def post_method(self, request, *args, **kwargs):
        return MySerializer(request.data, check_is_valid=True, request=request).get_response()

sample 1:

request data:
{
    "page": 1,
    "count": 5
}
response data:
{
    "status": {
        "code": 200,
        "message": "Done",
        "is_success": true
    },
    "result": {
        "list": [
            "a",
            "b",
            "c",
            "d",
            "e"
        ],
        "page": 1,
        "count_in_page": 5,
        "total_count": 12
    }
}

8. Config your settings

change structure response function by adding to your settings.py
REST_STRUCTURE_CONF = {
    'response_handler': 'django_rest_structure.results.structure.response_structure'
}
define your custom function like:
def response_structure(response):
    return {
        'status': {
            'code': response.message.code,
            'message': response.message.message,
            'is_success': response.message.is_success_result,

        },
        'result': response.body
    }
change logging handler function by adding to your settings.py
REST_STRUCTURE_CONF = {
        'log_handler': 'django_rest_structure.logs.console.emmit',
}
define your custom function like:
import logging
logger = logging.Logger('console')



def emmit(request, response, error, request_time, response_time):
    if error is not None:
        logger.debug(error)

9. You can see the simple project that use from this package at: django_rest_structure_sample

Release files for django-rest-structure 1.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-rest-structure 1.1.5
File Size Uploaded
django-rest-structure-1.1.5.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-rest-structure 1.1.5
File Interpreter ABI Platform
django_rest_structure-1.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / django-rest-structure-1.1.5.tar.gz

Download URL django-rest-structure-1.1.5.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2bcb292932961746421330792aa1c254371495e68de358a03f8dfd1e90cad5f7
BLAKE2b-256 checksum
How to use checksums
c3935f45778dd311c6cfbd75252bc54f295f72d675273942c0d3d99b83c774c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

Release files / django_rest_structure-1.1.5-py3-none-any.whl

Download URL django_rest_structure-1.1.5-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
417337e65878205a2b9664aa3421655b87806fcf9555931f68ba0a1fb65c1d6e
BLAKE2b-256 checksum
How to use checksums
5e9e5ab663a116aeac46d326f4fad2dc9a1075f7c8deb53ec140942099f6f10e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.1.5 This release

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page