Skip to content

Commit

Permalink
Update examples for aiohttp 3.9 (#943)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
JCHacking and Dreamsorcerer committed Nov 18, 2023
1 parent 480971b commit 1ffc82e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
22 changes: 10 additions & 12 deletions demo/flash_messages_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
from typing import Awaitable, Callable, List, NoReturn, cast

from aiohttp import web
from aiohttp.typedefs import Handler
from cryptography import fernet

from aiohttp_session import get_session, setup
from aiohttp_session.cookie_storage import EncryptedCookieStorage

_Handler = Callable[[web.Request], Awaitable[web.StreamResponse]]


def flash(request: web.Request, message: str) -> None:
request.setdefault("flash_outgoing", []).append(message)
Expand All @@ -18,17 +17,16 @@ def get_messages(request: web.Request) -> List[str]:
return cast(List[str], request.pop("flash_incoming"))


async def flash_middleware(app: web.Application, handler: _Handler) -> _Handler:
async def process(request: web.Request) -> web.StreamResponse:
session = await get_session(request)
request["flash_incoming"] = session.pop("flash", [])
response = await handler(request)
@web.middleware
async def flash_middleware(request: web.Request, handler: Handler) -> web.StreamResponse:
session = await get_session(request)
request["flash_incoming"] = session.pop("flash", [])
try:
return await handler(request)
finally:
session["flash"] = request.get("flash_incoming", []) + request.get(
"flash_outgoing", []
)
return response

return process


async def flash_handler(request: web.Request) -> NoReturn:
Expand All @@ -50,10 +48,10 @@ def make_app() -> web.Application:
fernet_key = fernet.Fernet.generate_key()
secret_key = base64.urlsafe_b64decode(fernet_key)
setup(app, EncryptedCookieStorage(secret_key))
# Install flash middleware (must be installed after aiohttp-session middleware).
app.middlewares.append(flash_middleware)
app.router.add_get("/", handler)
app.router.add_get("/flash", flash_handler)
# Install flash middleware
app.middlewares.append(flash_middleware)
return app


Expand Down
5 changes: 3 additions & 2 deletions demo/login_required_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
]
_Handler = Callable[[web.Request], Awaitable[web.StreamResponse]]

user_key = web.AppKey("user", str)

def login_required(fn: _Handler) -> _Handler:
async def wrapped(
Expand All @@ -29,15 +30,15 @@ async def wrapped(
user_id = session["user_id"]
# actually load user from your database (e.g. with aiopg)
user = DATABASE[user_id]
app["user"] = user
app[user_key] = user
return await fn(request, *args, **kwargs)

return wrapped


@login_required
async def handler(request: web.Request) -> web.Response:
user = request.app["user"]
user = request.app[user_key]
return web.Response(text=f"User {user} authorized")


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-e .
aiohttp==3.8.5
aiohttp==3.9.0
aiomcache==0.8.1
cryptography==41.0.5
docker==6.1.3
Expand Down

0 comments on commit 1ffc82e

Please sign in to comment.