diff --git a/datasette/app.py b/datasette/app.py index bff01bc1..f4be24cd 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -794,7 +794,7 @@ class Datasette: # Generate a regex snippet to match all registered renderer file extensions renderer_regex = "|".join(r"\." + key for key in self.renderers.keys()) - add_route(IndexView.as_asgi(self), r"/(?P(\.jsono?)?$)") + add_route(IndexView.as_view(self), r"/(?P(\.jsono?)?$)") # TODO: /favicon.ico and /-/static/ deserve far-future cache expires add_route(favicon, "/favicon.ico") @@ -819,62 +819,62 @@ class Datasette: ), ) add_route( - JsonDataView.as_asgi(self, "metadata.json", lambda: self._metadata), + JsonDataView.as_view(self, "metadata.json", lambda: self._metadata), r"/-/metadata(?P(\.json)?)$", ) add_route( - JsonDataView.as_asgi(self, "versions.json", self._versions), + JsonDataView.as_view(self, "versions.json", self._versions), r"/-/versions(?P(\.json)?)$", ) add_route( - JsonDataView.as_asgi( + JsonDataView.as_view( self, "plugins.json", self._plugins, needs_request=True ), r"/-/plugins(?P(\.json)?)$", ) add_route( - JsonDataView.as_asgi(self, "config.json", lambda: self._config), + JsonDataView.as_view(self, "config.json", lambda: self._config), r"/-/config(?P(\.json)?)$", ) add_route( - JsonDataView.as_asgi(self, "threads.json", self._threads), + JsonDataView.as_view(self, "threads.json", self._threads), r"/-/threads(?P(\.json)?)$", ) add_route( - JsonDataView.as_asgi(self, "databases.json", self._connected_databases), + JsonDataView.as_view(self, "databases.json", self._connected_databases), r"/-/databases(?P(\.json)?)$", ) add_route( - JsonDataView.as_asgi(self, "actor.json", self._actor, needs_request=True), + JsonDataView.as_view(self, "actor.json", self._actor, needs_request=True), r"/-/actor(?P(\.json)?)$", ) add_route( - AuthTokenView.as_asgi(self), r"/-/auth-token$", + AuthTokenView.as_view(self), r"/-/auth-token$", ) add_route( - PermissionsDebugView.as_asgi(self), r"/-/permissions$", + PermissionsDebugView.as_view(self), r"/-/permissions$", ) add_route( - MessagesDebugView.as_asgi(self), r"/-/messages$", + MessagesDebugView.as_view(self), r"/-/messages$", ) add_route( - PatternPortfolioView.as_asgi(self), r"/-/patterns$", + PatternPortfolioView.as_view(self), r"/-/patterns$", ) add_route( - DatabaseDownload.as_asgi(self), r"/(?P[^/]+?)(?P\.db)$" + DatabaseDownload.as_view(self), r"/(?P[^/]+?)(?P\.db)$" ) add_route( - DatabaseView.as_asgi(self), + DatabaseView.as_view(self), r"/(?P[^/]+?)(?P" + renderer_regex + r"|.jsono|\.csv)?$", ) add_route( - TableView.as_asgi(self), + TableView.as_view(self), r"/(?P[^/]+)/(?P[^/]+?$)", ) add_route( - RowView.as_asgi(self), + RowView.as_view(self), r"/(?P[^/]+)/(?P[^/]+?)/(?P[^/]+?)(?P" + renderer_regex + r")?$", @@ -952,7 +952,7 @@ class DatasetteRouter: if match is not None: new_scope = dict(scope, url_route={"kwargs": match.groupdict()}) try: - return await view(new_scope, receive, send) + return await view(Request(new_scope, receive), send) except NotFound as exception: return await self.handle_404(scope, receive, send, exception) except Exception as exception: diff --git a/datasette/utils/asgi.py b/datasette/utils/asgi.py index e1ccd17b..08c57b26 100644 --- a/datasette/utils/asgi.py +++ b/datasette/utils/asgi.py @@ -269,8 +269,8 @@ async def asgi_send_file( def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None): - async def inner_static(scope, receive, send): - path = scope["url_route"]["kwargs"]["path"] + async def inner_static(request, send): + path = request.scope["url_route"]["kwargs"]["path"] try: full_path = (Path(root_path) / path).resolve().absolute() except FileNotFoundError: diff --git a/datasette/views/base.py b/datasette/views/base.py index 72fb6b6b..280ae49d 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -120,17 +120,11 @@ class BaseView: ) @classmethod - def as_asgi(cls, *class_args, **class_kwargs): - async def view(scope, receive, send): - # Uses scope to create a request object, then dispatches that to - # self.get(...) or self.options(...) along with keyword arguments - # that were already tucked into scope["url_route"]["kwargs"] by - # the router, similar to how Django Channels works: - # https://channels.readthedocs.io/en/latest/topics/routing.html#urlrouter - request = Request(scope, receive) + def as_view(cls, *class_args, **class_kwargs): + async def view(request, send): self = view.view_class(*class_args, **class_kwargs) response = await self.dispatch_request( - request, **scope["url_route"]["kwargs"] + request, **request.scope["url_route"]["kwargs"] ) await response.asgi_send(send)