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: |
SectionsResource |
Section-related operations, bound to a :class: |
UsersResource |
User-related operations, bound to a :class: |
FoldersResource |
Folder-module operations, bound to a :class: |
LabelsResource |
Label-module operations, bound to a :class: |
AssignmentsResource |
Assignment-module operations, bound to a :class: |
ScormResource |
SCORM-module operations, bound to a :class: |
MoodleClient |
High-level, discoverable facade over the |
Classes¶
CoursesResource ¶
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: |
required |
Methods:
| Name | Description |
|---|---|
list |
Delegate to :func: |
create |
Delegate to :func: |
get |
Delegate to :func: |
delete |
Delegate to :func: |
get_with_sections_and_modules |
Delegate to :func: |
context_id |
Delegate to :func: |
SectionsResource ¶
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: |
required |
Methods:
| Name | Description |
|---|---|
list |
Delegate to :func: |
create |
Delegate to :func: |
delete |
Delegate to :func: |
UsersResource ¶
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: |
required |
Methods:
| Name | Description |
|---|---|
list |
Delegate to :func: |
create |
Delegate to :func: |
delete |
Delegate to :func: |
FoldersResource ¶
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: |
required |
Methods:
| Name | Description |
|---|---|
add |
Delegate to :func: |
delete |
Delegate to :func: |
add_file |
Delegate to :func: |
delete_file |
Delegate to :func: |
rename_file |
Delegate to :func: |
list_content |
Delegate to :func: |
LabelsResource ¶
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: |
required |
Methods:
| Name | Description |
|---|---|
add |
Delegate to :func: |
delete |
Delegate to :func: |
update |
Delegate to :func: |
AssignmentsResource ¶
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: |
required |
Methods:
| Name | Description |
|---|---|
add |
Delegate to :func: |
ScormResource ¶
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: |
required |
Methods:
| Name | Description |
|---|---|
add |
Delegate to :func: |
add_ajax |
Delegate to :func: |
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_settingsand 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: |
None
|
session
|
Optional[Session]
|
An already-authenticated |
None
|
base_url
|
Optional[str]
|
Base URL of the Moodle instance (used with |
None
|
token
|
Optional[str]
|
Webservice token (used with |
None
|
sesskey
|
Optional[str]
|
Session key (used with |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
Methods:
| Name | Description |
|---|---|
from_env |
Build a client from environment/profile configuration. |
close |
Close the underlying |
Attributes:
| Name | Type | Description |
|---|---|---|
base_url |
str
|
Return the Moodle instance's base URL. |
session |
Session
|
Return the authenticated |
token |
Optional[str]
|
Return the webservice token, or |
sesskey |
Optional[str]
|
Return the session key, or |
Attributes¶
Methods:¶
from_env
classmethod
¶
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. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
MoodleClient |
'MoodleClient'
|
A client wired to the cached session for |
close ¶
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).