Package for working with Django views and request/response validation in more convenient way.
Project description
django-hug
==========================
Package for working with Django urls/views and request/response validation in more convenient way.
Inspired by beautiful [hug](https://github.com/timothycrosley/hug).
Getting Started
===================
Simple API building example with django-hug
In your views module create routes and couple API endpoints.
```python
# views.py
import django_hug
from marshmallow import fields
routes = django_hug.Routes()
@routes.get('new/<int:year>/')
def simple(request, year, month: int):
return {"year": year, "month": month}
@routes.get('happy/<int:year>/')
def mm_field(request, year, month: fields.Int(validate=Range(min=1, max=12)) = 1):
return [year, month]
```
In your urls.py specify new endpoints
```python
from . import views
urlpatterns = views.routes.urls()
```
Thats all, now you can make requests to new API endpoints with convenient data validation
```bash
curl http://127.0.0.1:8000/new/2019/?month=133
>> {"year": 2019, "month": 133}
```
and nice errors handling
```bash
curl http://127.0.0.1:8000/happy/2019/?month=133
>> {"errors": {"month": ["Must be between 1 and 12."]}}
```
Usage
=====
#### Regexp path
You can also use regexp path
```python
@routes.get("(?P<year>[0-9]{4})/", re=True)
def index3(request, year, name: str):
loc = locals()
del loc["request"]
return loc
```
#### Directives
Use builtin directive `body` to validate whole POST request
```python
# views.py
import django_hug
from marshmallow import Schema, fields
routes = django_hug.Routes(prefix="api")
class RespSchema(Schema):
id = fields.Int(required=True)
quantity = fields.Int(required=True)
@routes.post("test/")
def view(request, body: RespSchema()):
return body
```
#### Routes prefix
Specify prefix in Routes object to add prefix to all urls
```python
# views.py
import django_hug
routes = django_hug.Routes(prefix="api")
@routes.get('<int:year>/')
def api_method(request, year, month: int = 1):
return {"year": year, "month": month}
```
```bash
curl http://127.0.0.1:8000/api/2019/
>> {"year": 2019, "month": 1}
```
#### Types
__Coming soon__
#### Response and request formatting
Underscore/camelcase transform
__Coming soon__
#### To start example app
```bash
make venv
source activate
cd example
./manage.py runserver
```
Attention! Work In Progress
==
#### TODO
* Full support various view decorators (test_decorators)
* Support multiple routes for one view (?)
* Add exception handler
* Take into account content type for response
* Cleanup tests
==========================
Package for working with Django urls/views and request/response validation in more convenient way.
Inspired by beautiful [hug](https://github.com/timothycrosley/hug).
Getting Started
===================
Simple API building example with django-hug
In your views module create routes and couple API endpoints.
```python
# views.py
import django_hug
from marshmallow import fields
routes = django_hug.Routes()
@routes.get('new/<int:year>/')
def simple(request, year, month: int):
return {"year": year, "month": month}
@routes.get('happy/<int:year>/')
def mm_field(request, year, month: fields.Int(validate=Range(min=1, max=12)) = 1):
return [year, month]
```
In your urls.py specify new endpoints
```python
from . import views
urlpatterns = views.routes.urls()
```
Thats all, now you can make requests to new API endpoints with convenient data validation
```bash
curl http://127.0.0.1:8000/new/2019/?month=133
>> {"year": 2019, "month": 133}
```
and nice errors handling
```bash
curl http://127.0.0.1:8000/happy/2019/?month=133
>> {"errors": {"month": ["Must be between 1 and 12."]}}
```
Usage
=====
#### Regexp path
You can also use regexp path
```python
@routes.get("(?P<year>[0-9]{4})/", re=True)
def index3(request, year, name: str):
loc = locals()
del loc["request"]
return loc
```
#### Directives
Use builtin directive `body` to validate whole POST request
```python
# views.py
import django_hug
from marshmallow import Schema, fields
routes = django_hug.Routes(prefix="api")
class RespSchema(Schema):
id = fields.Int(required=True)
quantity = fields.Int(required=True)
@routes.post("test/")
def view(request, body: RespSchema()):
return body
```
#### Routes prefix
Specify prefix in Routes object to add prefix to all urls
```python
# views.py
import django_hug
routes = django_hug.Routes(prefix="api")
@routes.get('<int:year>/')
def api_method(request, year, month: int = 1):
return {"year": year, "month": month}
```
```bash
curl http://127.0.0.1:8000/api/2019/
>> {"year": 2019, "month": 1}
```
#### Types
__Coming soon__
#### Response and request formatting
Underscore/camelcase transform
__Coming soon__
#### To start example app
```bash
make venv
source activate
cd example
./manage.py runserver
```
Attention! Work In Progress
==
#### TODO
* Full support various view decorators (test_decorators)
* Support multiple routes for one view (?)
* Add exception handler
* Take into account content type for response
* Cleanup tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-hug-0.1b1.tar.gz
(8.7 kB
view details)
Built Distribution
File details
Details for the file django-hug-0.1b1.tar.gz
.
File metadata
- Download URL: django-hug-0.1b1.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38ed25bd4e16e6e837f7abcea7c2fe8942f9a6190021c0ad5b2dcc6712f8c2b2 |
|
MD5 | d2d4badee9930a9ca7b4eadbaaf6b141 |
|
BLAKE2b-256 | cdcc2496e546733e34903c6482060ed05c56c7e643dc2b8d9b8bf71b3baeb4d8 |
File details
Details for the file django_hug-0.1b1-py3-none-any.whl
.
File metadata
- Download URL: django_hug-0.1b1-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83173e09591ca33fb7231fb5a2cd2e4fa944420507928699ee62c6ddfecf1c82 |
|
MD5 | 8798fa4830cc435548d7bc8341739d68 |
|
BLAKE2b-256 | 298d0e06f8966c8c134bab9ffa2f82ee4b1c92365e29a32d3f55667dcbc10b76 |