canvas-lms-api 0.7.0

Rust client for the Instructure Canvas LMS REST API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use crate::{
    error::{CanvasError, Result},
    http::Requester,
    pagination::PageStream,
    params::wrap_params,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Parameters for creating or updating a Canvas discussion topic.
#[derive(Debug, Default, Clone, Serialize)]
pub struct UpdateDiscussionParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub discussion_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub published: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delayed_post_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locked: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pinned: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_initial_post: Option<bool>,
}

/// Parameters for posting an entry on a discussion topic.
#[derive(Debug, Default, Clone, Serialize)]
pub struct PostEntryParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

/// A Canvas discussion topic (announcement or discussion board post).
#[derive(Debug, Clone, Deserialize, Serialize, canvas_lms_api_derive::CanvasResource)]
pub struct DiscussionTopic {
    pub id: u64,
    pub course_id: Option<u64>,
    pub title: Option<String>,
    pub message: Option<String>,
    pub html_url: Option<String>,
    pub posted_at: Option<DateTime<Utc>>,
    pub last_reply_at: Option<DateTime<Utc>>,
    pub require_initial_post: Option<bool>,
    pub user_can_see_posts: Option<bool>,
    pub discussion_subentry_count: Option<u64>,
    pub read_state: Option<String>,
    pub unread_count: Option<u64>,
    pub subscribed: Option<bool>,
    pub discussion_type: Option<String>,
    pub published: Option<bool>,
    pub locked: Option<bool>,
    pub pinned: Option<bool>,
    pub locked_for_user: Option<bool>,
    pub assignment_id: Option<u64>,
    pub delayed_post_at: Option<DateTime<Utc>>,
    pub due_at: Option<DateTime<Utc>>,

    #[serde(skip)]
    pub(crate) requester: Option<Arc<Requester>>,
    #[serde(skip)]
    pub course_id_ctx: Option<u64>,
    #[serde(skip)]
    pub group_id: Option<u64>,
}

impl DiscussionTopic {
    fn parent_prefix(&self) -> Result<String> {
        if let Some(id) = self.course_id.or(self.course_id_ctx) {
            Ok(format!("courses/{id}"))
        } else if let Some(id) = self.group_id {
            Ok(format!("groups/{id}"))
        } else {
            Err(CanvasError::BadRequest {
                message: "DiscussionTopic has no course_id or group_id".to_string(),
                errors: vec![],
            })
        }
    }

    fn propagate(&self, topic: &mut DiscussionTopic) {
        topic.requester = self.requester.clone();
        topic.course_id_ctx = self.course_id.or(self.course_id_ctx);
        topic.group_id = self.group_id;
    }

    fn make_entry(&self, entry: DiscussionEntry) -> DiscussionEntry {
        let mut e = entry;
        e.requester = self.requester.clone();
        e.course_id = self.course_id.or(self.course_id_ctx);
        e.group_id = self.group_id;
        e.topic_id = Some(self.id);
        e
    }

    /// Update this discussion topic.
    ///
    /// # Canvas API
    /// `PUT /api/v1/courses/:course_id/discussion_topics/:id`
    pub async fn update(&self, params: UpdateDiscussionParams) -> Result<DiscussionTopic> {
        let prefix = self.parent_prefix()?;
        let form = wrap_params("discussion_topic", &params);
        let mut topic: DiscussionTopic = self
            .req()
            .put(&format!("{prefix}/discussion_topics/{}", self.id), &form)
            .await?;
        self.propagate(&mut topic);
        Ok(topic)
    }

    /// Delete this discussion topic.
    ///
    /// # Canvas API
    /// `DELETE /api/v1/courses/:course_id/discussion_topics/:id`
    pub async fn delete(&self) -> Result<()> {
        let prefix = self.parent_prefix()?;
        self.req()
            .delete_void(&format!("{prefix}/discussion_topics/{}", self.id))
            .await
    }

    /// Post a new top-level entry to this discussion topic.
    ///
    /// # Canvas API
    /// `POST /api/v1/courses/:course_id/discussion_topics/:id/entries`
    pub async fn post_entry(&self, params: PostEntryParams) -> Result<DiscussionEntry> {
        let prefix = self.parent_prefix()?;
        let form = wrap_params("discussion_entry", &params);
        let entry: DiscussionEntry = self
            .req()
            .post(
                &format!("{prefix}/discussion_topics/{}/entries", self.id),
                &form,
            )
            .await?;
        Ok(self.make_entry(entry))
    }

    /// Stream all top-level entries of this topic.
    ///
    /// # Canvas API
    /// `GET /api/v1/courses/:course_id/discussion_topics/:id/entries`
    pub fn get_topic_entries(&self) -> PageStream<DiscussionEntry> {
        let course_id = self.course_id.or(self.course_id_ctx);
        let group_id = self.group_id;
        let topic_id = self.id;
        let prefix = if let Some(id) = course_id {
            format!("courses/{id}")
        } else if let Some(id) = group_id {
            format!("groups/{id}")
        } else {
            String::new()
        };
        PageStream::new_with_injector(
            Arc::clone(self.req()),
            &format!("{prefix}/discussion_topics/{topic_id}/entries"),
            vec![],
            move |mut e: DiscussionEntry, req| {
                e.requester = Some(Arc::clone(&req));
                e.course_id = course_id;
                e.group_id = group_id;
                e.topic_id = Some(topic_id);
                e
            },
        )
    }

