Django Easy API

Django Easy API

  • Installation
  • Usage
  • API

API

EasyAPI

Instantiation

EasyAPI(name, permissions, description=None)

source

Instantiates an EasyAPI.

Arguments:

name (String): The name you'd like to appear on the generated api root.

permissions (Object): The Django Rest Framework permission level you'd like this api to have as it's default for all actions.

description (String): The description you'd like to appear on the generated api root.

Returns:

(Object): The EasyAPI object which you will register models to.

Note: These are the permissions provided by Django Rest Framework.

Example:

# <project root>/api.py

from EasyAPI.models import EasyAPI

publicapi = EasyAPI('Public API',
                    permissions.AllowAny,
                    'This is a public API')

register

register(model, api_class)

source

Registers a model with an EasyAPI.

Arguments:

model (Object): The model which you'd like to register with the EasyAPI.

api_class (Object): The ModelAPI which you'd like to register with the EasyAPI.

Returns:

(None): The model is registered with the EasyAPI

Example:

# widgets/api.py

from .models import Widget
from <project root>.api import publicapi
from EasyAPI.models import ModelAPI

class PublicWidgetAPI(ModelAPI):
    api_fields = ('name', 'color')

publicapi.register(Widget, PublicWidgetAPI)

ModelAPI Attributes

api_fields

api_fields = () # Default is all fields.

(String): A tuple of the fields which this API will provide access to.

Example:

api_fields = ('name', 'shape')
api_fields = ('name', 'color', 'size', 'shape', 'cost')

actions

actions = {} # Default is all actions have permission of the API itself.

{Dictionary}: A dictionary whose key is the action and whose value is the permission level to perform that action.

Note: Actions are the Django Rest Framework ViewSet actions. (create, edit, retrieve, list, delete) Viewset actions

Note: These are the permissions provided by Django Rest Framework.

Example:

from rest_framework import permissions
actions = {'create': permissions.IsAdminUser} 
# create has IsAdminUser permission all else defaults to API global settings.

actions = {'create': permissions.IsAdminUser,
           'edit': permissions.IsAuthenticated,
           'retrieve': permissions.AllowAny, 
           'list': permissions.IsAuthenticated,
           'delete': permissions.IsAdminUser
           } 
# global api permissions are ignored as they were all set here.

description

description = '' # Default is 'Generated by EasyAPI'

String: The description of the model which will be displayed on the generated API

Example:

description = 'The data in this API is provided for research purposes'

  • EasyAPI
    • Instantiation
    • register
  • ModelAPI Attributes
    • api_fields
    • actions
    • description
Copyright © 2019 Net Prophet Technologies