Skip to main content

Parses query args or body parameters in sanic using type annotations

Project description

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.

parse_query_args deprecation

parse_query_args will be deprecated in future version in favor of parse_parameters Currently it is still usable as a legacy decorator

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

Uploaded Source

Built Distribution

sanicargs-2.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sanicargs-2.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.6.7 Linux/4.15.0-1077-gcp

File hashes

Hashes for sanicargs-2.1.0.tar.gz
Algorithm Hash digest
SHA256 8fe3320a7805ef1c846c9870da0946bf0fb701b0984f11611ffbe5701b461794
MD5 d1f7730d642994a9c3a9d9ea6d216a13
BLAKE2b-256 1f61493f85b4e8a6a4499b85a58de856d5ff54ec58a1c50af7547b14da96a633

See more details on using hashes here.

Provenance

File details

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

File metadata

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

File hashes

Hashes for sanicargs-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a63480610f565be3c72b890468c919dfbbfc1e0d9b930a2aed4e7b0174ae50d
MD5 3b5d19c31fda05e59f40eb881a86a75c
BLAKE2b-256 eefc4305e073783ae427eb10514558d9f78f8fd701373eef8c99dd412687cae3

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