Skip to content

Commit

Permalink
request.url_vars property, closes #822
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 9, 2020
1 parent db660db commit fac8e93
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions datasette/utils/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def url(self):
(self.scheme, self.host, self.path, None, self.query_string, None)
)

@property
def url_vars(self):
return (self.scope.get("url_route") or {}).get("kwargs") or {}

@property
def scheme(self):
return self.scope.get("scheme") or "http"
Expand Down
3 changes: 3 additions & 0 deletions docs/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ The request object is passed to various plugin hooks. It represents an incoming
``.args`` - MultiParams
An object representing the parsed querystring parameters, see below.

``.url_vars`` - dictionary (str -> str)
Variables extracted from the URL path, if that path was defined using a regular expression. See :ref:`plugin_register_routes`.

``.actor`` - dictionary (str -> Any) or None
The currently authenticated actor (see :ref:`actors <authentication_actor>`), or ``None`` if the request is unauthenticated.

Expand Down
4 changes: 2 additions & 2 deletions docs/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,8 @@ Return a list of ``(regex, async_view_function)`` pairs, something like this:
import html
async def hello_from(scope):
name = scope["url_route"]["kwargs"]["name"]
async def hello_from(request):
name = request.url_vars["name"]
return Response.html("Hello from {}".format(
html.escape(name)
))
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/my_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ async def one(datasette):
(await datasette.get_database().execute("select 1 + 1")).first()[0]
)

async def two(request, scope):
name = scope["url_route"]["kwargs"]["name"]
async def two(request):
name = request.url_vars["name"]
greeting = request.args.get("greeting")
return Response.text("{} {}".format(greeting, name))

Expand Down
17 changes: 17 additions & 0 deletions tests/test_internals_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,20 @@ def test_request_args():
assert 2 == len(request.args)
with pytest.raises(KeyError):
request.args["missing"]


def test_request_url_vars():
scope = {
"http_version": "1.1",
"method": "POST",
"path": "/",
"raw_path": b"/",
"query_string": b"",
"scheme": "http",
"type": "http",
"headers": [[b"content-type", b"application/x-www-form-urlencoded"]],
}
assert {} == Request(scope, None).url_vars
assert {"name": "cleo"} == Request(
dict(scope, url_route={"kwargs": {"name": "cleo"}}), None
).url_vars

0 comments on commit fac8e93

Please sign in to comment.