Added /-/actor.json - refs #699

Also added JSON highlighting to introspection documentation.
pull/703/head
Simon Willison 2020-05-30 18:51:00 -07:00
rodzic 9315bacf6f
commit 1fc6ceefb9
4 zmienionych plików z 57 dodań i 9 usunięć

Wyświetl plik

@ -586,6 +586,9 @@ class Datasette:
) )
return d return d
def _actor(self, request):
return {"actor": request.scope.get("actor", None)}
def table_metadata(self, database, table): def table_metadata(self, database, table):
"Fetch table-specific metadata." "Fetch table-specific metadata."
return ( return (
@ -762,6 +765,10 @@ class Datasette:
JsonDataView.as_asgi(self, "databases.json", self._connected_databases), JsonDataView.as_asgi(self, "databases.json", self._connected_databases),
r"/-/databases(?P<as_format>(\.json)?)$", r"/-/databases(?P<as_format>(\.json)?)$",
) )
add_route(
JsonDataView.as_asgi(self, "actor.json", self._actor, needs_request=True),
r"/-/actor(?P<as_format>(\.json)?)$",
)
add_route( add_route(
PatternPortfolioView.as_asgi(self), r"/-/patterns$", PatternPortfolioView.as_asgi(self), r"/-/patterns$",
) )

Wyświetl plik

@ -6,13 +6,17 @@ from .base import BaseView
class JsonDataView(BaseView): class JsonDataView(BaseView):
name = "json_data" name = "json_data"
def __init__(self, datasette, filename, data_callback): def __init__(self, datasette, filename, data_callback, needs_request=False):
self.ds = datasette self.ds = datasette
self.filename = filename self.filename = filename
self.data_callback = data_callback self.data_callback = data_callback
self.needs_request = needs_request
async def get(self, request, as_format): async def get(self, request, as_format):
data = self.data_callback() if self.needs_request:
data = self.data_callback(request)
else:
data = self.data_callback()
if as_format: if as_format:
headers = {} headers = {}
if self.ds.cors: if self.ds.cors:

Wyświetl plik

@ -10,7 +10,9 @@ Each of these pages can be viewed in your browser. Add ``.json`` to the URL to g
/-/metadata /-/metadata
----------- -----------
Shows the contents of the ``metadata.json`` file that was passed to ``datasette serve``, if any. `Metadata example <https://fivethirtyeight.datasettes.com/-/metadata>`_:: Shows the contents of the ``metadata.json`` file that was passed to ``datasette serve``, if any. `Metadata example <https://fivethirtyeight.datasettes.com/-/metadata>`_:
.. code-block:: json
{ {
"license": "CC Attribution 4.0 License", "license": "CC Attribution 4.0 License",
@ -18,7 +20,9 @@ Shows the contents of the ``metadata.json`` file that was passed to ``datasette
"source": "fivethirtyeight/data on GitHub", "source": "fivethirtyeight/data on GitHub",
"source_url": "https://github.com/fivethirtyeight/data", "source_url": "https://github.com/fivethirtyeight/data",
"title": "Five Thirty Eight", "title": "Five Thirty Eight",
"databases": {...} "databases": {
}
} }
.. _JsonDataView_versions: .. _JsonDataView_versions:
@ -26,7 +30,9 @@ Shows the contents of the ``metadata.json`` file that was passed to ``datasette
/-/versions /-/versions
----------- -----------
Shows the version of Datasette, Python and SQLite. `Versions example <https://latest.datasette.io/-/versions>`_:: Shows the version of Datasette, Python and SQLite. `Versions example <https://latest.datasette.io/-/versions>`_:
.. code-block:: json
{ {
"datasette": { "datasette": {
@ -63,7 +69,9 @@ Shows the version of Datasette, Python and SQLite. `Versions example <https://la
/-/plugins /-/plugins
---------- ----------
Shows a list of currently installed plugins and their versions. `Plugins example <https://san-francisco.datasettes.com/-/plugins>`_:: Shows a list of currently installed plugins and their versions. `Plugins example <https://san-francisco.datasettes.com/-/plugins>`_:
.. code-block:: json
[ [
{ {
@ -79,7 +87,9 @@ Shows a list of currently installed plugins and their versions. `Plugins example
/-/config /-/config
--------- ---------
Shows the :ref:`config` options for this instance of Datasette. `Config example <https://fivethirtyeight.datasettes.com/-/config>`_:: Shows the :ref:`config` options for this instance of Datasette. `Config example <https://fivethirtyeight.datasettes.com/-/config>`_:
.. code-block:: json
{ {
"default_facet_size": 30, "default_facet_size": 30,
@ -95,7 +105,9 @@ Shows the :ref:`config` options for this instance of Datasette. `Config example
/-/databases /-/databases
------------ ------------
Shows currently attached databases. `Databases example <https://latest.datasette.io/-/config>`_:: Shows currently attached databases. `Databases example <https://latest.datasette.io/-/config>`_:
.. code-block:: json
[ [
{ {
@ -113,7 +125,9 @@ Shows currently attached databases. `Databases example <https://latest.datasette
/-/threads /-/threads
---------- ----------
Shows details of threads and ``asyncio`` tasks. `Threads example <https://latest.datasette.io/-/threads>`_:: Shows details of threads and ``asyncio`` tasks. `Threads example <https://latest.datasette.io/-/threads>`_:
.. code-block:: json
{ {
"num_threads": 2, "num_threads": 2,
@ -136,3 +150,19 @@ Shows details of threads and ``asyncio`` tasks. `Threads example <https://latest
"<Task pending coro=<LifespanOn.main() running at uvicorn/lifespan/on.py:48> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10364f050>()]>>" "<Task pending coro=<LifespanOn.main() running at uvicorn/lifespan/on.py:48> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10364f050>()]>>"
] ]
} }
.. _JsonDataView_actor:
/-/actor
--------
Shows the currently authenticated actor. Useful for debugging Datasette authentication plugins.
.. code-block:: json
{
"actor": {
"id": 1,
"username": "some-user"
}
}

Wyświetl plik

@ -539,3 +539,10 @@ async def test_permission_allowed(app_client, action, expected):
{"id": "actor"}, action, default=None {"id": "actor"}, action, default=None
) )
assert expected == actual assert expected == actual
def test_actor_json(app_client):
assert {"actor": None} == app_client.get("/-/actor.json").json
assert {"actor": {"id": "bot2", "1+1": 2}} == app_client.get(
"/-/actor.json/?_bot2=1"
).json