Giter VIP home page Giter VIP logo

django-raw-api's Introduction

django-raw-api

Async-friendly straightforward Django API helpers

Hello world

from raw_api import validate_json

@validate_json({"name": str})
def hello(request):
    name = request.json["name"]
    if name == "death":
        return "not today", 403
    return {"hello": request.json["name"]}

Setup

  • Install in from pypi: pip install django-raw-api
  • Add raw_api into INSTALLED_APPS list of your settings.py
  • Add raw_api.middleware middleware into MIDDLEWARE

API

Middleware

It adds lazy request.json attribute and serializes raw responses such as:

  • str or tuple(message: str, status: int) - into plain text response
  • dict or tuple(data: dict, status: int) - into JSON response

Request

  • request.json: dict - parsed json
  • request.query: dict - parsed query (only after @validate_query)

Response

You can just return str or dict with an optional status code

def json_200ok(request):
    return {"hello": "world"}


def plain_text_with_status(request):
    return "bad request", 400

Authorization

Decorators @user_required and @staff_required is analogous to @login_required and @staff_member_required with JSON-based errors instead of redirecting

Both decorators cache request.user so you can use it without sync_to_async even in async views.

from raw_api import user_required, staff_required

@user_required
async def user(request):
    # no `sync_to_async` required
    return request.user.username

@staff_required
def staff(request):
    return {"admin": "zone"}

Data validation

@validate_query and @validate_json decorators are there to perform simple first-level validation of requests data. Internally they use the trafaret library.

from raw_api import validate_json, validate_query

@validate_json({"ids": [int], "hello?": str})
async def foo(request):
    return request.json

@validate_query({"id": int})
def bar(request):
    assert isinstance(id, int)
    return request.query

Examples

There's an example of using it with async views in the examples folder.

Tests

    python -m venv .venv
    source .venv/bin/activate
    pip install -Ur requirements-dev.txt
    python -m pytest tests

django-raw-api's People

Contributors

imbolc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.