A request parameter checking and parsing library based on pydantic under the sanic framework
Project description
sanic-dantic
sanic-dantic is a request parameter checking and parsing library based on pydantic under the sanic framework
sanic-dantic is a request parameter checking and parsing library based on pydantic under the sanic framework
It is based on pydantic, which can facilitate developers to quickly check and obtain request parameters
Installation
pip install sanic-dantic
Usage
Before writing all the examples, let's write a file server.py
first. In other examples, we will focus on the use of views and no longer write extraneous peripheral code.
from sanic import Sanic
from sanic.response import json
from sanic_dantic import parse_params, BaseModel, validator
app = Sanic("sanic-dantic-example")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
Before using, we need to use pydantic to create a class for format checking of request parameters
class Person(BaseModel):
name: str
age: int
Use in function-based views
In the function view, you can pass the pydantic model to different formal parameters in parse_params to check and parse the values of different types of request parameters
You can get all the parsed parameters by appending the formal parameter params
, and get the value of the parameter through the attribute
@app.route('/path_example/<name>/<age>/')
@parse_params(path=Person)
async def path_param_examples(request, name, age, params):
print(params.name, params.age)
return json({"message": f"hello {params.name} are you {params.age} years old ?"})
If you do not want to pass additional parameters in the form of params
way to get the parameters of the resolution, you can also get them by request.ctx.params
@app.route('/get_example/')
@parse_params(query=Person)
async def path_param_examples(request, params):
print(params.name, params.age)
print(request.ctx.params.name, request.ctx.params.age)
return json({"message": f"hello {params.name} are you {params.age} years old ?"})
@app.route('/post_example/')
@parse_params(body=Person)
async def path_param_examples(request, params):
print(params.name, params.age)
print(request.ctx.params.name, request.ctx.params.age)
return json({"message": f"hello {params.name} are you {params.age} years old ?"})
Use in class-based views
Of course, sanic-dantic
not only supports function-based views, it also supports class-based views
class SanicDanticExampleView(HTTPMethodView):
@parse_params(query=Person)
def get(self, request):
name, age = request.ctx.params.values()
return json({"message": f"hello {name} are you {age} years old ?"})
@parse_params(body=Person)
def put(self, request):
name, age = request.ctx.params.values()
return json({"message": f"hello {name} are you {age} years old ?"})
@parse_params(body=Person)
def put(self, request):
name, age = request.ctx.params.values()
return json({"message": f"hello {name} are you {age} years old ?"})
Notice
If you need, you can even add inspection classes for path, query, and body at the same time
@app.route('/get_example/')
@parse_params(query=Person, body=Person)
async def path_param_examples(request, params):
print(params.name, params.age)
print(request.ctx.params.name, request.ctx.params.age)
return json({"message": f"hello {params.name} are you {params.age} years old ?"})
However, if the parameter names in different types of parameters are consistent, the original parameters may be overwritten
> curl 'http://localhost:8000/get_example/?name=Connor&age=10' -x POST -d '{"name":"Calvin", "age":20}'
{"message": "hello Calvin are you 20 years old ?"}
The priority of different parameters is body
> query
> path
Other
sanic-dantic
integrates most of the usage of pydantic
, other more usage methods can refer to pydantic
Thanks
Thanks to the author Eric Jolibois of pydantic and the author Ahmed Nafies of sanic-pydantic.
Because of their open source, I have the inspiration to write sanic-dantic. Thank them very much
License
I'm pleased to support the open source community by making sanic-dantic available. Copyright (C) 2020, Connor Zhang. All rights reserved. If you have downloaded a copy of the sanic-dantic source code from here, please note that sanic-dantic source code is licensed under the MIT License. Your integration of sanic-dantic into your own projects may require compliance with the MIT License. A copy of the MIT License is included in this file.
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
File details
Details for the file sanic-dantic-1.0.5.tar.gz
.
File metadata
- Download URL: sanic-dantic-1.0.5.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6rc1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2e5ed8433d29dc1cd01a7f09ce62d2234d928d05eae912369f6bab840b011c3 |
|
MD5 | 3e619488c0a5583ee531001668398a0a |
|
BLAKE2b-256 | 2303e0f4a7208104520f505548d9ae829901b2acd4cb4b468491a49019730cd3 |