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_apiintoINSTALLED_APPSlist of yoursettings.py - Add
raw_api.middlewaremiddleware intoMIDDLEWARE
API
Middleware
It adds lazy request.json attribute and serializes raw responses such as:
strortuple(message: str, status: int)- into plain text responsedictortuple(data: dict, status: int)- into JSON response
Request
request.json: dict- parsed jsonrequest.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
Metadata
Release files for django-raw-api 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_raw_api-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for django_raw_api-0.4.0-py3-none-any.whl |
Release files / django_raw_api-0.4.0-py3-none-any.whl
| Download URL | django_raw_api-0.4.0-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70e51497f3304378f6d7f644d3f490144aaa60ae7f10d9158d4698d8bde5558d
|
|
BLAKE2b-256 checksum How to use checksums |
b7f19e7e496588994828418a99398e22ae008f6da122aafb0257e03a01014f28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2
|
Security for django-raw-api 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.