Skip to content

Commit ed6fbc8

Browse files
authored
Merge pull request #3 from trustpilot/request-can-be-renamed
Request can be renamed
2 parents 73fe31e + e3b1dec commit ed6fbc8

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

HISTORY.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ History
1010
------------------
1111

1212
* added test suite
13-
* added path_param type casting
13+
* added path_param type casting
14+
15+
1.1.0(2018-03-01)
16+
------------------
17+
18+
* request can be renamed and even type-annotated

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ app = Sanic("test_sanic_app")
2222

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

5454
### `request` is mandatory!
5555

56-
You should always have request as the first argument in your function in order to use `parse_query_args`
56+
You should always have request as the first argument in your function in order to use `parse_query_args`.
57+
58+
**Note** that `request` arg can be renamed and even type-annotated as long as it is the first arg.

sanicargs/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ async def generate_csv(request, query: str, businessunitid: str):
4848
(name, p.annotation, p.default)
4949
for name, p in notations.parameters.items()
5050
]
51+
request_arg_name = inspect.getfullargspec(func)[0][0]
52+
5153

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

5961
# provided in route
60-
if name in route_parameters or name=="request":
61-
if name=="request":
62+
if name in route_parameters or name==request_arg_name:
63+
if name==request_arg_name:
6264
continue
6365
raw_value = route_parameters[name]
6466

sanicargs/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.0'
1+
__version__ = '1.1.0'

tests/test_sanicargs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from sanicargs import parse_query_args, fields
66
from sanic.websocket import WebSocketProtocol
77
from sanic.exceptions import InvalidUsage
8+
from sanic import request
9+
810
import datetime
911

1012

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

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

4042
@app.route("/all", methods=['GET'])
4143
@parse_query_args
4244
async def test_all(
43-
request,
45+
req: request,
4446
a: int,
4547
b: str,
4648
c: datetime.datetime,

0 commit comments

Comments
 (0)