Async support for magic parameters

Closes #2441
This commit is contained in:
Simon Willison 2024-11-15 13:17:45 -08:00
commit dce718961c
4 changed files with 38 additions and 4 deletions

View file

@ -1315,7 +1315,7 @@ Magic parameters all take this format: ``_prefix_rest_of_parameter``. The prefix
To register a new function, return it as a tuple of ``(string prefix, function)`` from this hook. The function you register should take two arguments: ``key`` and ``request``, where ``key`` is the ``rest_of_parameter`` portion of the parameter and ``request`` is the current :ref:`internals_request`.
This example registers two new magic parameters: ``:_request_http_version`` returning the HTTP version of the current request, and ``:_uuid_new`` which returns a new UUID:
This example registers two new magic parameters: ``:_request_http_version`` returning the HTTP version of the current request, and ``:_uuid_new`` which returns a new UUID. It also registers an `:_asynclookup_key` parameter, demonstrating that these functions can be asynchronous:
.. code-block:: python
@ -1337,11 +1337,15 @@ This example registers two new magic parameters: ``:_request_http_version`` retu
raise KeyError
async def asynclookup(key, request):
return await do_something_async(key)
@hookimpl
def register_magic_parameters(datasette):
return [
("request", request),
("uuid", uuid),
("asynclookup", asynclookup),
]
.. _plugin_hook_forbidden: