Skip to content

MoodleClient

MoodleClient is a high-level, discoverable facade over the function-based py_moodle modules. It owns a connected Moodle session and exposes grouped resource namespaces (courses, sections, users, folders, labels, assignments, scorm) instead of requiring every call site to thread session, base_url, token and sesskey by hand.

It is a pure facade: every resource-namespace method delegates to the existing, already-documented functions in course.py, section.py, user.py, folder.py, label.py, assign.py and scorm.py, without changing their signatures or behavior.

High-level, object-oriented facade over the py_moodle function API.

MoodleClient wraps a requests.Session, base_url, token and sesskey tuple (obtained either explicitly or via :class:MoodleSession) and exposes grouped resource namespaces (courses, sections, users, folders, labels, assignments, scorm) whose methods delegate to the existing, already-tested functions in course.py, section.py, user.py, folder.py, label.py, assign.py and scorm.py.

This module is purely additive: it does not modify the signature or behavior of any function it delegates to.

Classes:

Name Description
CoursesResource

Course-related operations, bound to a :class:MoodleClient.

SectionsResource

Section-related operations, bound to a :class:MoodleClient.

UsersResource

User-related operations, bound to a :class:MoodleClient.

FoldersResource

Folder-module operations, bound to a :class:MoodleClient.

LabelsResource

Label-module operations, bound to a :class:MoodleClient.

AssignmentsResource

Assignment-module operations, bound to a :class:MoodleClient.

ScormResource

SCORM-module operations, bound to a :class:MoodleClient.

MoodleClient

High-level, discoverable facade over the py_moodle function API.

Classes

CoursesResource

CoursesResource(client: 'MoodleClient')

Bases: _BaseResource

Course-related operations, bound to a :class:MoodleClient.

Bind the resource proxy to its owning client.

Parameters:

Name Type Description Default
client 'MoodleClient'

The :class:MoodleClient instance owning this resource.

required

Methods:

Name Description
list

Delegate to :func:py_moodle.course.list_courses.

create

Delegate to :func:py_moodle.course.create_course.

get

Delegate to :func:py_moodle.course.get_course.

delete

Delegate to :func:py_moodle.course.delete_course.

get_with_sections_and_modules

Delegate to :func:py_moodle.course.get_course_with_sections_and_modules.

context_id

Delegate to :func:py_moodle.course.get_course_context_id.

Methods:

