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

Add Sanic Extension support #89

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
33 changes: 1 addition & 32 deletions sanic_session/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .aioredis import AIORedisSessionInterface
from .extension import Session
from .memcache import MemcacheSessionInterface
from .memory import InMemorySessionInterface
from .mongodb import MongoDBSessionInterface
Expand All @@ -12,35 +13,3 @@
"AIORedisSessionInterface",
"Session",
)


class Session:
def __init__(self, app=None, interface=None):
self.interface = None
if app:
self.init_app(app, interface)

def init_app(self, app, interface):
self.interface = interface or InMemorySessionInterface()
if not hasattr(app.ctx, "extensions"):
app.ctx.extensions = {}

app.ctx.extensions[
self.interface.session_name
] = self # session_name defaults to 'session'

# @app.middleware('request')
async def add_session_to_request(request):
"""Before each request initialize a session
using the client's request."""
await self.interface.open(request)

# @app.middleware('response')
async def save_session(request, response):
"""After each request save the session, pass
the response to set client cookies.
"""
await self.interface.save(request, response)

app.request_middleware.appendleft(add_session_to_request)
app.response_middleware.append(save_session)
49 changes: 49 additions & 0 deletions sanic_session/extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from .memory import InMemorySessionInterface

try:
from sanic_ext import Extension

SANIC_EXTENSIONS = True
except ImportError:
Extension = type("Extension", (), {}) # type: ignore
SANIC_EXTENSIONS = False


class Session(Extension):
name = "session"

def __init__(self, app=None, interface=None):
self.interface = interface
if app:
self.init_app(app, interface)

def init_app(self, app, interface):
self.interface = interface or InMemorySessionInterface()
if not hasattr(app.ctx, "extensions"):
app.ctx.extensions = {}

app.ctx.extensions[
self.interface.session_name
] = self # session_name defaults to 'session'

async def add_session_to_request(request):
"""Before each request initialize a session
using the client's request."""
await self.interface.open(request)

async def save_session(request, response):
"""After each request save the session, pass
the response to set client cookies.
"""
await self.interface.save(request, response)

app.request_middleware.appendleft(add_session_to_request)
app.response_middleware.append(save_session)

def startup(self, _) -> None:
if not SANIC_EXTENSIONS:
raise RuntimeError("Sanic Extensions is not installed")
self.init_app(self.app, self.interface)

def label(self):
return self.interface.__class__.__name__