You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The documentation seen on the Canvas API docs specifies (vaguely) the requirement for Quiz POST and PUT requests to be structured with all parameters assigned to the key "quiz". This library is inconsistent in its handling of similar situations and lacks documentation clarifying when the library handles such situations and when it does not, resulting in unnecessary confusion and debugging.
As an example of this discrepancy, in Course.py we find this method:
defcreate_quiz(self, quiz, **kwargs):
""" Create a new quiz in this course. :calls: POST /api/v1/courses/:course_id/quizzes \ <https://canvas.instructure.com/doc/api/quizzes.html#method.quizzes/quizzes_api.create>_ :param quiz: The attributes for the quiz. :type quiz: dict :rtype: :class:`canvasapi.quiz.Quiz` """fromcanvasapi.quizimportQuizifisinstance(quiz, dict) and"title"inquiz:
kwargs["quiz"] =quizelse:
raiseRequiredFieldMissing("Dictionary with key 'title' is required.")
response=self._requester.request(
"POST",
"courses/{}/quizzes".format(self.id),
_kwargs=combine_kwargs(**kwargs),
)
quiz_json=response.json()
quiz_json.update({"course_id": self.id})
returnQuiz(self._requester, quiz_json)
Note the line kwargs["quiz"] = quiz is executed before being passed into self._requester.request() thus resulting in a properly formatted POST params to Canvas's Quiz creation endpoint.
Unlike the first example where we create a quiz, in the PUT handler for quizzes, there is no attempt to format the kwargs to match the required params for the Canvas endpoint. If your first inclination is to simply pass in something like Quiz.edit(quiz_type="practice_quiz"), not only will it not work, it also won't error, you will receive a 200 and a valid quiz object from Canvas, but with no change applied. You are required to pass a single argument named 'quiz' for this to work: Quiz.edit(quiz={"quiz_type": "practice_quiz"}). As far as I can tell this is entirely undocumented and the only place to see an actual implementation of this method is in the tests, where the named argument 'quiz' is used.
There are other examples of this as well, here and here, there may be other examples but these are the ones I've encountered so far. This is annoying and unclear what the problem is when it occurs.
Lazy Solution
Append kwargs to a dict with the key 'quiz' and add a more comprehensive docstring
defedit(self, **kwargs):
""" Modify this quiz. :param **kwargs: Accepts any or all params described in the link below. :calls: `PUT /api/v1/courses/:course_id/quizzes/:id \ <https://canvas.instructure.com/doc/api/quizzes.html#method.quizzes/quizzes_api.update>`_ :returns: The updated quiz. :rtype: :class:`canvasapi.quiz.Quiz` --- Usage Example: --- ```python from canvasapi import Canvas API_URL = "https://canvas.example.edu/" API_KEY = "dawedasd4aq65sd453awd--dawedawnotarealkey" canvas_client = Canvas(API_URL, API_KEY) course = canvas_client.get_course(55555) quiz = course.get_quiz(77777) quiz.edit(quiz_type="practice_quiz", description="this is a test") ``` """quiz= {'quiz': kwargs}
response=self._requester.request(
"PUT",
"courses/{}/quizzes/{}".format(self.course_id, self.id),
_kwargs=combine_kwargs(**quiz),
)
quiz_json=response.json()
quiz_json.update({"course_id": self.course_id})
returnQuiz(self._requester, quiz_json)
As far as I can tell, this has the same combine_kwargs() output as passing the argument Quiz.edit(quiz={"key": "val"}) to the existing method and would work for the other examples cited. If there is any reason why this wouldn't work that I'm unaware of or better ideas, I'm all ears, but even just a docstring showing the required arg format would be an improvement.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Primary Issue
CanvasAPI Version: 3.2.0
Python Version: 3.9.2
The documentation seen on the Canvas API docs specifies (vaguely) the requirement for Quiz POST and PUT requests to be structured with all parameters assigned to the key "quiz". This library is inconsistent in its handling of similar situations and lacks documentation clarifying when the library handles such situations and when it does not, resulting in unnecessary confusion and debugging.
As an example of this discrepancy, in Course.py we find this method:
Note the line
kwargs["quiz"] = quiz
is executed before being passed intoself._requester.request()
thus resulting in a properly formatted POST params to Canvas's Quiz creation endpoint.Now take a look at this example in Quiz.py:
Unlike the first example where we create a quiz, in the PUT handler for quizzes, there is no attempt to format the kwargs to match the required params for the Canvas endpoint. If your first inclination is to simply pass in something like
Quiz.edit(quiz_type="practice_quiz")
, not only will it not work, it also won't error, you will receive a 200 and a valid quiz object from Canvas, but with no change applied. You are required to pass a single argument named 'quiz' for this to work:Quiz.edit(quiz={"quiz_type": "practice_quiz"})
. As far as I can tell this is entirely undocumented and the only place to see an actual implementation of this method is in the tests, where the named argument 'quiz' is used.There are other examples of this as well, here and here, there may be other examples but these are the ones I've encountered so far. This is annoying and unclear what the problem is when it occurs.
Lazy Solution
Append
kwargs
to a dict with the key 'quiz' and add a more comprehensive docstringAs far as I can tell, this has the same
combine_kwargs()
output as passing the argumentQuiz.edit(quiz={"key": "val"})
to the existing method and would work for the other examples cited. If there is any reason why this wouldn't work that I'm unaware of or better ideas, I'm all ears, but even just a docstring showing the required arg format would be an improvement.Beta Was this translation helpful? Give feedback.
All reactions