Skip to content

jam.sessions.base

__base__

Classes:

Name Description
BaseSessionModule

Abstract base class for session management modules.

BaseSessionModule

BaseSessionModule(
    id_factory: Callable[[], str] = lambda: str(uuid4()),
    is_session_crypt: bool = False,
    session_aes_secret: bytes | str | None = None,
    serializer: type[BaseEncoder]
    | BaseEncoder = JsonEncoder,
    logger: BaseLogger | None = None,
)

Bases: ABC

Abstract base class for session management modules.

You can create your own module for sessions. For example:

from jam.sessions import BaseSessionModule

class CustomSessionModule(BaseSessionModule):
    def __init__(self, some_param: str) -> None:
        super().__init__(
            is_session_crypt=True,
            session_aes_secret=b'your-32-byte-base64-encoded-key'
        )

    # Your initialization code here
    def create(session_key: str) -> str:
        ...

    def get(session_id: str) -> dict:
        ...

    def delete(session_id: str) -> None:
        ...

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

    def rework(session_id: str) -> str:
        ...

    def clear(session_key: str) -> None:
        ...

Parameters:

Name Type Description Default
id_factory Callable[str]

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

lambda: str(uuid4())
is_session_crypt bool

If True, session keys will be encoded. Defaults to False.

False
session_aes_secret Optional[bytes, str]

AES secret for encoding session keys.

None
serializer Union[BaseEncoder, type[BaseEncoder]]

JSON encoder/decoder. Defaults to JsonEncoder.

JsonEncoder
logger Optional[BaseLogger]

Logger instance. Defaults to None.

None

Raises:

Type Description
JamSessionEmptyAESKey

If 'is_session_crypt' is True and 'session_aes_secret' is not provided.

Methods:

Name Description
__decode_session_data__

Decode session data.

__decode_session_id__

Decode the session using AES decryption.

__decode_session_id_if_needed__

Decode the session ID if it is encoded.

__encode_session_data__

Encode session data.

__encode_session_id__

Encode the session using AES encryption.

__encode_session_id_if_needed__

Encode the session ID if it is not already encoded.

clear

Clear all sessions by key.

create

Create a new session with the given session key and data.

delete

Delete a session by its key or ID.

get

Retrieve a session by its key or ID.

rework

Rework a session and return its new ID.

update

Update an existing session with new data.

Attributes:

Name Type Description
id str

Return the unique ID use id_factory.

id property

id: str

Return the unique ID use id_factory.

__decode_session_data__

__decode_session_data__(data: str) -> dict

Decode session data.

__decode_session_id__

__decode_session_id__(data: str) -> str

Decode the session using AES decryption.

__decode_session_id_if_needed__

__decode_session_id_if_needed__(data: str) -> str

Decode the session ID if it is encoded.

__encode_session_data__

__encode_session_data__(data: dict) -> str

Encode session data.

__encode_session_id__

__encode_session_id__(data: str) -> str

Encode the session using AES encryption.

__encode_session_id_if_needed__

__encode_session_id_if_needed__(data: str) -> str

Encode the session ID if it is not already encoded.

clear abstractmethod

clear(session_key: str) -> None

Clear all sessions by key.

Parameters:

Name Type Description Default
session_key str

The key for the sessions to clear.

required

create abstractmethod

create(session_key: str, data: dict[str, Any]) -> str

Create a new session with the given session key and data.

Parameters:

Name Type Description Default
session_key str

The key for the session.

required
data dict

The data to be stored in the session.

required

delete abstractmethod

delete(session_id: str) -> None

Delete a session by its key or ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session.

required

get abstractmethod

get(session_id: str) -> dict[str, Any] | None

Retrieve a session by its key or ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session.

required

Returns:

Type Description
dict[str, Any] | None

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

rework abstractmethod

rework(session_id: str) -> str

Rework a session and return its new ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session to rework.

required

Raises:

Type Description
JamSessionNotFound

If the session with the given ID does not exist.

Returns:

Name Type Description
str str

The new session ID.

update abstractmethod

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

Update an existing session with new data.

Parameters:

Name Type Description Default
session_id str

The ID of the session to update.

required
data dict

The new data to be stored in the session.

required

Raises:

Type Description
JamSessionNotFound

If the session with the given ID does not exist.