Refactor default views to use register_routes
Opened this issue · 10 comments
It would be much cleaner if Datasette's default views were all registered using the new register_routes()
plugin hook. Could dramatically reduce the code in datasette/app.py
.
The ideal fix here would be to rework my
BaseView
subclass mechanism to work withregister_routes()
so that those views don't have any special privileges above plugin-provided views.
Originally posted by @simonw in #864 (comment)
This would be a lot easier if I had extracted out the hash logic to a plugin, see:
There's a lot of complex logic in the DataView
class, which handles conditionally returning content as .json
or as HTML or as .csv
.
That view subclasses AsgiView
which is itself request-aware, so maybe I don't need to reconsider how those classes work - just figure out how to hook them up with register_routes
.
Since AsgiRouter
is only used as the super-class of the DatasetteRouter
class maybe I should get rid of AsgiRouter
entirely - no point in having a Datasette-specific subclass of it if the parent class isn't ever used by anything else.
I could also rename it to just Router
which is a nicer name than DatasetteRouter
.
Maybe I could add a as_request_view
method as an alternative to as_asgi
:
datasette/datasette/utils/asgi.py
Lines 150 to 174 in a8bcafc
Or I could teach the Router
to spot the dispatch_request
method and call it directly.
I'm going to ditch that AsgiView
class too, by combining it into BaseView
.
So now the problem is simpler: I need to get BaseView
to a state where it can accept a shared request
object and it can be used in conjunction with register_routes()
.
This code is interesting:
Lines 948 to 955 in 3bc2461
I want to change the signature of that return await view(new_scope, receive, send)
method to instead take (request, send)
- so I can have a single shared request object that's created just once per HTTP request.
The problem is the scope modification: I have code that modifies the scope, but how should that impact a shared Request
instance? Should its .scope
be replaced with alternative scopes as it travels through the codebase?