SessionConfig¶
SessionConfig is simple set of configurations that when passed enables the built-in middleware of Esmerald.
When a SessionConfig object is passed to an application instance, it will automatically start the SessionMiddleware
.
Tip
More information about HTTP Sessions here.
SessionConfig and application¶
To use the SessionConfig in an application instance.
from esmerald import Esmerald, SessionConfig, settings
session_config = SessionConfig(
secret_key=settings.secret_key,
)
app = Esmerald(session_config=session_config)
Another example
from esmerald import Esmerald, SessionConfig, settings
session_config = SessionConfig(
secret_key=settings.secret_key,
session_cookie="session",
)
app = Esmerald(session_config=session_config)
Parameters¶
All the parameters and defaults are available in the SessionConfig Reference.
SessionConfig and application settings¶
The SessionConfig can be done directly via application instantiation but also via settings.
from esmerald import EsmeraldAPISettings, ImproperlyConfigured, SessionConfig
class CustomSettings(EsmeraldAPISettings):
@property
def session_config(self) -> SessionConfig:
"""
Initial Default configuration for the SessionConfig.
This can be overwritten in another setting or simply override
`session_config` or then override the `def session_config()`
property to change the behavior of the whole session_config.
"""
if not self.secret_key:
raise ImproperlyConfigured("`secret` setting not configured.")
return SessionConfig(
secret_key=self.secret_key,
session_cookie="session",
)
This will make sure you keep the settings clean, separated and without a bloated Esmerald instance.
Esmerald Sessions¶
If you don't want to use the built-in session configuration and if you fancy a more custom way of handling the sessions with Esmerald, there is an official package Esmerald Sessions that can help you with that including the middleware.