Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CBV pattern #35

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions starlette/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from starlette.decorators import asgi_application
from starlette.decorators import make_asgi
from starlette.response import (
FileResponse,
HTMLResponse,
Expand All @@ -12,7 +12,7 @@


__all__ = (
"asgi_application",
"make_asgi",
"FileResponse",
"HTMLResponse",
"JSONResponse",
Expand Down
3 changes: 1 addition & 2 deletions starlette/decorators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import asyncio
from starlette.request import Request
from starlette.response import Response
from starlette.types import ASGIInstance, Receive, Send, Scope


def asgi_application(func):
def make_asgi(func):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kinda thinking that we'll drop the asgi_application/make_asgi decorator altogether - I think it just muddies things up.

Let's leave this alone for now, but perhaps don't use it in the PR, and include any logic directly in the class.

is_coroutine = asyncio.iscoroutinefunction(func)

def app(scope: Scope) -> ASGIInstance:
Expand Down
10 changes: 9 additions & 1 deletion starlette/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def __call__(self, scope: Scope) -> ASGIInstance:


class Router:
def __init__(self, routes: typing.List[Route], default: ASGIApp = None) -> None:
def __init__(
self, routes: typing.List[Route] = [], default: ASGIApp = None
) -> None:
self.routes = routes
self.default = self.not_found if default is None else default

Expand All @@ -80,5 +82,11 @@ def __call__(self, scope: Scope) -> ASGIInstance:
return route(child_scope)
return self.not_found(scope)

def add_route(
self, path: str, *, app: ASGIApp, methods: typing.Sequence[str] = ()
) -> None:
route = Path(path=path, app=app, methods=methods)
self.routes.append(route)

def not_found(self, scope: Scope) -> ASGIInstance:
return Response("Not found", 404, media_type="text/plain")
28 changes: 28 additions & 0 deletions starlette/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import asyncio

from starlette.request import Request
from starlette.types import ASGIApp, ASGIInstance, Receive, Send, Scope


class View:
def __call__(self, scope: Scope) -> ASGIApp:
return self.dispatch(scope)

def dispatch(self, scope: Scope) -> ASGIInstance:
request_method = scope["method"] if scope["method"] != "HEAD" else "GET"
func = getattr(self, request_method.lower(), None)
if func is None:
raise Exception(
f"Method {request_method} is not implemented for this view."
)
is_coroutine = asyncio.iscoroutinefunction(func)

async def awaitable(receive: Receive, send: Send) -> None:
request = Request(scope, receive)
if is_coroutine:
response = await func(request)
else:
response = func(request)
await response(receive, send)

return awaitable