|
1 | 1 | py_canvas_api |
2 | | -============= |
| 2 | +############# |
3 | 3 |
|
4 | 4 | The idea is to have a simple, easy to understand library for the Canvas API. |
5 | 5 |
|
6 | 6 | The Canvas API (https://canvas.instructure.com/doc/api/index.html) has hundreds of endpoints. |
| 7 | + |
7 | 8 | It seemed pointless to me to write a unique method for every one of them. |
8 | 9 | Instead, I created a class, called `ResterAPI` that uses `__getattr__` to |
9 | | -dynamically generate the URLS for an endpoint and `__call__` to make the actual |
10 | | -request. The `Canvas` class is built on top of `ResterAPI`. |
| 10 | +dynamically generate the path for an endpoint and `__call__` to add a path |
| 11 | +element with a parameter. The `Canvas` class is built on top of `ResterAPI`. |
11 | 12 |
|
12 | 13 | The result is a fairly small library that can handle the vast majority of |
13 | | -Canvas API endpoints. There are a few unique cases that are addressed in special |
14 | | -classes. For example, the `SIS Import API`_ takes a file upload and needs |
15 | | -special handling. |
| 14 | +Canvas API endpoints. |
16 | 15 |
|
17 | | -Here is how to do an SIS Import. |
| 16 | + |
| 17 | +Instantiate the Canvas object like this. |
18 | 18 |
|
19 | 19 | .. code-block:: py |
20 | 20 |
|
21 | | - from canvas_api import SISImporter |
22 | | - sis_importer = SISImporter('somedomain.instructure.com', CANVAS_ACCESS_TOKEN=os.getenv('ACCESS_TOKEN')) |
23 | | - sis_importer.do_sis_import(filepath) |
| 21 | + from canvas_api import Canvas |
| 22 | + c = Canvas('somedomain.instructure.com', CANVAS_ACCESS_TOKEN=os.getenv('ACCESS_TOKEN')) |
24 | 23 |
|
25 | 24 |
|
26 | | -Instantiate the Canvas object like this. |
| 25 | +Building paths |
| 26 | +============== |
| 27 | +You build the path appending "methods" after the instanted canvas_api object. |
| 28 | + |
| 29 | +You will typically see a line like `GET /api/v1/accounts/:account_id/courses` |
| 30 | +in the Canvas API documentation. This tells you what the path is and the inline |
| 31 | +parameters it needs. You would build this path with the `py_canvas_api` library |
| 32 | +like this: `c.accounts(8423).courses`. Don't worry about the `/api/v1` part. |
| 33 | + |
| 34 | +The `py_canvas_api` library takes care of the path parameters like :user by taking |
| 35 | +them as method arguments. For example, here is how you would get courses in the |
| 36 | +account with the id of 10 using the path above. |
| 37 | + |
| 38 | +.. code-block:: py |
| 39 | +
|
| 40 | + # get a list of courses (paginated to 10) in the account |
| 41 | + accounts = c.accounts('self').courses.get().json() |
| 42 | +
|
| 43 | +Common Requests |
| 44 | +================ |
| 45 | + |
| 46 | +GET |
| 47 | +----- |
| 48 | + |
| 49 | +To make a `GET` request, simply end with `get()`. This tells the library to |
| 50 | +make a GET request. This library uses the awesome `Requests`_ library so the |
| 51 | +return object in this case is simple a Response object. You will most often |
| 52 | +want the response as a json object. You get this by calling .json() with the |
| 53 | +Response. |
| 54 | + |
| 55 | +.. code-block:: py |
| 56 | +
|
| 57 | + # get a list of courses (paginated to 10) in the account |
| 58 | + accounts_json = c.accounts('self').courses.get().json() |
| 59 | +
|
| 60 | +If you need to send query parameters (key-value pairs added after a question |
| 61 | +mark), add these as keyword parameters in the `get()` call. Let's say you want |
| 62 | +a list of your own courses where you are a teacher and the enrollment is active. You would normally need to |
| 63 | +add `?enrollment_type=teacher&enrollment_state=active` to the URL to do this. With the `py_canvas_api` |
| 64 | +library, however, you would do it like this. |
27 | 65 |
|
28 | 66 | .. code-block:: py |
29 | 67 |
|
30 | | - from canvas_api import Canvas |
31 | | - c = Canvas('somedomain.instructure.com', CANVAS_ACCESS_TOKEN=os.getenv('ACCESS_TOKEN')) |
32 | 68 | # get a list of courses (paginated to 10) in the account |
33 | | - accounts = c.accounts('self').courses.get() |
| 69 | + accounts_json = c.courses.get(enrollment_type='teacher', enrollment_state='active').json() |
| 70 | +
|
| 71 | +Here are several more `GET` examples. |
| 72 | + |
| 73 | +.. code-block:: py |
| 74 | +
|
| 75 | + # list of users |
| 76 | + users = c.accounts('self').users.get().json() |
| 77 | +
|
| 78 | + # assignments in course with canvas id 23423 |
| 79 | + assignments = c.courses(23423).assignments.get().json() |
| 80 | +
|
| 81 | + # assignments in course with sis id ENG101 |
| 82 | + assignments = c.courses('sis_course_id:ENG101').assignments.get().json() |
| 83 | +
|
| 84 | + # list communication channels for user with id 82 |
| 85 | + channels = c.users(82).communication_channels.get().json() |
| 86 | +
|
| 87 | + # list own communication channels |
| 88 | + channels = c.users('self').communication_channels.get().json() |
34 | 89 |
|
35 | 90 | # Get a list of all courses in the account. This will keep pulling results as |
36 | 91 | # long as there are more pages. It uses generator functions to do this is a |
37 | 92 | # smart way. |
38 | 93 | accounts = c.accounts('self').course.get_paginated() |
39 | 94 |
|
| 95 | +Special Cases |
| 96 | +============== |
| 97 | +There are a few unique cases that are addressed in special |
| 98 | +classes. For example, the `SIS Import API`_ takes a file upload and needs |
| 99 | +special handling. |
| 100 | + |
| 101 | +Here is how to do an SIS Import. |
| 102 | + |
| 103 | +.. code-block:: py |
| 104 | +
|
| 105 | + from canvas_api import SISImporter |
| 106 | + sis_importer = SISImporter('somedomain.instructure.com', CANVAS_ACCESS_TOKEN=os.getenv('ACCESS_TOKEN')) |
| 107 | + sis_importer.do_sis_import(filepath) |
| 108 | +
|
40 | 109 |
|
41 | 110 | .. _`SIS Import API`: https://canvas.instructure.com/doc/api/sis_imports.html |
| 111 | + |
| 112 | +.. _`Requests`: http://docs.python-requests.org/en/master/ |
0 commit comments