Struct canvas_lms_connector::Canvas
source · pub struct Canvas {}
Expand description
Represents the main interface for interacting with the Canvas Learning Management System (LMS).
This structure provides methods for performing various operations related to the Canvas LMS, such as fetching courses or handling authentication. It acts as a central point for accessing the functionalities offered by the Canvas API.
Examples
// Example of using the Canvas struct to fetch courses
let canvas = Canvas { /* fields initialization */ };
match canvas.fetch_courses_with_credentials(&canvas_info) {
Ok(courses) => println!("Courses retrieved: {:?}", courses),
Err(e) => eprintln!("Error fetching courses: {:?}", e),
}
Implementations§
source§impl Canvas
impl Canvas
Implementation of Canvas struct functionalities.
Provides methods to interact with the Canvas Learning Management System (LMS), encapsulating the logic for operations such as fetching courses, managing credentials, and other interactions with the Canvas API.
Methods
fetch_courses_with_credentials
: Fetches courses using specific Canvas credentials.fetch_courses
: Retrieves courses using stored or system-provided credentials.load_credentials_from_file
: Loads Canvas credentials from a configuration file.load_credentials_from_system
: Loads Canvas credentials stored in the system keyring.
Each method focuses on a specific aspect of Canvas LMS interaction, ensuring ease of use in various application contexts.
sourcepub fn fetch_courses_with_credentials(info: &CanvasInfo) -> CanvasResult
pub fn fetch_courses_with_credentials(info: &CanvasInfo) -> CanvasResult
Fetches a list of courses from the Canvas API using the provided credentials.
This function attempts to retrieve all courses accessible with the given Canvas credentials.
It utilizes the Canvas API endpoint to gather course information, authenticating the request
with the credentials supplied in the CanvasInfo
structure.
Arguments
info
- A reference toCanvasInfo
containing the URL and API token for Canvas API access.
Returns
A CanvasResult
enum, which can be either:
CanvasResult::Ok(Vec<Course>)
on successful retrieval of courses.CanvasResult::ErrConnection(String)
for connection-related errors.CanvasResult::ErrCredentials(String)
for authentication failures.
Examples
let canvas_info = CanvasInfo { /* fields initialization */ };
match fetch_courses_with_credentials(&canvas_info) {
CanvasResult::Ok(courses) => println!("Courses fetched: {:?}", courses),
CanvasResult::ErrConnection(err) => eprintln!("Connection error: {}", err),
CanvasResult::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
}
sourcepub fn fetch_single_course_with_credentials(
info: &CanvasInfo,
course_id: u64
) -> CanvasResult
pub fn fetch_single_course_with_credentials( info: &CanvasInfo, course_id: u64 ) -> CanvasResult
Fetches details of a specific course from the Canvas API using provided credentials.
This function retrieves information about a single course, identified by course_id
, using the credentials
provided in info
. It communicates with the Canvas API and returns the course data in a CanvasResult
.
Arguments
info
- A reference toCanvasInfo
containing the URL and API token for Canvas API access.course_id
- The unique identifier for the course to be fetched.
Returns
A CanvasResult
enum, which is either:
CanvasResult::Ok(Vec<Course>)
on successful retrieval of the course.CanvasResult::ErrConnection(String)
for connection-related errors.CanvasResult::ErrCredentials(String)
for authentication failures.
Examples
let canvas_info = CanvasInfo { /* fields initialization */ };
let course_id = 123;
match fetch_single_course_with_credentials(&canvas_info, course_id) {
CanvasResult::Ok(course) => println!("Course fetched: {:?}", course),
CanvasResult::ErrConnection(err) => eprintln!("Connection error: {}", err),
CanvasResult::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
}
sourcepub fn fetch_courses<F>(f: F) -> CanvasResultwhere
F: FnOnce(&CanvasInfo) -> CanvasResult + Copy,
pub fn fetch_courses<F>(f: F) -> CanvasResultwhere F: FnOnce(&CanvasInfo) -> CanvasResult + Copy,
Fetches a list of courses using a provided function for specific fetching logic.
This higher-order function takes another function f
as an argument, which contains the logic for fetching courses.
It allows for customization in the way courses are retrieved, while fetch_courses
itself handles credential management and error handling.
Type Parameters
F
: A function or closure that takes a reference toCanvasInfo
and returnsCanvasResult
.
Arguments
f
- The function or closure that will be called with the loadedCanvasInfo
to fetch courses.
Returns
A CanvasResult
enum, either:
CanvasResult::Ok(Vec<Course>)
on successful retrieval of courses.CanvasResult::ErrConnection(String)
for connection-related errors.CanvasResult::ErrCredentials(String)
for authentication failures.
Examples
match Canvas::fetch_courses(|credential| Canvas::fetch_courses_with_credentials(credential)) {
CanvasResult::Ok(courses) => println!("Courses fetched: {:?}", courses),
CanvasResult::ErrConnection(err) => eprintln!("Connection error: {}", err),
CanvasResult::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
}
or
let course_id = 123;
match Canvas::fetch_courses(|credential| Canvas::fetch_single_course_with_credentials(credential, course_id)) {
CanvasResult::Ok(courses) => println!("Courses fetched: {:?}", courses),
CanvasResult::ErrConnection(err) => eprintln!("Connection error: {}", err),
CanvasResult::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
}
sourcepub fn load_credentials_from_file() -> Result<CanvasInfo, String>
pub fn load_credentials_from_file() -> Result<CanvasInfo, String>
Loads Canvas credentials from a configuration file.
This function attempts to read Canvas API credentials from a predefined configuration file.
It looks for a file named config.json
in the user’s Downloads
directory and tries to deserialize
the contents into a CanvasInfo
structure. The CanvasInfo
contains the base URL and API token required
for authenticating Canvas API requests.
Returns
Returns a Result
with either:
Ok(CanvasInfo)
containing the loaded credentials on successful file reading and deserialization.Err(String)
with an error message if the file cannot be read, the path is invalid, or the contents cannot be deserialized intoCanvasInfo
.
Examples
match load_credentials_from_file() {
Ok(canvas_info) => println!("CanvasInfo loaded: {:?}", canvas_info),
Err(e) => eprintln!("Error loading credentials: {}", e),
}
sourcepub fn load_credentials_from_system() -> Result<CanvasInfo, String>
pub fn load_credentials_from_system() -> Result<CanvasInfo, String>
Loads Canvas credentials from the system’s keyring.
This function retrieves the Canvas API credentials (URL and token) stored in the system’s keyring.
It uses the keyring
crate to access the secure storage provided by the operating system. The credentials
are expected to be stored under the application’s name, fetched from the CARGO_PKG_NAME
environment variable.
This approach enhances security by avoiding plain text storage of sensitive information.
Returns
Returns a Result
with either:
Ok(CanvasInfo)
containing the credentials if successfully retrieved from the system’s keyring.Err(String)
with an error message if there are issues accessing the keyring or retrieving the credentials.
Examples
match load_credentials_from_system() {
Ok(canvas_info) => println!("CanvasInfo loaded from system: {:?}", canvas_info),
Err(e) => eprintln!("Error loading credentials from system: {}", e),
}