sanicargs


Namesanicargs JSON
Version 3.0.1 PyPI version JSON
download
home_pagehttps://github.com/trustpilot/python-sanicargs
SummaryParses query args or body parameters in sanic using type annotations
upload_time2023-08-01 14:50:47
maintainer
docs_urlNone
authorJohn Sutherland
requires_python>=3.8,<4.0
license
keywords sanicargs sanic query args type annotations
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            [![Build Status](https://github.com/trustpilot/python-sanicargs/actions/workflows/tox.yaml/badge.svg)](https://github.com/trustpilot/python-sanicargs/actions/workflows/tox.yaml) [![Latest Version](https://img.shields.io/pypi/v/sanicargs.svg)](https://pypi.python.org/pypi/sanicargs) [![Python Support](https://img.shields.io/pypi/pyversions/sanicargs.svg)](https://pypi.python.org/pypi/sanicargs)

# Sanicargs
Parses query parameters and json body parameters for [Sanic](https://github.com/channelcat/sanic) using type annotations.

## Survey
Please fill out [this survey](https://docs.google.com/forms/d/e/1FAIpQLSdNLvB7NEJQhUyVdaZpBAgS0f1k9OywZp8xDqhaNY0rl-unZA/viewform?usp=sf_link) 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](https://github.com/channelcat/sanic)'s routes or blueprints like in this [example](https://github.com/trustpilot/python-sanicargs/tree/master/examples/simple.py) below:

```python
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 
```bash
$ 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()](https://docs.python.org/3/library/datetime.html#datetime.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.
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/trustpilot/python-sanicargs",
    "name": "sanicargs",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "sanicargs,sanic,query,args,type annotations",
    "author": "John Sutherland",
    "author_email": "johns@trustpilot.com",
    "download_url": "https://files.pythonhosted.org/packages/06/c7/abb3faa5f5e099b5049949d68a5e4c0304de82c2c1db9449f26d72f1567f/sanicargs-3.0.1.tar.gz",
    "platform": null,
    "description": "[![Build Status](https://github.com/trustpilot/python-sanicargs/actions/workflows/tox.yaml/badge.svg)](https://github.com/trustpilot/python-sanicargs/actions/workflows/tox.yaml) [![Latest Version](https://img.shields.io/pypi/v/sanicargs.svg)](https://pypi.python.org/pypi/sanicargs) [![Python Support](https://img.shields.io/pypi/pyversions/sanicargs.svg)](https://pypi.python.org/pypi/sanicargs)\n\n# Sanicargs\nParses query parameters and json body parameters for [Sanic](https://github.com/channelcat/sanic) using type annotations.\n\n## Survey\nPlease fill out [this survey](https://docs.google.com/forms/d/e/1FAIpQLSdNLvB7NEJQhUyVdaZpBAgS0f1k9OywZp8xDqhaNY0rl-unZA/viewform?usp=sf_link) if you are using Sanicargs, we are gathering feedback :-)\n\n## Install\nInstall with pip\n```\n$ pip install sanicargs\n```\n\n## Usage\n\nUse the `parse_parameters` decorator to parse query parameters (GET) or body parameters (POST) and type cast them together with path params in [Sanic](https://github.com/channelcat/sanic)'s routes or blueprints like in this [example](https://github.com/trustpilot/python-sanicargs/tree/master/examples/simple.py) below:\n\n```python\nimport datetime\nfrom sanic import Sanic, response\nfrom sanicargs import parse_parameters\n\napp = Sanic(\"test_sanic_app\")\n\n@app.route(\"/me/<id>/birthdate\", methods=['GET'])\n@parse_parameters\nasync def test_datetime(req, id: str, birthdate: datetime.datetime):\n    return response.json({\n        'id': id, \n        'birthdate': birthdate.isoformat()\n    })\n\nif __name__ == \"__main__\":\n  app.run(host=\"0.0.0.0\", port=8080, access_log=False, debug=False)\n```\n\nTest it running with \n```bash\n$ curl 'http://0.0.0.0:8080/me/123/birthdate?birthdate=2017-10-30'\n```\n\n### Query parameters\n\n* **str** : `ex: ?message=hello world`\n* **int** : `ex: ?age=100`\n* **bool** : `ex: ?missing=false`\n* **datetime.datetime** : `ex: ?currentdate=2017-10-30T10:10:30 or 2017-10-30`\n* **datetime.date** : `ex: ?birthdate=2017-10-30`\n* **List[str]** : `ex: ?words=you,me,them,we`\n\n### JSON body parameters\n\n{\n  \"message\": \"hello word\",\n  \"age\": 100,\n  \"missing\": false,\n  \"currentDate\": \"2017-10-30\",\n  \"currentDateTime\": \"2017-10-30T10:10:30\",\n  \"words\": [\"you\", \"me\", \"them\", \"we\"]\n}\n\n### Note about datetimes\n\nDates and datetimes are parsed without timezone information giving you a \"naive datetime\" object. See the note on [datetime.timestamp()](https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp) about handling timezones if you require epoch format timestamps.\n\n### Important notice about decorators\n\nThe sequence of decorators is, as usual, important in Python.\n\nYou need to apply the `parse_parameters` decorator as the first one executed which means closest to the `def`.\n\n### `request` is mandatory!\n\nYou should always have request as the first argument in your function in order to use `parse_parameters`.\n\n**Note** that `request` arg can be renamed and even type-annotated as long as it is the first arg.",
    "bugtrack_url": null,
    "license": "",
    "summary": "Parses query args or body parameters in sanic using type annotations",
    "version": "3.0.1",
    "project_urls": {
        "Homepage": "https://github.com/trustpilot/python-sanicargs",
        "Repository": "https://github.com/trustpilot/python-sanicargs"
    },
    "split_keywords": [
        "sanicargs",
        "sanic",
        "query",
        "args",
        "type annotations"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7bb4d4000d63e9f78376615ee7f5da2161b230f4fbd5d85e0f8e613e60b0218e",
                "md5": "2fae09943c83fd00f1f4f1fa63248393",
                "sha256": "740eb48b6365065a5af196cbcd947db24c1e1d81970bc66918f6e51e0153347d"
            },
            "downloads": -1,
            "filename": "sanicargs-3.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2fae09943c83fd00f1f4f1fa63248393",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 4788,
            "upload_time": "2023-08-01T14:50:45",
            "upload_time_iso_8601": "2023-08-01T14:50:45.577983Z",
            "url": "https://files.pythonhosted.org/packages/7b/b4/d4000d63e9f78376615ee7f5da2161b230f4fbd5d85e0f8e613e60b0218e/sanicargs-3.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06c7abb3faa5f5e099b5049949d68a5e4c0304de82c2c1db9449f26d72f1567f",
                "md5": "d581e12d7dcf0f79a5cda135dde10da4",
                "sha256": "008ab6e9e96808db696e5abd037517df42b6f0716820c83b173ee3f640bb8827"
            },
            "downloads": -1,
            "filename": "sanicargs-3.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "d581e12d7dcf0f79a5cda135dde10da4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 4299,
            "upload_time": "2023-08-01T14:50:47",
            "upload_time_iso_8601": "2023-08-01T14:50:47.056687Z",
            "url": "https://files.pythonhosted.org/packages/06/c7/abb3faa5f5e099b5049949d68a5e4c0304de82c2c1db9449f26d72f1567f/sanicargs-3.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-01 14:50:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "trustpilot",
    "github_project": "python-sanicargs",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "sanicargs"
}
        
Elapsed time: 1.99706s