Crates.io | canvasapi |
lib.rs | canvasapi |
version | 0.5.0 |
source | src |
created_at | 2020-08-31 12:23:05.812913 |
updated_at | 2022-12-12 20:28:05.482694 |
description | Canvas LMS REST API |
homepage | https://gitlab.com/thvdveld/canvasapi |
repository | https://gitlab.com/thvdveld/canvasapi |
max_upload_size | |
id | 283065 |
size | 99,873 |
This is the (unofficial) Rust version of the library for accessing the Canvas LMS API. The documentation of the official API can be found here.
This client that is used for making the calls is reqwest. The API calls are by default asynchronous. The Tokio runtime is used for the tests.
canvas
binaryThis binary allows interacting with the Canvas API. The output of the binary is JSON. To install:
cargo install canvasapi --bin canvas
blocking
: enables blocking requests.devel
: enables functions that are still in development.First, the information about the Canvas LMS server needs to be stored in a CanvasInformation
struct.
Using this information, the correct request URL's are created and, with a valid API token,
executed.
let base_url = std::env::var("CANVAS_BASE_URL").unwrap();
let canvas_token = std::env::var("CANVAS_ACCESS_TOKEN").unwrap();
let canvas = CanvasInformation::new(&base_url, &canvas_token);
let course = Canvas::get_course(13369).fetch(&canvas).await.unwrap().inner();
This crate only supports GET requests, and not all of them are implemented. Only the ones I use are implemented. Feel free to add more.
To add support for new request, follow these steps:
models
, when it is not yet defined.
This information can be retrieved using the official Canvas API.api_get!
macro. The following
example shows the usage of the macro.impl Course {
api_get! {
/// Get all courses from the current user.
courses():
"courses" =>
() -> () -> [Course]
}
api_get! {
/// Return the assignment with the given id.
get_assignment(self):
"courses/{id}/assignments/{assignment_id}" =>
(id: self.id) -> (assignment_id: usize) -> Assignment
}
api_get! {
/// Get only the students from the course.
get_students(self):
"courses/{id}/search_users" =>
(id: self.id) -> () -> [User]
[EnrollmentType::Student]
}
api_get! {
/// This is still in development.
function_in_developmen():
"test" =>
() -> () -> Test
features = [(name = "devel", reason = "Function is not ready yet.")]
}
api_todo! {
/// This function still needs to be fully implemented.
function_that_is_todo()
}
}
First, you write the (optional) documentation string, for use in this generated
documentation, followed by the function/method name.
If it is a method, the self
keyword is used between the parentheses.
Next, you write the request url, without the base url and without /api/v1/
.
The full request is generated using the CanvasInformation
struct.
The request url can contain named parameters.
The self
arguments are defined in the first group and the function arguments are
passed in the second group.
Finaly, the return type of the request is defined by passing the returned struct.
Square brackets are used when a Vec
is returned by the API.
Optionaly, requests parameters can be added.
License: MIT OR Apache-2.0