Skip to content

jam.sessions.BaseSessionModule

BaseSessionModule

BaseSessionModule(
    id_factory: Callable[[], str] = lambda: str(uuid4()),
    is_session_crypt: bool = False,
    session_aes_secret: Optional[bytes] = 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]

AES secret for encoding session keys.

None

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,
) -> Union[None, Coroutine[Any, Any, 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
) -> Union[str, Coroutine[Any, 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,
) -> Optional[Coroutine[Any, Any, 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) -> Any

Retrieve a session by its key or ID.

Parameters:

Name Type Description Default
session_id str

The ID of the session.

required

Returns:

Name Type Description
Any Any

The session data if found, otherwise None.

rework abstractmethod

rework(
    session_id: str,
) -> Union[str, Coroutine[Any, Any, 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

Returns:

Name Type Description
str Union[str, Coroutine[Any, Any, str]]

The new session ID.

update abstractmethod

update(
    session_id: str, data: dict
) -> Union[None, Coroutine[Any, 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

FILE PATH: jam/sessions/__abc_session_repo__.py