Package: py_canvas_api
The idea is to have a simple, easy to understand library for the Canvas API.The Canvas API (https://canvas.instructure.com/doc/api/index.html) has hundreds of endpoints.It seemed pointless to me to write a unique method for every one of them.
Instead, I created a class, calledResterAPIthat uses__getattr__to
dynamically generate the path for an endpoint and__call__to add a path
element with a parameter. TheCanvasclass is built on top ofResterAPI.The result is a fairly small library that can handle the vast majority of
Canvas API endpoints.Instantiate the Canvas object like this.fromcanvas_apiimportCanvasc=Canvas('somedomain.instructure.com',CANVAS_ACCESS_TOKEN=os.getenv('ACCESS_TOKEN'))Building pathsYou build the path appending “methods” after the instanted canvas_api object.You will typically see a line likeGET /api/v1/accounts/:account_id/coursesin the Canvas API documentation. This tells you what the path is and the inline
parameters it needs. You would build this path with thepy_canvas_apilibrary
like this:c.accounts(8423).courses. Don’t worry about the/api/v1part.Thepy_canvas_apilibrary takes care of the path parameters like :user by taking
them as method arguments. For example, here is how you would get courses in the
account with the id of 10 using the path above.# get a list of courses (paginated to 10) in the accountaccounts=c.accounts('self').courses.get().json()Common RequestsGETTo make aGETrequest, simply end withget(). This tells the library to
make a GET request. This library uses the awesomeRequestslibrary so the
return object in this case is simple a Response object. You will most often
want the response as a json object. You get this by calling .json() with the
Response.# get a list of courses (paginated to 10) in the accountaccounts_json=c.accounts('self').courses.get().json()If you need to send query parameters (key-value pairs added after a question
mark), add these as keyword parameters in theget()call. Let’s say you want
a list of your own courses where you are a teacher and the enrollment is active. You would normally need to
add?enrollment_type=teacher&enrollment_state=activeto the URL to do this. With thepy_canvas_apilibrary, however, you would do it like this.# get a list of courses (paginated to 10) in the accountaccounts_json=c.courses.get(enrollment_type='teacher',enrollment_state='active').json()Here are several moreGETexamples.# list of usersusers=c.accounts('self').users.get().json()# assignments in course with canvas id 23423assignments=c.courses(23423).assignments.get().json()# assignments in course with sis id ENG101assignments=c.courses('sis_course_id:ENG101').assignments.get().json()# list communication channels for user with id 82channels=c.users(82).communication_channels.get().json()# list own communication channelschannels=c.users('self').communication_channels.get().json()# Get a list of all courses in the account. This will keep pulling results as# long as there are more pages. It uses generator functions to do this is a# smart way.accounts=c.accounts('self').course.get_paginated()Special CasesThere are a few unique cases that are addressed in special
classes. For example, theSIS Import APItakes a file upload and needs
special handling.Here is how to do an SIS Import.fromcanvas_apiimportSISImportersis_importer=SISImporter('somedomain.instructure.com',CANVAS_ACCESS_TOKEN=os.getenv('ACCESS_TOKEN'))sis_importer.do_sis_import(filepath)