A Django REST framework API adapter for the App(IOS, Andorid, WebApp) API spec.
Project description
IOS&Andorid API and Django Rest Framework
Overview
App API support for Django REST Framework
Documentation: None
Format specification: http://jsonapi.org/format/
By default, Django REST Framework will produce a request like:
http://example.com/api/1.0/identities/?page=1
and then the response like:
{ "count": 200, "msg": "success!", "data": { "list": [ { "id": 3, "username": "john1", "full_name": "John Coltrane1" }, { "id": 4, "username": "john2", "full_name": "John Coltrane2" }, ], "total_count": 2, "total_pages": 1 } }
Goals
As a Django REST Framework APP API (short DJA) we are trying to address following goals:
Support the REST_FRAMEWORK_APPAPI to compliance
Be as compatible with Django REST Framework as possible
Have sane defaults to be as easy to pick up as possible
Be solid and tested with good coverage
Be performant
Requirements
Python (3.5, 3.6, 3.7)
Django (1.11, 2.1, 2.2)
Django REST Framework (3.10)
We highly recommend and only officially support the latest patch release of each Python, Django and REST Framework series.
Installation
From PyPI
$ pip install djangorestframework-appapi
From Source
$ git clone https://github.com/allran/djangorestframework-appapi.git $ cd django-rest-framework-app-api $ pip install -e .
Running the example app
It is recommended to create a virtualenv for testing. Assuming it is already installed and activated:
$ git clone https://github.com/allran/djangorestframework-appapi.git $ cd django-rest-framework-app-api $ pip install -U -e . -r requirements.txt $ django-admin migrate --settings=example.settings $ django-admin loaddata drf_example --settings=example.settings $ django-admin runserver --settings=example.settings
Browse to http://localhost:8000
Running Tests and linting
It is recommended to create a virtualenv for testing. Assuming it is already installed and activated:
$ pip install -Ur requirements.txt $ flake8 $ pytest
Usage
rest_framework_app_api assumes you are using class-based views in Django Rest Framework.
if you use like ListAPIView in from rest_framework.generics import ListAPIView, please replace with from rest_framework_app_api.generics import ListAPIView.
from snippets.models import Snippet from snippets.serializers import SnippetSerializer from rest_framework_app_api import generics class SnippetList(generics.ListCreateAPIView): queryset = Snippet.objects.all() serializer_class = SnippetSerializer class SnippetDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Snippet.objects.all() serializer_class = SnippetSerializer
if you use like ListModelMixin in from rest_framework.mixins import ListModelMixin, please replace with from rest_framework_app_api.mixins import ListModelMixin.
from snippets.models import Snippet from snippets.serializers import SnippetSerializer from rest_framework_app_api import mixins from rest_framework import generics class SnippetDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView): queryset = Snippet.objects.all() serializer_class = SnippetSerializer def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.update(request, *args, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs)
if you use like APIView in from rest_framework.views import APIView, please replace with from rest_framework_app_api.views import APIView.
from snippets.models import Snippet from snippets.serializers import SnippetSerializer from rest_framework_app_api.views import APIView from rest_framework_app_api.response import APIResponse from rest_framework import status class SnippetList(APIView): """ List all snippets, or create a new snippet. """ def get(self, request, format=None): snippets = Snippet.objects.all() serializer = SnippetSerializer(snippets, many=True) return APIResponse(serializer.data) def post(self, request, format=None): serializer = SnippetSerializer(data=request.data) if serializer.is_valid(): serializer.save() return APIResponse(serializer.data) return APIResponse(serializer.errors) class SnippetDetail(APIView): """ Retrieve, update or delete a snippet instance. """ def get(self, request, pk, format=None): snippet = self.get_object(pk) serializer = SnippetSerializer(snippet) return APIResponse(serializer.data) def put(self, request, pk, format=None): snippet = self.get_object(pk) serializer = SnippetSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() return APIResponse(serializer.data) return APIResponse(serializer.errors, code=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk, format=None): snippet = self.get_object(pk) snippet.delete() return APIResponse(code=status.HTTP_204_NO_CONTENT)
if you use like ModelViewSet in from rest_framework.viewsets import ModelViewSet, please replace with from rest_framework_app_api.viewsets import ModelViewSet.
from snippets.models import Snippet from rest_framework_app_api import viewsets class SnippetViewSet(viewsets.ModelViewSet): queryset = Author.objects.all() serializer_class = AuthorSerializer
if you use like Response in from rest_framework.response import Response, please replace with from rest_framework_app_api.response import APIResponse.
Settings
REST_FRAMEWORK = { 'PAGE_SIZE': 10, # rest_framework_json_api 'EXCEPTION_HANDLER': 'rest_framework_app_api.exceptions.exception_handler', 'DEFAULT_PAGINATION_CLASS': 'rest_framework_app_api.pagination.JsonApiPageNumberPagination', # rest_framework_json_api code 'DEFAULT_APP_CODE_SUCCESS': 200, # default success code 'DEFAULT_APP_CODE_FAIL': 0, # default error code # rest_framework_json_api msg 'DEFAULT_APP_MSG_CREAT_SUCCESS': 'create success!', 'DEFAULT_APP_MSG_UPDATE_SUCCESS': 'update success!', 'DEFAULT_APP_MSG_DELETE_SUCCESS': 'delete success!', 'DEFAULT_APP_MSG_SEARCH_SUCCESS': 'get data success!', 'DEFAULT_APP_MSG_SEARCH_NODATA': 'no data!', 'DEFAULT_APP_MSG_UNNONE': 'unknown error!', }
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for djangorestframework-appapi-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ccd18e45652f803ed498b7cc3cb661a1942caacc54acdbefadaa8cb4d7afd2c |
|
MD5 | ff1b882d6b94caac6033af066febac67 |
|
BLAKE2b-256 | 66929ae4a1fca97494b8a60d20baa35a8d5e6e5fa8f973df94c0b64b082faeda |
Hashes for djangorestframework_appapi-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7947fee743e6245ea9c6f53cf7fab65ecb1c67822645cfbdae59f01a5d317173 |
|
MD5 | 9f814f25052c4fc18710838d222fad71 |
|
BLAKE2b-256 | 756a309bce8a365745fd4dfb5a9e9d71f035aeaa957bc649464a5051ed4a532e |