    /// Fetch specific entries by ID.
    ///
    /// # Canvas API
    /// `GET /api/v1/courses/:course_id/discussion_topics/:id/entry_list?ids[]=...`
    pub async fn get_entries(&self, ids: &[u64]) -> Result<Vec<DiscussionEntry>> {
        let prefix = self.parent_prefix()?;
        let params: Vec<(String, String)> = ids
            .iter()
            .map(|id| ("ids[]".to_string(), id.to_string()))
            .collect();
        let entries: Vec<DiscussionEntry> = self
            .req()
            .get(
                &format!("{prefix}/discussion_topics/{}/entry_list", self.id),
                &params,
            )
            .await?;
        Ok(entries.into_iter().map(|e| self.make_entry(e)).collect())
    }

    /// Mark this topic as read.
    ///
    /// # Canvas API
    /// `PUT /api/v1/courses/:course_id/discussion_topics/:id/read`
    pub async fn mark_as_read(&self) -> Result<()> {
        let prefix = self.parent_prefix()?;
        self.req()
            .put_void(&format!("{prefix}/discussion_topics/{}/read", self.id))
            .await
    }

    /// Mark this topic as unread.
    ///
    /// # Canvas API
    /// `DELETE /api/v1/courses/:course_id/discussion_topics/:id/read`
    pub async fn mark_as_unread(&self) -> Result<()> {
        let prefix = self.parent_prefix()?;
        self.req()
            .delete_void(&format!("{prefix}/discussion_topics/{}/read", self.id))
            .await
    }

    /// Mark all entries as read.
    ///
    /// # Canvas API
    /// `PUT /api/v1/courses/:course_id/discussion_topics/:id/read_all`
    pub async fn mark_entries_as_read(&self, forced: bool) -> Result<()> {
        let prefix = self.parent_prefix()?;
        let params = if forced {
            vec![("forced_read_state".to_string(), "true".to_string())]
        } else {
            vec![]
        };
        let _ = params; // Canvas ignores this in practice; just issue the PUT
        self.req()
            .put_void(&format!("{prefix}/discussion_topics/{}/read_all", self.id))
            .await
    }

    /// Mark all entries as unread.
    ///
    /// # Canvas API
    /// `DELETE /api/v1/courses/:course_id/discussion_topics/:id/read_all`
    pub async fn mark_entries_as_unread(&self, forced: bool) -> Result<()> {
        let prefix = self.parent_prefix()?;
        let _ = forced;
        self.req()
            .delete_void(&format!("{prefix}/discussion_topics/{}/read_all", self.id))
            .await
    }

    /// Subscribe to this topic.
    ///
    /// # Canvas API
    /// `PUT /api/v1/courses/:course_id/discussion_topics/:id/subscribed`
    pub async fn subscribe(&self) -> Result<()> {
        let prefix = self.parent_prefix()?;
        self.req()
            .put_void(&format!(
                "{prefix}/discussion_topics/{}/subscribed",
                self.id
            ))
            .await
    }

    /// Unsubscribe from this topic.
    ///
    /// # Canvas API
    /// `DELETE /api/v1/courses/:course_id/discussion_topics/:id/subscribed`
    pub async fn unsubscribe(&self) -> Result<()> {
        let prefix = self.parent_prefix()?;
        self.req()
            .delete_void(&format!(
                "{prefix}/discussion_topics/{}/subscribed",
                self.id
            ))
            .await
    }
}

/// A single entry (post) within a Canvas discussion topic.
#[derive(Debug, Clone, Deserialize, Serialize, canvas_lms_api_derive::CanvasResource)]
pub struct DiscussionEntry {
    pub id: u64,
    pub user_id: Option<u64>,
    pub discussion_id: Option<u64>,
    pub parent_id: Option<u64>,
    pub message: Option<String>,
    pub created_at: Option<DateTime<Utc>>,
    pub updated_at: Option<DateTime<Utc>>,

    #[serde(skip)]
    pub(crate) requester: Option<Arc<Requester>>,
    #[serde(skip)]
    pub course_id: Option<u64>,
    #[serde(skip)]
    pub group_id: Option<u64>,
    #[serde(skip)]
    pub topic_id: Option<u64>,
}

impl DiscussionEntry {
    fn parent_prefix(&self) -> Result<String> {
        if let Some(id) = self.course_id {
            Ok(format!("courses/{id}"))
        } else if let Some(id) = self.group_id {
            Ok(format!("groups/{id}"))
        } else {
            Err(CanvasError::BadRequest {
                message: "DiscussionEntry has no course_id or group_id".to_string(),
                errors: vec![],
            })
        }
    }

