Skip to content

Session

py_moodle.session

Reusable, thread-safe Moodle session.

Lazy login on first access and cache sessions per environment.

Classes:

Name Description
MoodleSessionError

Raised when we cannot obtain token or sesskey.

MoodleSession

Reusable and thread-safe Moodle session manager.

Classes

MoodleSessionError

Bases: RuntimeError

Raised when we cannot obtain token or sesskey.

MoodleSession

MoodleSession(settings: Settings)

Reusable and thread-safe Moodle session manager.

Initialize a session wrapper for the given settings.

Methods:

Name Description
call

Makes a call to the Moodle webservice API.

get

Return or create a cached session for the given environment.

Attributes:

Name Type Description
settings
session Session

Return the authenticated requests.Session (login once).

sesskey str

Return the session key (guaranteed to exist).

token str | None

Return the webservice token, or None if not available.

Attributes

settings instance-attribute
settings = settings
session property
session: Session

Return the authenticated requests.Session (login once).

sesskey property
sesskey: str

Return the session key (guaranteed to exist).

token property
token: str | None

Return the webservice token, or None if not available.

Functions

call
call(
    wsfunction: str, params: Optional[Dict[str, Any]] = None
) -> Any

Makes a call to the Moodle webservice API.

get classmethod
get(env: str | None = None) -> MoodleSession

Return or create a cached session for the given environment.

Parameters:

Name Type Description Default
env str | None

Environment key (e.g., "local" or "staging").

None

Returns:

Name Type Description
MoodleSession MoodleSession

Cached session instance.

Functions