Skip to content

Commit

Permalink
Merge pull request #3 from trustpilot/request-can-be-renamed
Browse files Browse the repository at this point in the history
Request can be renamed
  • Loading branch information
deesouky authored Mar 1, 2018
2 parents 73fe31e + e3b1dec commit ed6fbc8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
7 changes: 6 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ History
------------------

* added test suite
* added path_param type casting
* added path_param type casting

1.1.0(2018-03-01)
------------------

* request can be renamed and even type-annotated
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ app = Sanic("test_sanic_app")

@app.route("/me/<id>/birthdate", methods=['GET'])
@parse_query_args
async def test_datetime(request, id: str, birthdate: datetime.datetime):
async def test_datetime(req, id: str, birthdate: datetime.datetime):
return response.json({
'id': id,
'birthdate': birthdate.isoformat()
Expand Down Expand Up @@ -53,4 +53,6 @@ You need to apply the `parse_query_args` decorator as the first one executed whi

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

**Note** that `request` arg can be renamed and even type-annotated as long as it is the first arg.
6 changes: 4 additions & 2 deletions sanicargs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ async def generate_csv(request, query: str, businessunitid: str):
(name, p.annotation, p.default)
for name, p in notations.parameters.items()
]
request_arg_name = inspect.getfullargspec(func)[0][0]


async def inner(request, *old_args, **route_parameters):
kwargs = {}
Expand All @@ -57,8 +59,8 @@ async def inner(request, *old_args, **route_parameters):
raw_value = request.args.get(name, None)

# provided in route
if name in route_parameters or name=="request":
if name=="request":
if name in route_parameters or name==request_arg_name:
if name==request_arg_name:
continue
raw_value = route_parameters[name]

Expand Down
2 changes: 1 addition & 1 deletion sanicargs/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0'
__version__ = '1.1.0'
6 changes: 4 additions & 2 deletions tests/test_sanicargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from sanicargs import parse_query_args, fields
from sanic.websocket import WebSocketProtocol
from sanic.exceptions import InvalidUsage
from sanic import request

import datetime


Expand Down Expand Up @@ -34,13 +36,13 @@ async def test_date(request, test: datetime.date):

@app.route("/list", methods=['GET'])
@parse_query_args
async def test_list(request, test: fields.List[str] = None):
async def test_list(req, test: fields.List[str] = None):
return response.json({'test': test})

@app.route("/all", methods=['GET'])
@parse_query_args
async def test_all(
request,
req: request,
a: int,
b: str,
c: datetime.datetime,
Expand Down

0 comments on commit ed6fbc8

Please sign in to comment.