Skip to main content

An additional layer to Django aiming to simplify resilient API creation

Project description

Jaad

Build Version Black

Overview

Jaad is an addition to Django.

It is based on Django Rest Framework and is designed to simplify the syntax needed to expose a resilient REST API.

Installation

To install Jaad using pip run:

pip install jaad

Creating a new project

To create a new project create a project directory and use jaad-admin.py startproject.

This will create the following files:

.
|-- server/
|     |-- apps/
|     |    |-- __init__.py
|     |    `-- index/
|     |         |-- __init__.py
|     |         |-- parameters.py
|     |         `-- view.py
|     |-- logs/
|     |-- settings/
|     |    |-- __init__.py
|     |    |-- application.py
|     |    |-- local.py
|     |    `-- __init__.py
|     `-- urls.py
`-- manage.py

manage.py is the classic Django management script hence you can run your server using:

DJANGO_SETTINGS_MODULE=server.settings.local python manage.py runserver

This will launch your server and make it available here: http://127.0.0.1:8000/

A default Index view was automatically created.

You can have a look at its definition in server/apps/index/ as it uses Jaad core features.

Creating a new view

To create a view create a submodule in apps and define in it a class inheriting from JaadView. Then define the HTTP method you want to support:

from jaad.view import JaadView

class Hello(JaadView):
    def get(self, request):
        return "Hello"

    def post(self, request):
        return "This was a POST request"

You can then expose it by modifying server/urls.py

Defining parameters

You can define a set of parameters that a view support using a Jaad Form:

from jaad.forms import fields


class HelloWorldParameters(fields.Form):
    name = fields.StringField(
        required=True,
        help_text="Enter your name :)"
    )

And then using it in your views:

from jaad.view import JaadView, with_parameters


class Hello(JaadView):
    @with_parameters("GET", HelloWorldParameters)
    def get(self, request, parameters):
        return "Hello {}".format(parameters["name"])

    def post(self, request):
        return "This was a POST request"

The additional parameter used to call your method contains your parameter values validated and converted to python objects (int, datetime, ...).

with_parameters can be called with as many Form as you want and will add a parameter to your method for each Form.

Interactive documentation

You can add an interactive documentation to your API using jaad-admin.py add-doc.

This will create the following files in server/apps/

documentation/
    |-- static/
    |     `-- css/
    |          `-- documentation.css
    |-- templates/
    |     `-- rest_framework_swagger/
    |          |-- base.html
    |          `-- index.html
    `-- urls.py

This is a Swagger UI that relies on Django Rest Framework Swagger and is not exposed yet.

To expose it:

  1. In server/urls.py import include from django.conf.urls if not already done and add url(r'^doc/', include('documentation.urls')) to urlpatterns.
  2. Add 'server.apps.documentation' to INSTALLED_APPS in server/settings/application.py

You can now access your API documentation on http://127.0.0.1:8000/doc/

The documentation is automatically updated based on all exposed view docstrings and parameter definitions.

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

jaad-0.11.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

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

jaad-0.11-py3-none-any.whl (29.1 kB view details)

Uploaded Python 3

File details

Details for the file jaad-0.11.tar.gz.

File metadata

  • Download URL: jaad-0.11.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for jaad-0.11.tar.gz
Algorithm Hash digest
SHA256 03a871657bd7e3dfd57da14745b04c796be0b75f5fcf70269a6f47fca3f0190b
MD5 a678dbaedc98a3c7c0d9bff46d37020c
BLAKE2b-256 a411ee8d1a86f283d4e84295f274c6d4b704e5557c732cdf9b53e999014588a2

See more details on using hashes here.

File details

Details for the file jaad-0.11-py3-none-any.whl.

File metadata

  • Download URL: jaad-0.11-py3-none-any.whl
  • Upload date:
  • Size: 29.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for jaad-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 3cdd525bdb0238210994667dec4d5f259f41ae2f4fbd20ae2d8d97faa0e4008e
MD5 cb3bed895e21c27945f0d4bce125445d
BLAKE2b-256 66afd8a0feaa37fad347469e7740642bf6a436883f41c9e643ceede4271e1888

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