Skip to content

Commit

Permalink
Added /-/actor.json - refs #699
Browse files Browse the repository at this point in the history
Also added JSON highlighting to introspection documentation.
  • Loading branch information
simonw committed Jun 1, 2020
1 parent 9315bac commit 1fc6cee
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
7 changes: 7 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ def _threads(self):
)
return d

def _actor(self, request):
return {"actor": request.scope.get("actor", None)}

def table_metadata(self, database, table):
"Fetch table-specific metadata."
return (
Expand Down Expand Up @@ -762,6 +765,10 @@ def add_route(view, regex):
JsonDataView.as_asgi(self, "databases.json", self._connected_databases),
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(
PatternPortfolioView.as_asgi(self), r"/-/patterns$",
)
Expand Down
8 changes: 6 additions & 2 deletions datasette/views/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
class JsonDataView(BaseView):
name = "json_data"

def __init__(self, datasette, filename, data_callback):
def __init__(self, datasette, filename, data_callback, needs_request=False):
self.ds = datasette
self.filename = filename
self.data_callback = data_callback
self.needs_request = needs_request

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:
headers = {}
if self.ds.cors:
Expand Down
44 changes: 37 additions & 7 deletions docs/introspection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,29 @@ Each of these pages can be viewed in your browser. Add ``.json`` to the URL to g
/-/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_url": "http://creativecommons.org/licenses/by/4.0/",
"source": "fivethirtyeight/data on GitHub",
"source_url": "https://github.com/fivethirtyeight/data",
"title": "Five Thirty Eight",
"databases": {...}
"databases": {
}
}
.. _JsonDataView_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": {
Expand Down Expand Up @@ -63,7 +69,9 @@ Shows the version of Datasette, Python and SQLite. `Versions example <https://la
/-/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
[
{
Expand All @@ -79,7 +87,9 @@ Shows a list of currently installed plugins and their versions. `Plugins example
/-/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,
Expand All @@ -95,7 +105,9 @@ Shows the :ref:`config` options for this instance of Datasette. `Config example
/-/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
[
{
Expand All @@ -113,7 +125,9 @@ Shows currently attached databases. `Databases example <https://latest.datasette
/-/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,
Expand All @@ -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>()]>>"
]
}
.. _JsonDataView_actor:

/-/actor
--------

Shows the currently authenticated actor. Useful for debugging Datasette authentication plugins.

.. code-block:: json
{
"actor": {
"id": 1,
"username": "some-user"
}
}
7 changes: 7 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,10 @@ async def test_permission_allowed(app_client, action, expected):
{"id": "actor"}, action, default=None
)
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

0 comments on commit 1fc6cee

Please sign in to comment.