Skip to main content

Parses query args in sanic using type annotations

Project description

Build Status Latest Version Python Support

Sanicargs

Parses query args in 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_query_args decorator to parse query args and type cast query args and path params with Sanic's routes or blueprints like in the example below:

import datetime
from sanic import Sanic, response
from sanicargs import parse_query_args

app = Sanic("test_sanic_app")

@app.route("/me/<id>/birthdate", methods=['GET'])
@parse_query_args
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'

Fields

  • 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

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_query_args 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_query_args.

Note that request arg can be renamed and even type-annotated as long as it is the first arg.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanicargs-2.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

sanicargs-2.0.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file sanicargs-2.0.3.tar.gz.

File metadata

  • Download URL: sanicargs-2.0.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.7 Linux/4.15.0-1028-gcp

File hashes

Hashes for sanicargs-2.0.3.tar.gz
Algorithm Hash digest
SHA256 5fcc7f63c4e9c781f14ad1bf59be2afc1e9344aa6b6d6762455d6b8b2e523d05
MD5 22d68273c58b7c5ceb332c457cabe467
BLAKE2b-256 0ef33b66eb54a500a2f30801676a2df194a4d83ebfc470ee9ac490edc10e87de

See more details on using hashes here.

Provenance

File details

Details for the file sanicargs-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: sanicargs-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.7 Linux/4.15.0-1028-gcp

File hashes

Hashes for sanicargs-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c817fd50538e3dbef64d9f366fc8df1f8d55da208756894831f18024d55f073b
MD5 56298408fbe4ced670b3f18d9be10033
BLAKE2b-256 269a33274f3380226cd2fa8f3359f5ca0ff47cda924d979bb4a29fec2efbb853

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page