Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field usage as post endpoint #14

Merged
merged 6 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@

# 2.0.3 (2020-03-18)

- re-release version since @sloev reacted a little too fast on deleting a release
- re-release version since @sloev reacted a little too fast on deleting a release

# 2.1.0 (2020-12-18)
sallas marked this conversation as resolved.
Show resolved Hide resolved

- Add parse_parameters decorator to support both GET(query params) and POST(body params) requests
- Start deprecated process of parse_query_args
sallas marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Build Status](https://travis-ci.org/trustpilot/python-sanicargs.svg?branch=master)](https://travis-ci.org/trustpilot/python-sanicargs) [![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 args in [Sanic](https://github.com/channelcat/sanic) using type annotations.
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 :-)
Expand All @@ -14,17 +14,17 @@ $ pip install sanicargs

## Usage

Use the `parse_query_args` decorator to parse query args and type cast query args and path params with [Sanic](https://github.com/channelcat/sanic)'s routes or blueprints like in the [example](https://github.com/trustpilot/python-sanicargs/tree/master/examples/simple.py) below:
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_query_args
from sanicargs import parse_parameters

app = Sanic("test_sanic_app")

@app.route("/me/<id>/birthdate", methods=['GET'])
@parse_query_args
@parse_parameters
async def test_datetime(req, id: str, birthdate: datetime.datetime):
return response.json({
'id': id,
Expand All @@ -40,7 +40,7 @@ Test it running with
$ curl 'http://0.0.0.0:8080/me/123/birthdate?birthdate=2017-10-30'
```

### Fields
### Query parameters

* **str** : `ex: ?message=hello world`
* **int** : `ex: ?age=100`
Expand All @@ -49,6 +49,17 @@ $ curl 'http://0.0.0.0:8080/me/123/birthdate?birthdate=2017-10-30'
* **datetime.date** : `ex: ?birthdate=2017-10-30`
* **List[str]** : `ex: ?words=you,me,them,we`

### JSON body parameters
sallas marked this conversation as resolved.
Show resolved Hide resolved

{
"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.
Expand All @@ -57,10 +68,15 @@ Dates and datetimes are parsed without timezone information giving you a "naive

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`.
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_query_args`.
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
sallas marked this conversation as resolved.
Show resolved Hide resolved
Loading