Skip to content

Commit

Permalink
⬆️ Bump Starlette from 0.22.0 to 0.23.0 (#5739)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
  • Loading branch information
Kludex and tiangolo authored Feb 8, 2023
1 parent 58757f6 commit 9293795
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs_src/app_testing/tutorial002.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async def read_main():
return {"msg": "Hello World"}


@app.websocket_route("/ws")
@app.websocket("/ws")
async def websocket(websocket: WebSocket):
await websocket.accept()
await websocket.send_json({"msg": "Hello WebSocket"})
Expand Down
33 changes: 33 additions & 0 deletions fastapi/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from starlette.datastructures import State
from starlette.exceptions import HTTPException
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.errors import ServerErrorMiddleware
from starlette.middleware.exceptions import ExceptionMiddleware
from starlette.requests import Request
Expand Down Expand Up @@ -870,3 +871,35 @@ def trace(
openapi_extra=openapi_extra,
generate_unique_id_function=generate_unique_id_function,
)

def websocket_route(
self, path: str, name: Union[str, None] = None
) -> Callable[[DecoratedCallable], DecoratedCallable]:
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.router.add_websocket_route(path, func, name=name)
return func

return decorator

def on_event(
self, event_type: str
) -> Callable[[DecoratedCallable], DecoratedCallable]:
return self.router.on_event(event_type)

def middleware(
self, middleware_type: str
) -> Callable[[DecoratedCallable], DecoratedCallable]:
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.add_middleware(BaseHTTPMiddleware, dispatch=func)
return func

return decorator

def exception_handler(
self, exc_class_or_status_code: Union[int, Type[Exception]]
) -> Callable[[DecoratedCallable], DecoratedCallable]:
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.add_exception_handler(exc_class_or_status_code, func)
return func

return decorator
37 changes: 37 additions & 0 deletions fastapi/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,25 @@ def __init__(
self.default_response_class = default_response_class
self.generate_unique_id_function = generate_unique_id_function

def route(
self,
path: str,
methods: Optional[List[str]] = None,
name: Optional[str] = None,
include_in_schema: bool = True,
) -> Callable[[DecoratedCallable], DecoratedCallable]:
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.add_route(
path,
func,
methods=methods,
name=name,
include_in_schema=include_in_schema,
)
return func

return decorator

def add_api_route(
self,
path: str,
Expand Down Expand Up @@ -686,6 +705,15 @@ def decorator(func: DecoratedCallable) -> DecoratedCallable:

return decorator

def websocket_route(
self, path: str, name: Union[str, None] = None
) -> Callable[[DecoratedCallable], DecoratedCallable]:
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.add_websocket_route(path, func, name=name)
return func

return decorator

def include_router(
self,
router: "APIRouter",
Expand Down Expand Up @@ -1247,3 +1275,12 @@ def trace(
openapi_extra=openapi_extra,
generate_unique_id_function=generate_unique_id_function,
)

def on_event(
self, event_type: str
) -> Callable[[DecoratedCallable], DecoratedCallable]:
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.add_event_handler(event_type, func)
return func

return decorator
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ classifiers = [
"Topic :: Internet :: WWW/HTTP",
]
dependencies = [
"starlette==0.22.0",
"starlette>=0.22.0,<=0.23.0",
"pydantic >=1.6.2,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0",
]
dynamic = ["version"]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_route_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ def test_websocket():

def test_websocket_invalid_path_doesnt_match():
with pytest.raises(WebSocketDisconnect):
with client.websocket_connect("/itemsx/portal-gun") as websocket:
websocket.receive_json()
with client.websocket_connect("/itemsx/portal-gun"):
pass

0 comments on commit 9293795

Please sign in to comment.