Skip to content

Conversation

@SillyFreak
Copy link

(again based on #24)

This PR adds the ability to use the file upload endpoint directly through Moodle instances:

result = moodle.upload(
    ('filename.txt', open('./filename.txt', 'rb')),
)
result.filename  # filename.txt
result.itemid    # can be used e.g. to call mod_assign_save_submission, to add files to the submission

Since file uploads go through a separate endpoint (/webservice/upload.php instead of /webservice/rest/server.php), I added a method to the Mdl base class, and an exception in case the URL can't be determined (for very nonstandard Moodle setups). The user facing API mirrors the other modules, with an Upload class extending BaseMoodle. Since there are no other methods, I used __call__(). If you don't agree with any of these decisions, I'd be happy to adjust the code, or accept your adjustments.

I also added a test case to make sure the code works.

@SillyFreak SillyFreak marked this pull request as ready for review July 25, 2025 20:18
@SillyFreak
Copy link
Author

I just noticed that malformed tokens can lead to error responses that are different from what the process_response method expects. I did a pretty ad-hoc fix for that; I'm again open to changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant