Prototyp of link rel=alternate header for tables

Refs https://github.com/simonw/datasette-notebook/issues/2
link-rel-alternate-header
Simon Willison 2021-11-27 12:08:42 -08:00
rodzic 48f11998b7
commit e0a84691c2
3 zmienionych plików z 19 dodań i 2 usunięć

Wyświetl plik

@ -1094,3 +1094,4 @@ async def derive_named_parameters(db, sql):
def add_cors_headers(headers):
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Headers"] = "Authorization"
headers["Access-Control-Expose-Headers"] = "Link"

Wyświetl plik

@ -137,10 +137,18 @@ class BaseView:
],
},
}
# Hacky cheat to add extra headers
headers = {}
if "_extra_headers" in context:
headers.update(context["_extra_headers"])
return Response.html(
await self.ds.render_template(
template, template_context, request=request, view_name=self.name
)
template,
template_context,
request=request,
view_name=self.name,
),
headers=headers,
)
@classmethod

Wyświetl plik

@ -17,6 +17,7 @@ from datasette.utils import (
is_url,
path_from_row_pks,
path_with_added_args,
path_with_format,
path_with_removed_args,
path_with_replaced_args,
to_css_class,
@ -958,6 +959,13 @@ class TableView(RowTableShared):
"metadata": metadata,
"view_definition": await db.get_view_definition(table),
"table_definition": await db.get_table_definition(table),
"_extra_headers": {
"Link": '{}; rel="alternate"; type="application/json+datasette"'.format(
self.ds.absolute_url(
request, path_with_format(request=request, format="json")
),
)
},
}
return (