    fn topic_id_or_err(&self) -> Result<u64> {
        self.topic_id.ok_or_else(|| CanvasError::BadRequest {
            message: "DiscussionEntry has no topic_id".to_string(),
            errors: vec![],
        })
    }

    fn propagate(&self, entry: &mut DiscussionEntry) {
        entry.requester = self.requester.clone();
        entry.course_id = self.course_id;
        entry.group_id = self.group_id;
        entry.topic_id = self.topic_id;
    }

    /// Update this entry's message.
    ///
    /// # Canvas API
    /// `PUT /api/v1/courses/:course_id/discussion_topics/:topic_id/entries/:id`
    pub async fn update(&self, message: &str) -> Result<DiscussionEntry> {
        let prefix = self.parent_prefix()?;
        let topic_id = self.topic_id_or_err()?;
        let params = vec![("message".to_string(), message.to_string())];
        let mut entry: DiscussionEntry = self
            .req()
            .put(
                &format!("{prefix}/discussion_topics/{topic_id}/entries/{}", self.id),
                &params,
            )
            .await?;
        self.propagate(&mut entry);
        Ok(entry)
    }

    /// Delete this entry.
    ///
    /// # Canvas API
    /// `DELETE /api/v1/courses/:course_id/discussion_topics/:topic_id/entries/:id`
    pub async fn delete(&self) -> Result<()> {
        let prefix = self.parent_prefix()?;
        let topic_id = self.topic_id_or_err()?;
        self.req()
            .delete_void(&format!(
                "{prefix}/discussion_topics/{topic_id}/entries/{}",
                self.id
            ))
            .await
    }

    /// Post a reply to this entry.
    ///
    /// # Canvas API
    /// `POST /api/v1/courses/:course_id/discussion_topics/:topic_id/entries/:id/replies`
    pub async fn post_reply(&self, message: &str) -> Result<DiscussionEntry> {
        let prefix = self.parent_prefix()?;
        let topic_id = self.topic_id_or_err()?;
        let params = vec![("message".to_string(), message.to_string())];
        let mut entry: DiscussionEntry = self
            .req()
            .post(
                &format!(
                    "{prefix}/discussion_topics/{topic_id}/entries/{}/replies",
                    self.id
                ),
                &params,
            )
            .await?;
        self.propagate(&mut entry);
        Ok(entry)
    }

    /// Stream replies to this entry.
    ///
    /// # Canvas API
    /// `GET /api/v1/courses/:course_id/discussion_topics/:topic_id/entries/:id/replies`
    pub fn get_replies(&self) -> PageStream<DiscussionEntry> {
        let course_id = self.course_id;
        let group_id = self.group_id;
        let topic_id = self.topic_id.unwrap_or(0);
        let entry_id = self.id;
        let prefix = if let Some(id) = course_id {
            format!("courses/{id}")
        } else if let Some(id) = group_id {
            format!("groups/{id}")
        } else {
            String::new()
        };
        PageStream::new_with_injector(
            Arc::clone(self.req()),
            &format!("{prefix}/discussion_topics/{topic_id}/entries/{entry_id}/replies"),
            vec![],
            move |mut e: DiscussionEntry, req| {
                e.requester = Some(Arc::clone(&req));
                e.course_id = course_id;
                e.group_id = group_id;
                e.topic_id = Some(topic_id);
                e
            },
        )
    }

    /// Mark this entry as read.
    ///
    /// # Canvas API
    /// `PUT /api/v1/courses/:course_id/discussion_topics/:topic_id/entries/:id/read`
    pub async fn mark_as_read(&self) -> Result<()> {
        let prefix = self.parent_prefix()?;
        let topic_id = self.topic_id_or_err()?;
        self.req()
            .put_void(&format!(
                "{prefix}/discussion_topics/{topic_id}/entries/{}/read",
                self.id
            ))
            .await
    }

    /// Mark this entry as unread.
    ///
    /// # Canvas API
    /// `DELETE /api/v1/courses/:course_id/discussion_topics/:topic_id/entries/:id/read`
    pub async fn mark_as_unread(&self) -> Result<()> {
        let prefix = self.parent_prefix()?;
        let topic_id = self.topic_id_or_err()?;
        self.req()
            .delete_void(&format!(
                "{prefix}/discussion_topics/{topic_id}/entries/{}/read",
                self.id
            ))
            .await
    }

    /// Rate this entry (0 = unrate, 1 = rate).
    ///
    /// # Canvas API
    /// `POST /api/v1/courses/:course_id/discussion_topics/:topic_id/entries/:id/rating`
    pub async fn rate(&self, rating: u8) -> Result<()> {
        if rating > 1 {
            return Err(CanvasError::BadRequest {
                message: "rating must be 0 or 1".to_string(),
                errors: vec![],
            });
        }
        let prefix = self.parent_prefix()?;
        let topic_id = self.topic_id_or_err()?;
        let params = vec![("rating".to_string(), rating.to_string())];
        self.req()
            .post_void_with_params(
                &format!(
                    "{prefix}/discussion_topics/{topic_id}/entries/{}/rating",
                    self.id
                ),
                &params,
            )
            .await
    }
}