Skip to content

jam.aio.sessions.json

json

Classes:

Name Description
JSONSessions

Async session management module for JSON storage.

JSONSessions

JSONSessions(
    json_path: str = "sessions.json",
    is_session_crypt: bool = False,
    session_aes_secret: bytes | None = None,
    id_factory: Callable[[], str] = lambda: str(uuid4()),
    serializer: BaseEncoder
    | type[BaseEncoder] = JsonEncoder,
    logger: BaseLogger | None = None,
)

Bases: BaseAsyncSessionModule

Async session management module for JSON storage.

Parameters:

Name Type Description Default
json_path str

Path to the JSON file where sessions will be stored.

'sessions.json'
is_session_crypt bool

If True, session keys will be encoded.

False
session_aes_secret Optional[bytes]

AES secret for encoding session keys. Required if is_session_crypt is True.

None
id_factory Callable[[], str]

A callable that generates unique IDs. Defaults to a UUID factory.

lambda: str(uuid4())
serializer Union[BaseEncoder, type[BaseEncoder]]

JSON encoder/decoder. Defaults to JsonEncoder.

JsonEncoder
logger Optional[BaseLogger]

Logger instance. Defaults to None.

None

Methods:

Name Description
clear

Clear all sessions for a given session key.

create

Create a new session.

delete

Delete a session by its ID.

get

Retrieve session data by session ID.

rework

Rework (regenerate) a session ID.

update

Update session data by its ID.

clear async

clear(session_key: str) -> None

Clear all sessions for a given session key.

Parameters:

Name Type Description Default
session_key str

The session key to clear.

required

Returns:

Type Description
None

None

create async

create(session_key: str, data: dict) -> str

Create a new session.

Parameters:

Name Type Description Default
session_key str

The session key.

required
data dict

The data to be stored in the session.

required

Returns:

Name Type Description
str str

The ID of the created session.

delete async

delete(session_id: str) -> None

Delete a session by its ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session to delete.

required

Returns:

Type Description
None

None

get async

get(session_id) -> dict | None

Retrieve session data by session ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session to retrieve.

required

Returns:

Type Description
dict | None

dict | None: The session data if found, otherwise None.

rework async

rework(session_id: str) -> str

Rework (regenerate) a session ID.

Parameters:

Name Type Description Default
session_id str

The current session ID to be reworked.

required

Raises:

Type Description
JamSessionNotFound

If session not found

Returns:

Name Type Description
str str

The new session ID.

update async

update(session_id: str, data: dict) -> None

Update session data by its ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session to update.

required
data dict

The new data to store in the session.

required

Raises:

Type Description
JamSessionNotFound

If session not found

Returns:

Type Description
None

None