list
list(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.course.list_courses.

create
create(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.course.create_course.

get
get(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.course.get_course.

delete
delete(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.course.delete_course.

get_with_sections_and_modules
get_with_sections_and_modules(
    *args: Any, **kwargs: Any
) -> Any

Delegate to :func:py_moodle.course.get_course_with_sections_and_modules.

context_id
context_id(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.course.get_course_context_id.

SectionsResource

SectionsResource(client: 'MoodleClient')

Bases: _BaseResource

Section-related operations, bound to a :class:MoodleClient.

Bind the resource proxy to its owning client.

Parameters:

Name Type Description Default
client 'MoodleClient'

The :class:MoodleClient instance owning this resource.

required

Methods:

Name Description
list

Delegate to :func:py_moodle.section.list_sections.

create

Delegate to :func:py_moodle.section.create_section.

delete

Delegate to :func:py_moodle.section.delete_section.

Methods:

list
list(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.section.list_sections.

create
create(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.section.create_section.

delete
delete(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.section.delete_section.

UsersResource

UsersResource(client: 'MoodleClient')

Bases: _BaseResource

User-related operations, bound to a :class:MoodleClient.

Bind the resource proxy to its owning client.

Parameters:

Name Type Description Default
client 'MoodleClient'

The :class:MoodleClient instance owning this resource.

required

Methods:

Name Description
list

Delegate to :func:py_moodle.user.list_course_users.

create

Delegate to :func:py_moodle.user.create_user.

delete

Delegate to :func:py_moodle.user.delete_user.

Methods:

list
list(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.user.list_course_users.

create
create(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.user.create_user.

delete
delete(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.user.delete_user.

FoldersResource

FoldersResource(client: 'MoodleClient')

Bases: _BaseResource

Folder-module operations, bound to a :class:MoodleClient.

Bind the resource proxy to its owning client.

Parameters:

Name Type Description Default
client 'MoodleClient'

The :class:MoodleClient instance owning this resource.

required

Methods:

Name Description
add

Delegate to :func:py_moodle.folder.add_folder.

delete

Delegate to :func:py_moodle.folder.delete_folder.

add_file

Delegate to :func:py_moodle.folder.add_file_to_folder.

delete_file

Delegate to :func:py_moodle.folder.delete_file_from_folder.

rename_file

Delegate to :func:py_moodle.folder.rename_file_in_folder.

list_content

Delegate to :func:py_moodle.folder.list_folder_content.

Methods:

add
add(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.folder.add_folder.

delete
delete(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.folder.delete_folder.

add_file
add_file(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.folder.add_file_to_folder.

delete_file
delete_file(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.folder.delete_file_from_folder.

rename_file
rename_file(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.folder.rename_file_in_folder.

list_content
list_content(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.folder.list_folder_content.

LabelsResource

LabelsResource(client: 'MoodleClient')

Bases: _BaseResource

Label-module operations, bound to a :class:MoodleClient.

Bind the resource proxy to its owning client.

Parameters:

Name Type Description Default
client 'MoodleClient'

The :class:MoodleClient instance owning this resource.

required

Methods:

Name Description
add

Delegate to :func:py_moodle.label.add_label.

delete

Delegate to :func:py_moodle.label.delete_label.

update

Delegate to :func:py_moodle.label.update_label.

Methods:

add
add(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.label.add_label.

delete
delete(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.label.delete_label.

update
update(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.label.update_label.

AssignmentsResource

AssignmentsResource(client: 'MoodleClient')

Bases: _BaseResource

Assignment-module operations, bound to a :class:MoodleClient.

Bind the resource proxy to its owning client.

Parameters:

Name Type Description Default
client 'MoodleClient'

The :class:MoodleClient instance owning this resource.

required

Methods:

Name Description
add

Delegate to :func:py_moodle.assign.add_assign.

Methods:

add
add(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.assign.add_assign.

ScormResource

ScormResource(client: 'MoodleClient')

Bases: _BaseResource

SCORM-module operations, bound to a :class:MoodleClient.

Bind the resource proxy to its owning client.

Parameters:

Name Type Description Default
client 'MoodleClient'

The :class:MoodleClient instance owning this resource.

required

Methods:

Name Description
add

Delegate to :func:py_moodle.scorm.add_scorm.

add_ajax

Delegate to :func:py_moodle.scorm.add_scorm_ajax.

Methods:

add
add(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.scorm.add_scorm.

add_ajax
add_ajax(*args: Any, **kwargs: Any) -> Any

Delegate to :func:py_moodle.scorm.add_scorm_ajax.

MoodleClient

MoodleClient(
    settings: Optional[Settings] = None,
    *,
    session: Optional[Session] = None,
    base_url: Optional[str] = None,
    token: Optional[str] = None,
    sesskey: Optional[str] = None
)

High-level, discoverable facade over the py_moodle function API.

A MoodleClient owns a connected Moodle session and exposes grouped resource namespaces (courses, sections, users, folders, labels, assignments, scorm) instead of requiring callers to thread session/base_url/token/sesskey through every call.

It can be built two ways:

  • From explicit settings: MoodleClient(settings), which lazily wraps a :class:MoodleSession (no network I/O happens until the session, token or sesskey is actually accessed).
  • From an explicit connection: MoodleClient(session=..., base_url=..., token=..., sesskey=...), for callers who already have these values.
  • From environment/profile: :meth:MoodleClient.from_env, which reuses :func:py_moodle.settings.load_settings and the cached, thread-safe :meth:MoodleSession.get.

It also supports the context manager protocol, closing the underlying requests.Session on exit (if one was actually opened).

Construct a client from explicit settings or an explicit connection.

Parameters:

Name Type Description Default
settings Optional[Settings]

Environment settings used to lazily create a :class:MoodleSession. Mutually exclusive with the explicit session/base_url arguments.

None
session Optional[Session]

An already-authenticated requests.Session. Requires base_url to also be provided.

None
base_url Optional[str]

Base URL of the Moodle instance (used with session).

None
token Optional[str]

Webservice token (used with session).

None
sesskey Optional[str]

Session key (used with session).

None

Raises:

Type Description
ValueError

If neither settings nor a session/base_url pair is provided.

Methods:

Name Description
from_env

Build a client from environment/profile configuration.

close

Close the underlying requests.Session, if one was opened.

Attributes:

Name Type Description
base_url str

Return the Moodle instance's base URL.

session Session

Return the authenticated requests.Session (login once).

token Optional[str]

Return the webservice token, or None if not available.

sesskey Optional[str]

Return the session key, or None if not available.

Attributes

base_url property
base_url: str

Return the Moodle instance's base URL.

session property
session: Session

Return the authenticated requests.Session (login once).

token property
token: Optional[str]

Return the webservice token, or None if not available.

sesskey property
sesskey: Optional[str]

Return the session key, or None if not available.

Methods:

from_env classmethod
from_env(env: Optional[str] = None) -> 'MoodleClient'

Build a client from environment/profile configuration.

Internally calls :meth:MoodleSession.get (which itself calls :func:py_moodle.settings.load_settings), reusing its existing per-environment caching, thread-safety, and lazy-login behavior.

Parameters:

Name Type Description Default
env Optional[str]

Environment key (e.g. "local", "staging", "prod"). Defaults to MoodleSession.get's own default ("local").

None

Returns:

Name Type Description
MoodleClient 'MoodleClient'

A client wired to the cached session for env.

close
close() -> None

Close the underlying requests.Session, if one was opened.

Safe to call multiple times, and safe to call even if no HTTP session was ever established (it will not force a login just to close it).