Issue with update_score_and_comments() API call

Jump to solution
MichaelCrowl
Community Explorer

EDIT: Updated the post to reflect some of the things I learned in comments and feedback to make this more clear.

Good day!  First time posting here, so here's to hoping I can make this easy to understand.  I'm an instructor with a passion to automate manual tasks.

Foregoing the fact that I'm using the Python canvasapi, I'm struggling with the Live API (which will later inform how I use my Python API).  

The Canvas LMS API provides the following example of a hash:

{
  "quiz_submissions": [{
    "attempt": 1,
    "fudge_points": -2.4,
    "questions": {
      "1": {
        "score": 2.5,
        "comment": "This can't be right, but I'll let it pass this one time."
      },
      "2": {
        "score": 0,
        "comment": "Good thinking. Almost!"
      }
    }
  }]
}

 

 

The link to that source is here:

https://canvas.instructure.com/doc/api/quiz_submissions.html#method.quizzes/quiz_submissions_api.upd...

Here's the hash my Python code produces:

{
'quiz_submissions': [{
'attempt': 1,
'fudge_points': 0,
'questions': {
'229417081': {'score': 10, 'comment': ''},
'229417071': {'score': 10, 'comment': ''},
'229417074': {'score': 10, 'comment': ''},
'229417023': {'score': 10, 'comment': ''},
'229415416': {'score': 30, 'comment': ''},
'229417041': {'score': 30, 'comment': ''}
}
}]
}

We've already established that the question IDs are the correct value to use rather than 1/2/3/etc, and the single/double quotes are not the issue.  The course_id, quiz_id, and submission ID I give to Live API are all correct, as they work for the GETs.  It's this one PUT that is rejecting my hash with the following error:

{ "status": "bad_request", "message": "missing required key :quiz_submissions" }

What am I doing wrong with my hash in Live API? 

0 Likes
1 Solution

The canvasapi Python library is just a wrapper for raw API calls -- internally, it still makes the API calls as stated in the official Instructure Canvas API documentation.

The canvasapi Python library just handles the pagination and also wraps the JSON responses into a Python object that can be manipulated easier. Think of the library as a way to quickly interface with the official Canvas API without having to be bogged down with handling HTTP calls. 🙂

I also recommend reading the following as well: Keyword Arguments -- canvasapi  as it will give you the syntax for making the calls within the canvasapi library.  Note that the update score and submissions API code matches the 'list of nested parameters' style.

I did make a successful update_score_and_comments call in my beta instance right now and here's the format of how the call should work within the canvasapi library:

(the update object to update the submission by)

update_obj = [
    {
        'attempt': #the attempt number that you want to edit, required,
        'fudge_points': #optional, how much to fudge by,
        'questions': {
            <question id, in quotes>: {
                'score': <score>,
                'comment': <comment, in quotes>
            },
            #continue listing questions in the format above
        }
    }
]

 

(the call itself)

#you have to fetch the submission that you want to edit. This I will leave up as an exercise.

quiz_sub = #the quiz submission that you fetched up
update = quiz_sub.update_score_and_comments(quiz_submissions=update_obj) #update_obj from the above code

print(update) #should print up the quiz no, the user id, and the submission no

Hope this helps!

View solution in original post

0 Likes