Skip to content

Commit eb48048

Browse files
committed
Add HttpRequest.route_params property to access parsed route arguments
Fixes: #198
1 parent f72c0f2 commit eb48048

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed

azure/functions_worker/bindings/http.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ class HttpRequest(azf_abc.HttpRequest):
1515
__body_bytes: typing.Optional[bytes]
1616
__body_str: typing.Optional[str]
1717

18-
def __init__(self, method: str, url: str,
18+
def __init__(self, method: str, url: str, *,
1919
headers: typing.Mapping[str, str],
2020
params: typing.Mapping[str, str],
21+
route_params: typing.Mapping[str, str],
2122
body_type: meta.TypedDataKind,
2223
body: typing.Union[str, bytes]) -> None:
2324
self.__method = method
2425
self.__url = url
2526
self.__headers = azf_http.HttpRequestHeaders(headers)
2627
self.__params = types.MappingProxyType(params)
28+
self.__route_params = types.MappingProxyType(route_params)
2729
self.__body_type = body_type
2830

2931
if isinstance(body, str):
@@ -52,6 +54,10 @@ def headers(self):
5254
def params(self):
5355
return self.__params
5456

57+
@property
58+
def route_params(self):
59+
return self.__route_params
60+
5561
def get_body(self) -> bytes:
5662
if self.__body_bytes is None:
5763
assert self.__body_str is not None
@@ -147,5 +153,6 @@ def from_proto(cls, data: protos.TypedData, *,
147153
url=data.http.url,
148154
headers=data.http.headers,
149155
params=data.http.query,
156+
route_params=data.http.params,
150157
body_type=body_type,
151158
body=body)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def run(self):
225225
install_requires=[
226226
'grpcio~=1.14.0',
227227
'grpcio-tools~=1.14.0',
228-
'azure-functions==1.0.0a4',
228+
'azure-functions==1.0.0a5',
229229
],
230230
extras_require={
231231
'dev': [
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"scriptFile": "main.py",
3+
"disabled": false,
4+
"bindings": [
5+
{
6+
"type": "httpTrigger",
7+
"direction": "in",
8+
"name": "req",
9+
"route": "return_route_params/{param1}/{param2}"
10+
},
11+
{
12+
"type": "http",
13+
"direction": "out",
14+
"name": "$return"
15+
}
16+
]
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
import azure.functions
3+
4+
5+
def main(req: azure.functions.HttpRequest) -> str:
6+
return json.dumps(dict(req.route_params))

tests/test_http_functions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,9 @@ def test_unhandled_error(self):
153153
self.assertEqual(r.status_code, 500)
154154
# https://github.com/Azure/azure-functions-host/issues/2706
155155
# self.assertIn('Exception: ZeroDivisionError', r.text)
156+
157+
def test_return_route_params(self):
158+
r = self.webhost.request('GET', 'return_route_params/foo/bar')
159+
self.assertEqual(r.status_code, 200)
160+
resp = r.json()
161+
self.assertEqual(resp, {'param1': 'foo', 'param2': 'bar'})

tests/test_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ def test_http_request_bytes(self):
1414
'http://example.com/abc?a=1',
1515
headers=dict(aaa='zzz', bAb='xYz'),
1616
params=dict(a='b'),
17+
route_params={'route': 'param'},
1718
body_type=bind_meta.TypedDataKind.bytes,
1819
body=b'abc')
1920

2021
self.assertEqual(r.method, 'GET')
2122
self.assertEqual(r.url, 'http://example.com/abc?a=1')
2223
self.assertEqual(r.params, {'a': 'b'})
24+
self.assertEqual(r.route_params, {'route': 'param'})
2325

2426
with self.assertRaises(TypeError):
2527
r.params['a'] = 'z'
@@ -48,12 +50,14 @@ def test_http_request_json(self):
4850
'http://example.com/abc?a=1',
4951
headers={},
5052
params={},
53+
route_params={},
5154
body_type=bind_meta.TypedDataKind.json,
5255
body='{"a":1}')
5356

5457
self.assertEqual(r.method, 'POST')
5558
self.assertEqual(r.url, 'http://example.com/abc?a=1')
5659
self.assertEqual(r.params, {})
60+
self.assertEqual(r.route_params, {})
5761

5862
self.assertEqual(r.get_body(), b'{"a":1}')
5963
self.assertEqual(r.get_json(), {'a': 1})

0 commit comments

Comments
 (0)