mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Refactored AsgiView into BaseView, refs #870
This commit is contained in:
parent
a8bcafc177
commit
3bc2461c77
2 changed files with 25 additions and 30 deletions
|
|
@ -147,33 +147,6 @@ class AsgiLifespan:
|
||||||
await self.app(scope, receive, send)
|
await self.app(scope, receive, send)
|
||||||
|
|
||||||
|
|
||||||
class AsgiView:
|
|
||||||
async def dispatch_request(self, request, *args, **kwargs):
|
|
||||||
handler = getattr(self, request.method.lower(), None)
|
|
||||||
return await handler(request, *args, **kwargs)
|
|
||||||
|
|
||||||
@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)
|
|
||||||
self = view.view_class(*class_args, **class_kwargs)
|
|
||||||
response = await self.dispatch_request(
|
|
||||||
request, **scope["url_route"]["kwargs"]
|
|
||||||
)
|
|
||||||
await response.asgi_send(send)
|
|
||||||
|
|
||||||
view.view_class = cls
|
|
||||||
view.__doc__ = cls.__doc__
|
|
||||||
view.__module__ = cls.__module__
|
|
||||||
view.__name__ = cls.__name__
|
|
||||||
return view
|
|
||||||
|
|
||||||
|
|
||||||
class AsgiStream:
|
class AsgiStream:
|
||||||
def __init__(self, stream_fn, status=200, headers=None, content_type="text/plain"):
|
def __init__(self, stream_fn, status=200, headers=None, content_type="text/plain"):
|
||||||
self.stream_fn = stream_fn
|
self.stream_fn = stream_fn
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,9 @@ from datasette.utils import (
|
||||||
from datasette.utils.asgi import (
|
from datasette.utils.asgi import (
|
||||||
AsgiStream,
|
AsgiStream,
|
||||||
AsgiWriter,
|
AsgiWriter,
|
||||||
AsgiView,
|
|
||||||
Forbidden,
|
Forbidden,
|
||||||
NotFound,
|
NotFound,
|
||||||
|
Request,
|
||||||
Response,
|
Response,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -55,7 +55,7 @@ class DatasetteError(Exception):
|
||||||
self.messagge_is_html = messagge_is_html
|
self.messagge_is_html = messagge_is_html
|
||||||
|
|
||||||
|
|
||||||
class BaseView(AsgiView):
|
class BaseView:
|
||||||
ds = None
|
ds = None
|
||||||
|
|
||||||
async def head(self, *args, **kwargs):
|
async def head(self, *args, **kwargs):
|
||||||
|
|
@ -90,7 +90,8 @@ class BaseView(AsgiView):
|
||||||
)
|
)
|
||||||
except BadSignature:
|
except BadSignature:
|
||||||
pass
|
pass
|
||||||
response = await super().dispatch_request(request, *args, **kwargs)
|
handler = getattr(self, request.method.lower(), None)
|
||||||
|
response = await handler(request, *args, **kwargs)
|
||||||
if self.ds:
|
if self.ds:
|
||||||
self.ds._write_messages_to_response(request, response)
|
self.ds._write_messages_to_response(request, response)
|
||||||
return response
|
return response
|
||||||
|
|
@ -118,6 +119,27 @@ class BaseView(AsgiView):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@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)
|
||||||
|
self = view.view_class(*class_args, **class_kwargs)
|
||||||
|
response = await self.dispatch_request(
|
||||||
|
request, **scope["url_route"]["kwargs"]
|
||||||
|
)
|
||||||
|
await response.asgi_send(send)
|
||||||
|
|
||||||
|
view.view_class = cls
|
||||||
|
view.__doc__ = cls.__doc__
|
||||||
|
view.__module__ = cls.__module__
|
||||||
|
view.__name__ = cls.__name__
|
||||||
|
return view
|
||||||
|
|
||||||
|
|
||||||
class DataView(BaseView):
|
class DataView(BaseView):
|
||||||
name = ""
|
name = ""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue