Skip to main content

Build Status Latest Version Python Support

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.

Metadata

Release files for sanicargs 3.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanicargs 3.0.1
File Size Uploaded Provenance
sanicargs-3.0.1.tar.gz 4.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sanicargs 3.0.1
File Interpreter ABI Platform
sanicargs-3.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.1 kB

Release files / sanicargs-3.0.1.tar.gz

Download URL sanicargs-3.0.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
008ab6e9e96808db696e5abd037517df42b6f0716820c83b173ee3f640bb8827
BLAKE2b-256 checksum
How to use checksums
06c7abb3faa5f5e099b5049949d68a5e4c0304de82c2c1db9449f26d72f1567f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.4 Darwin/22.6.0

Release files / sanicargs-3.0.1-py3-none-any.whl

Download URL sanicargs-3.0.1-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
740eb48b6365065a5af196cbcd947db24c1e1d81970bc66918f6e51e0153347d
BLAKE2b-256 checksum
How to use checksums
7bb4d4000d63e9f78376615ee7f5da2161b230f4fbd5d85e0f8e613e60b0218e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.4 Darwin/22.6.0

Security for sanicargs 3.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page