Skip to content

add file upload method#26

Open
ensko wants to merge 3 commits intohexatester:masterfrom
ensko:upload-files
Open

add file upload method#26
ensko wants to merge 3 commits intohexatester:masterfrom
ensko:upload-files

Conversation

@ensko
Copy link

@ensko ensko commented Jul 25, 2025

(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.

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

ensko commented Jul 25, 2025

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