Batch API requests
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm working on a proof-of-concept project which pulls in specific assignments and their submissions for a list of Enrollments. This means that I'm making repeated API calls in a loop (I'm using a Python backend and the
I know you can batch requests for SIS imports and other large migrations, but is there a genera batching parameter for the API?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I finally found the solution.
I was trying to string together assignment IDs into one parameter, but the Canvas API expects each ID separately in the URL string. For example, this is the wrong way to send the request:
https://yourinsitute.instructure.com/api/v1/courses/12345/students/submissions?student_ids[]=all&assignment_ids[]=118888,118889&grouped=1
The API expects multiple assignment_ids[] params specifying a valid ID. So, the correct string formatting is:
https://yourinsitute.instructure.com/api/v1/courses/12345/students/submissions?student_ids[]=all&assignment_ids[]=118888&assignment_ids[]=118889&grouped=1
Creating the string manually is tricky, but the fantastic canvasapi Python library from UCF Open has a simple method that takes the arguments as a list and makes the API call. To get the specific assignments, get a Course object and then pass in the arguments:
# See https://github.com/ucfopen/canvasapi for install and instantiation
from canvasapi import Canvas
course = canvas.get_course(12345)
assignments = course.get_multiple_submissions(student_ids='all', assignment_ids=(123, 456, 789, ...)
print(assignments)
<PaginatedList of Submissions>
Note that the current version of the library disables the grouped argument because it cannot (yet?) parse the returned nested object. I really wanted the student submissions grouped because we're building a table/row UI and grouping assignments makes that much easier. To get around it, I edited the source file (for dev only...) and used the Object.to_json() method to convert it to a JSON string and then loaded it back into the list as a JSON object for parsing.
from canvasapi import Canvas
import json
json_data = []
course = canvas.get_course(id)
submissions = course.get_multiple_submissions(assignment_ids=(123, 456, 789), student_ids='all', grouped=1)
for sub in submissions:
json_data.append(json.loads(sub.to_json()))
The project isn't quite finished yet, but this is a big problem out of the way. When the whole thing is done, I'll write up a longer blog post.