canvasapi icon indicating copy to clipboard operation
canvasapi copied to clipboard

"typed" API

Open dave-doty opened this issue 1 year ago • 1 comments

It seems this package would be much easier to use if it supported "typed" access. Many parts of the Canvas API are only accessible via keyword arguments or passing dictionaries with special keys. For example, to set the question_text of a QuizQuestion requires doing this:

course = canvasutil.get_course(123456)
quiz = course.get_quiz(654321)
question = quiz.get_questions()[0]
question.edit({'question_text': 'new question text'})

which itself requires consulting the (non-Python) Canvas API to determine that question_text is a valid key to use in that dict. (And even then it's not without problems, e.g., #579).

A better way would be if the canvasapi Python package supported this directly with a setter, e.g.,

course = canvasutil.get_course(123456)
quiz = course.get_quiz(654321)
question = quiz.get_questions()[0]
question.question_text = 'new question text'
# or
question.set_question_text('new question text')

which could then be documented (through docstrings on the setter method or property field) directly in the canvasapi Python package API docs at https://canvasapi.readthedocs.io/. It would also (huge win) allow auto-complete in IDEs to help make the API more easily "discoverable".

Is the only issue with this the time it would take to go through the (non-Python) Canvas API and look up all these attributes? Or the fear that it would become out of date if the Canvas API changes?

This isn't merely a matter of convenience; it's really not clear in some cases how to set these attributes, and the error messages when you do it wrong are also not very clear. (If you're lucky enough to get an error message; in some cases getting it wrong just means it silently fails.)

dave-doty avatar Dec 31 '22 20:12 dave-doty