canvas_lms_sync/canvas_api/
files.rs

1use super::{Client, Error};
2use async_stream::stream;
3use serde::Deserialize;
4use tokio_stream::Stream;
5
6#[derive(Debug, Clone, Deserialize)]
7pub struct FolderResp {
8    pub id: i64,
9    pub name: String,
10    pub full_name: String,
11    pub context_id: i64,
12    pub context_type: String,
13    pub parent_folder_id: Option<i64>,
14    pub created_at: String,
15    pub updated_at: String,
16    pub lock_at: Option<String>,
17    pub unlock_at: Option<String>,
18    pub position: Option<i64>,
19    pub locked: bool,
20    pub folders_url: String,
21    pub files_url: String,
22    pub files_count: i64,
23    pub folders_count: i64,
24    pub hidden: Option<bool>,
25    pub locked_for_user: bool,
26    pub hidden_for_user: bool,
27    pub for_submissions: bool,
28    pub can_upload: bool,
29}
30
31#[derive(Debug, Clone, Deserialize)]
32pub struct FileResp {
33    pub id: i64,
34    pub uuid: String,
35    pub folder_id: i64,
36    pub display_name: String,
37    pub filename: String,
38    pub upload_status: String,
39    #[serde(rename = "content-type")]
40    pub content_type: String,
41    pub url: String,
42    pub size: i64,
43    pub created_at: String,
44    pub updated_at: String,
45    pub unlock_at: Option<String>,
46    pub locked: bool,
47    pub hidden: bool,
48    pub lock_at: Option<String>,
49    pub hidden_for_user: bool,
50    pub modified_at: String,
51    pub mime_class: String,
52    pub media_entry_id: Option<String>,
53    pub locked_for_user: bool,
54}
55
56impl Client {
57    pub fn get_all_folders(
58        &self,
59        courseid: i64,
60    ) -> impl Stream<Item = Result<FolderResp, Error>> + '_ {
61        let url = self.build_url(&format!("/api/v1/courses/{}/folders", courseid));
62
63        stream! {
64            let mut next = Some(url);
65            while let Some(url) = next {
66                let (data, pagination) = self.make_json_request::<Vec<FolderResp>, _>(url).await?;
67                next = pagination.and_then(|p| p.next);
68                for folder in data {
69                    yield Ok(folder);
70                }
71            }
72        }
73    }
74    pub fn get_all_files(&self, courseid: i64) -> impl Stream<Item = Result<FileResp, Error>> + '_ {
75        let url = self.build_url(&format!("/api/v1/courses/{}/files", courseid));
76
77        stream! {
78            let mut next = Some(url);
79            while let Some(url) = next {
80                let (data, pagination) = self.make_json_request::<Vec<FileResp>, _>(url).await?;
81                next = pagination.and_then(|p| p.next);
82                for file in data {
83                    yield Ok(file);
84                }
85            }
86        }
87    }
88    pub async fn get_course_file(&self, courseid: i64, fileid: i64) -> Result<FileResp, Error> {
89        let url = self.build_url(&format!("/api/v1/courses/{}/files/{}", courseid, fileid));
90
91        let (data, _) = self.make_json_request::<FileResp, _>(url).await?;
92
93        Ok(data)
94    }
95}