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.

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

  • 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

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.

History

0.0.1 (2017-01-09)

  • git init

1.0.0(2017-01-12)

  • added test suite

  • added path_param type casting

1.1.0(2018-03-01)

  • request can be renamed and even type-annotated

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-1.1.0.tar.gz (5.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: sanicargs-1.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sanicargs-1.1.0.tar.gz
Algorithm Hash digest
SHA256 2bc995457236fcf3a990e835b2d6353ed78ad353330791be7fbee45b39831d5c
MD5 61022c10985ba39449f038da8a8be7d2
BLAKE2b-256 079a6d7cf501d15aa84b73f0043a0f76662da4742fc5b6234571c3d7e1c6d4d4

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