mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Added asgi_wrapper plugin hook, closes #520
This commit is contained in:
parent
b9ede4c189
commit
4d2fdafe39
5 changed files with 78 additions and 1 deletions
|
|
@ -372,6 +372,7 @@ def render_cell(value, column, table, database, datasette):
|
|||
|
||||
PLUGIN2 = """
|
||||
from datasette import hookimpl
|
||||
from functools import wraps
|
||||
import jinja2
|
||||
import json
|
||||
|
||||
|
|
@ -413,6 +414,28 @@ def render_cell(value, database):
|
|||
label=jinja2.escape(data["label"] or "") or " "
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@hookimpl
|
||||
def asgi_wrapper(datasette):
|
||||
def wrap_with_databases_header(app):
|
||||
@wraps(app)
|
||||
async def add_x_databases_header(scope, recieve, send):
|
||||
async def wrapped_send(event):
|
||||
if event["type"] == "http.response.start":
|
||||
original_headers = event.get("headers") or []
|
||||
event = {
|
||||
"type": event["type"],
|
||||
"status": event["status"],
|
||||
"headers": original_headers + [
|
||||
[b"x-databases",
|
||||
", ".join(datasette.databases.keys()).encode("utf-8")]
|
||||
],
|
||||
}
|
||||
await send(event)
|
||||
await app(scope, recieve, wrapped_send)
|
||||
return add_x_databases_header
|
||||
return wrap_with_databases_header
|
||||
"""
|
||||
|
||||
TABLES = (
|
||||
|
|
|
|||
|
|
@ -162,3 +162,8 @@ def test_plugins_extra_body_script(app_client, path, expected_extra_body_script)
|
|||
json_data = r.search(app_client.get(path).body.decode("utf8")).group(1)
|
||||
actual_data = json.loads(json_data)
|
||||
assert expected_extra_body_script == actual_data
|
||||
|
||||
|
||||
def test_plugins_asgi_wrapper(app_client):
|
||||
response = app_client.get("/fixtures")
|
||||
assert "fixtures" == response.headers["x-databases"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue