Skip to content

jam.sessions.json

json

Classes:

Name Description
JSONSessions

Session management module for JSON storage.

JSONSessions

JSONSessions(
    json_path: str = "sessions.json",
    is_session_crypt: bool = False,
    session_aes_secret: Optional[bytes] = None,
    id_factory: Callable[[], str] = lambda: str(uuid4()),
)

Bases: BaseSessionModule

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())

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

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

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

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

get(session_id) -> Optional[dict]

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
Optional[dict]

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

rework

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
SessionNotFoundError

If session not found

Returns:

Name Type Description
str str

The new session ID.

update

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

Returns:

Type Description
None

None

FILE PATH: jam/sessions/json.py