Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/imbolc/django-raw-api
Async-friendly straightforward Django API helpers
https://github.com/imbolc/django-raw-api
async asyncio django json json-api json-validation rest rest-api
Last synced: about 1 month ago
JSON representation
Async-friendly straightforward Django API helpers
- Host: GitHub
- URL: https://github.com/imbolc/django-raw-api
- Owner: imbolc
- License: mit
- Created: 2018-11-28T15:03:20.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-30T11:10:46.000Z (over 4 years ago)
- Last Synced: 2024-11-09T08:47:02.277Z (about 1 month ago)
- Topics: async, asyncio, django, json, json-api, json-validation, rest, rest-api
- Language: Python
- Homepage:
- Size: 17.6 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
django-raw-api
==============
[Async][async views]-friendly straightforward Django API helpersHello world
-----------
```python
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
```python
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 redirectingBoth decorators cache `request.user` so you can use it without `sync_to_async`
even in async views.```python
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.```python
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
-----
```bash
python -m venv .venv
source .venv/bin/activate
pip install -Ur requirements-dev.txt
python -m pytest tests
```[async views]: https://docs.djangoproject.com/en/3.1/topics/async/#async-views
[trafaret]: https://github.com/Deepwalker/trafaret