Add actor= parameter to datasette.client methods (#2688)

`datasette.client.get(path, actor={"id": "root"}` now makes the internal request with that actor as `request.actor` - same for the other HTTP verb methods on `datasette.client`.

Upgraded relevant tests to use the new `actor=` mechanism.
This commit is contained in:
Simon Willison 2026-04-14 18:31:57 -07:00 • committed by GitHub
commit 9c164572d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 149 additions and 53 deletions

View file

@ -1312,6 +1312,28 @@ These methods can be used with :ref:`internals_datasette_urls` - for example:
For documentation on available ``**kwargs`` options and the shape of the HTTPX Response object refer to the `HTTPX Async documentation <https://www.python-httpx.org/async/>`__.
.. _internals_datasette_client_actor:
Authenticating as an actor
~~~~~~~~~~~~~~~~~~~~~~~~~~
All ``datasette.client`` methods accept an optional ``actor=`` parameter. When set to a dictionary describing an actor, the request is made with a signed ``ds_actor`` cookie identifying that actor — as if the request had been made by a user who is signed in as that actor.
This is a convenient shorthand equivalent to signing the cookie manually using ``datasette.client.actor_cookie()``.
Example usage:
.. code-block:: python
response = await datasette.client.get(
"/-/actor.json", actor={"id": "root"}
)
assert response.json() == {"actor": {"id": "root"}}
This parameter works with all HTTP methods (``get``, ``post``, ``put``, ``patch``, ``delete``, ``options``, ``head``) and the generic ``request`` method.
Passing both ``actor=`` and a ``ds_actor`` cookie via ``cookies=`` raises a ``TypeError``. Other unrelated cookies can be combined with ``actor=``.
Bypassing permission checks
~~~~~~~~~~~~~~~~~~~~~~~~~~~