Django简易json api接口封装。
Project description
django-apis
Django简易json api接口框架。使用Django Form做接口参数验证。
安装
pip install django-apis
使用
简单的接口开发
- 所有视图参数按django标准,添加request或其它路径参数。
- 可以定义Form来完成请求参数的校验。
- 在检验不通过的情况下,抛出
django_apis.exceptions.InputValidationError
。 django_apis.exceptions.InputValidationError
会把django的异常信息转化为文本格式。
- 在检验不通过的情况下,抛出
apiview
中methods
参数表示允许的HTTP请求类型。- 可以是字符串,如:
"get"
。 - 也可以是字符串数组,如:
["get", "post"]
。 - 大小写均可。
- 可以是字符串,如:
forms.py
from django.forms import Form
from django.forms.fields import CharField
class EchoInput(Form):
msg = CharField(max_length=16, required=True)
views.py
from django_apis.views import get_apiview
from django_apis.views import get_json_payload
from django_apis.exceptions import InputValidationError
from .forms import EchoInput
apiview = get_apiview()
@apiview(methods="GET")
def ping(request):
return "pong"
@apiview(methods="POST")
def echo(request):
payload = get_json_payload(request)
form = EchoInput(payload)
if not form.is_valid():
raise InputValidationError(form)
return form.cleaned_data["msg"]
@apiview(methods="GET")
def getRequestInfo(request):
result = {}
for key, value in request.META.items():
if isinstance(value, str):
result[key] = value
return result
配置项
- DJANGO_APIS_ALLOWED_METHODS
- DJANGO_APIS_APIVIEW
- DJANGO_APIS_ENABLE_REQUEST_LOG
- DJANGO_APIS_REQUEST_LOG_NAME
版本记录
v0.1.4
- 版本首发。
v0.1.5
- 添加
http_bearer_auth_protect
方法。
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-apis-0.1.5.tar.gz
(9.8 kB
view details)
Built Distribution
File details
Details for the file django-apis-0.1.5.tar.gz
.
File metadata
- Download URL: django-apis-0.1.5.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fd9312208da188ce2eb50ab2fc94c81537881c97194f2f9cc21b6247415d829 |
|
MD5 | ea254a4716c2ea1f8079597095cde329 |
|
BLAKE2b-256 | f17abc1770497d25f4988b9b5cdaec2c497b7e00c4b6999c42c1f08c91f25017 |
File details
Details for the file django_apis-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: django_apis-0.1.5-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e5ab51dd1a4c1262da51fb0a0d7b2d921bcb70ec2776da6c5781235359175a9 |
|
MD5 | 03b54622644b4cf4df04959cc415765c |
|
BLAKE2b-256 | 2c966066cbe1438735ad2362ba423a29467e49acbcc25ea0821c65f880165d5b |