Parses query args or body parameters in sanic using type annotations
Project description
Sanicargs
Parses query parameters and json body parameters for Sanic using type annotations.
Survey
Please fill out this survey if you are using Sanicargs, we are gathering feedback :-)
Install
Install with pip
$ pip install sanicargs
Usage
Use the parse_parameters
decorator to parse query parameters (GET) or body parameters (POST) and type cast them together with path params in Sanic's routes or blueprints like in this example below:
import datetime
from sanic import Sanic, response
from sanicargs import parse_parameters
app = Sanic("test_sanic_app")
@app.route("/me/<id>/birthdate", methods=['GET'])
@parse_parameters
async def test_datetime(req, id: str, birthdate: datetime.datetime):
return response.json({
'id': id,
'birthdate': birthdate.isoformat()
})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, access_log=False, debug=False)
Test it running with
$ curl 'http://0.0.0.0:8080/me/123/birthdate?birthdate=2017-10-30'
Query parameters
- str :
ex: ?message=hello world
- int :
ex: ?age=100
- bool :
ex: ?missing=false
- datetime.datetime :
ex: ?currentdate=2017-10-30T10:10:30 or 2017-10-30
- datetime.date :
ex: ?birthdate=2017-10-30
- List[str] :
ex: ?words=you,me,them,we
JSON body parameters
{ "message": "hello word", "age": 100, "missing": false, "currentDate": "2017-10-30", "currentDateTime": "2017-10-30T10:10:30", "words": ["you", "me", "them", "we"] }
Note about datetimes
Dates and datetimes are parsed without timezone information giving you a "naive datetime" object. See the note on datetime.timestamp() about handling timezones if you require epoch format timestamps.
Important notice about decorators
The sequence of decorators is, as usual, important in Python.
You need to apply the parse_parameters
decorator as the first one executed which means closest to the def
.
request
is mandatory!
You should always have request as the first argument in your function in order to use parse_parameters
.
Note that request
arg can be renamed and even type-annotated as long as it is the first arg.
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
Built Distribution
File details
Details for the file sanicargs-3.0.1.tar.gz
.
File metadata
- Download URL: sanicargs-3.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.4 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 008ab6e9e96808db696e5abd037517df42b6f0716820c83b173ee3f640bb8827 |
|
MD5 | d581e12d7dcf0f79a5cda135dde10da4 |
|
BLAKE2b-256 | 06c7abb3faa5f5e099b5049949d68a5e4c0304de82c2c1db9449f26d72f1567f |
File details
Details for the file sanicargs-3.0.1-py3-none-any.whl
.
File metadata
- Download URL: sanicargs-3.0.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.4 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 740eb48b6365065a5af196cbcd947db24c1e1d81970bc66918f6e51e0153347d |
|
MD5 | 2fae09943c83fd00f1f4f1fa63248393 |
|
BLAKE2b-256 | 7bb4d4000d63e9f78376615ee7f5da2161b230f4fbd5d85e0f8e613e60b0218e |