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.0.tar.gz
.
File metadata
- Download URL: sanicargs-3.0.0.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 | 95b78272330315933b6f999e8abcdd030dc1b2a6197ffeace57a812aaede0935 |
|
MD5 | 9cfa790231179bc4c9ec62f88ac8a11b |
|
BLAKE2b-256 | 48c2254435b656e564146c98ee0f85add0b57813a50a9e4442c8a27a05bcbbde |
Provenance
File details
Details for the file sanicargs-3.0.0-py3-none-any.whl
.
File metadata
- Download URL: sanicargs-3.0.0-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 | d8ea67b950faa3684bac3771e9d2e3108aa0177b5314927d57dab73d48fd6575 |
|
MD5 | 2f3526604025d6798cfb4b1c158ad0ab |
|
BLAKE2b-256 | 14fc3d3c27ca320eed60d7d70c195e9767ff5d5f242c5c57f5c6ad2555b8eb49 |