canvas_lms_sync/canvas_api/
modules.rs

1use super::{Client, Error};
2use async_stream::stream;
3use serde::Deserialize;
4use tokio_stream::Stream;
5
6#[derive(Debug, Clone, Deserialize)]
7pub struct ModuleResp {
8    pub id: i64,
9    pub name: String,
10    pub position: i64,
11    pub unlock_at: Option<String>,
12    pub require_sequential_progress: bool,
13    pub publish_final_grade: bool,
14    pub prerequisite_module_ids: Vec<i64>,
15    pub state: String,
16    pub completed_at: Option<String>,
17    pub items_count: i64,
18    pub items_url: String,
19}
20
21#[derive(Debug, Clone, Deserialize)]
22pub struct ModuleItemResp {
23    pub id: i64,
24    pub module_id: i64,
25    pub position: i64,
26    pub title: String,
27    pub indent: i64,
28    #[serde(rename = "type")]
29    pub type_: String,
30    pub content_id: Option<i64>,
31    pub html_url: Option<String>,
32    pub url: Option<String>,
33    pub page_url: Option<String>,
34    pub external_url: Option<String>,
35    pub new_tab: Option<bool>,
36}
37
38impl Client {
39    pub fn list_modules(
40        &self,
41        courseid: i64,
42    ) -> impl Stream<Item = Result<ModuleResp, Error>> + '_ {
43        let url = self.build_url(&format!("/api/v1/courses/{}/modules", courseid));
44        stream! {
45            let mut next = Some(url);
46            while let Some(url) = next {
47                let (resp, pagination) = self.make_json_request::<Vec<ModuleResp>, _>(url).await?;
48
49                next = pagination.and_then(|p| p.next);
50
51                for module in resp {
52                    yield Ok(module);
53                }
54
55            }
56        }
57    }
58    pub fn list_module_items(
59        &self,
60        courseid: i64,
61        moduleid: i64,
62    ) -> impl Stream<Item = Result<ModuleItemResp, Error>> + '_ {
63        let url = self.build_url(&format!(
64            "/api/v1/courses/{}/modules/{}/items",
65            courseid, moduleid
66        ));
67        stream! {
68            let mut next = Some(url);
69            while let Some(url) = next {
70                let (resp, pagination) = self.make_json_request::<Vec<ModuleItemResp>, _>(url).await?;
71
72                next = pagination.and_then(|p| p.next);
73
74                for module in resp {
75                    yield Ok(module);
76                }
77
78            }
79        }
80    }
81}