canvas-lms-api 0.5.1

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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use crate::{
    error::Result,
    http::Requester,
    pagination::PageStream,
    params::wrap_params,
    resources::{
        communication_channel::CommunicationChannel, course::Course, enrollment::Enrollment,
        file::File, folder::Folder,
    },
};

/// Parameters for editing a Canvas user.
#[derive(Debug, Default, Clone, serde::Serialize)]
pub struct EditUserParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub short_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sortable_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_zone: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locale: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bio: Option<String>,
}
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// A Canvas user.
#[derive(Debug, Clone, Deserialize, Serialize, canvas_lms_api_derive::CanvasResource)]
pub struct User {
    pub id: u64,
    pub name: Option<String>,
    pub sortable_name: Option<String>,
    pub short_name: Option<String>,
    pub sis_user_id: Option<String>,
    pub login_id: Option<String>,
    pub email: Option<String>,
    pub avatar_url: Option<String>,
    pub locale: Option<String>,
    pub last_login: Option<DateTime<Utc>>,
    pub time_zone: Option<String>,
    pub bio: Option<String>,

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

impl User {
    /// Stream all courses for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/courses`
    pub fn get_courses(&self) -> PageStream<Course> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/courses", self.id),
            vec![],
        )
    }

    /// Stream all enrollments for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/enrollments`
    pub fn get_enrollments(&self) -> PageStream<Enrollment> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/enrollments", self.id),
            vec![],
        )
    }

    /// Stream all communication channels for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/communication_channels`
    pub fn get_communication_channels(&self) -> PageStream<CommunicationChannel> {
        let user_id = self.id;
        PageStream::new_with_injector(
            Arc::clone(self.req()),
            &format!("users/{user_id}/communication_channels"),
            vec![],
            |mut c: CommunicationChannel, req| {
                c.requester = Some(Arc::clone(&req));
                c
            },
        )
    }

    fn propagate(&self, u: &mut User) {
        u.requester = self.requester.clone();
    }

    /// Edit this user's profile.
    ///
    /// # Canvas API
    /// `PUT /api/v1/users/:id`
    pub async fn edit(&self, params: EditUserParams) -> Result<User> {
        let form = wrap_params("user", &params);
        let mut u: User = self.req().put(&format!("users/{}", self.id), &form).await?;
        self.propagate(&mut u);
        Ok(u)
    }

    /// Get this user's profile.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/profile`
    pub async fn get_profile(&self) -> Result<serde_json::Value> {
        self.req()
            .get(&format!("users/{}/profile", self.id), &[])
            .await
    }

    /// Terminate all sessions for this user.
    ///
    /// # Canvas API
    /// `DELETE /api/v1/users/:id/sessions`
    pub async fn terminate_sessions(&self) -> Result<()> {
        self.req()
            .delete_void(&format!("users/{}/sessions", self.id))
            .await
    }

    /// Merge this user into another user.
    ///
    /// # Canvas API
    /// `PUT /api/v1/users/:id/merge_into/:destination_user_id`
    pub async fn merge_into(&self, destination_user_id: u64) -> Result<User> {
        let mut u: User = self
            .req()
            .put(
                &format!("users/{}/merge_into/{destination_user_id}", self.id),
                &[],
            )
            .await?;
        self.propagate(&mut u);
        Ok(u)
    }

    /// Stream all avatar options for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/avatars`
    pub fn get_avatars(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/avatars", self.id),
            vec![],
        )
    }

    /// Stream page views for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/page_views`
    pub fn get_page_views(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/page_views", self.id),
            vec![],
        )
    }

    /// Stream all observees for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/observees`
    pub fn get_observees(&self) -> PageStream<User> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/observees", self.id),
            vec![],
        )
    }

    /// Add an observee by user ID.
    ///
    /// # Canvas API
    /// `PUT /api/v1/users/:id/observees/:observee_id`
    pub async fn add_observee(&self, observee_id: u64) -> Result<User> {
        let mut u: User = self
            .req()
            .put(&format!("users/{}/observees/{observee_id}", self.id), &[])
            .await?;
        self.propagate(&mut u);
        Ok(u)
    }

    /// Remove an observee.
    ///
    /// # Canvas API
    /// `DELETE /api/v1/users/:id/observees/:observee_id`
    pub async fn remove_observee(&self, observee_id: u64) -> Result<User> {
        let mut u: User = self
            .req()
            .delete(&format!("users/{}/observees/{observee_id}", self.id), &[])
            .await?;
        self.propagate(&mut u);
        Ok(u)
    }

    /// Fetch a single observee.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/observees/:observee_id`
    pub async fn show_observee(&self, observee_id: u64) -> Result<User> {
        let mut u: User = self
            .req()
            .get(&format!("users/{}/observees/{observee_id}", self.id), &[])
            .await?;
        self.propagate(&mut u);
        Ok(u)
    }

    /// Stream all observers of this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/observers`
    pub fn get_observers(&self) -> PageStream<User> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/observers", self.id),
            vec![],
        )
    }

    /// Get all custom colors for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/colors`
    pub async fn get_colors(&self) -> Result<serde_json::Value> {
        self.req()
            .get(&format!("users/{}/colors", self.id), &[])
            .await
    }

    /// Get the color for a specific asset.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/colors/:asset_string`
    pub async fn get_color(&self, asset_string: &str) -> Result<serde_json::Value> {
        self.req()
            .get(&format!("users/{}/colors/{asset_string}", self.id), &[])
            .await
    }

    /// Update the color for a specific asset.
    ///
    /// # Canvas API
    /// `PUT /api/v1/users/:id/colors/:asset_string`
    pub async fn update_color(
        &self,
        asset_string: &str,
        hexcode: &str,
    ) -> Result<serde_json::Value> {
        let params = vec![("hexcode".to_string(), hexcode.to_string())];
        self.req()
            .put(&format!("users/{}/colors/{asset_string}", self.id), &params)
            .await
    }

    /// Stream missing submissions for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/missing_submissions`
    pub fn get_missing_submissions(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/missing_submissions", self.id),
            vec![],
        )
    }

    /// Stream all files for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/files`
    pub fn get_files(&self) -> PageStream<File> {
        PageStream::new_with_injector(
            Arc::clone(self.req()),
            &format!("users/{}/files", self.id),
            vec![],
            move |mut f: File, req| {
                f.requester = Some(Arc::clone(&req));
                f
            },
        )
    }

    /// Stream all folders for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/folders`
    pub fn get_folders(&self) -> PageStream<Folder> {
        PageStream::new_with_injector(
            Arc::clone(self.req()),
            &format!("users/{}/folders", self.id),
            vec![],
            move |mut f: Folder, req| {
                f.requester = Some(Arc::clone(&req));
                f
            },
        )
    }

    /// Create a folder for this user.
    ///
    /// # Canvas API
    /// `POST /api/v1/users/:id/folders`
    pub async fn create_folder(&self, name: &str) -> Result<Folder> {
        let params = vec![("name".to_string(), name.to_string())];
        let mut f: Folder = self
            .req()
            .post(&format!("users/{}/folders", self.id), &params)
            .await?;
        f.requester = self.requester.clone();
        Ok(f)
    }

    /// Get the file storage quota for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/files/quota`
    pub async fn get_file_quota(&self) -> Result<serde_json::Value> {
        self.req()
            .get(&format!("users/{}/files/quota", self.id), &[])
            .await
    }

    /// Stream login information for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/logins`
    pub fn get_user_logins(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/logins", self.id),
            vec![],
        )
    }

    /// Get profile settings for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/settings`
    pub async fn get_settings(&self) -> Result<serde_json::Value> {
        self.req()
            .get(&format!("users/{}/settings", self.id), &[])
            .await
    }

    /// Update profile settings for this user.
    ///
    /// # Canvas API
    /// `PUT /api/v1/users/:id/settings`
    pub async fn update_settings(&self, params: &[(String, String)]) -> Result<serde_json::Value> {
        self.req()
            .put(&format!("users/{}/settings", self.id), params)
            .await
    }

    /// Create a communication channel for this user.
    ///
    /// `address` is the email address, phone number, etc.
    /// `channel_type` is `"email"`, `"sms"`, `"push"`, etc.
    ///
    /// # Canvas API
    /// `POST /api/v1/users/:id/communication_channels`
    pub async fn create_communication_channel(
        &self,
        address: &str,
        channel_type: &str,
    ) -> Result<CommunicationChannel> {
        let params = vec![
            (
                "communication_channel[address]".to_string(),
                address.to_string(),
            ),
            (
                "communication_channel[type]".to_string(),
                channel_type.to_string(),
            ),
        ];
        let mut channel: CommunicationChannel = self
            .req()
            .post(
                &format!("users/{}/communication_channels", self.id),
                &params,
            )
            .await?;
        channel.requester = self.requester.clone();
        Ok(channel)
    }

    /// Create an observer pairing code for this user.
    ///
    /// # Canvas API
    /// `POST /api/v1/users/:id/observer_pairing_codes`
    pub async fn create_pairing_code(&self) -> Result<serde_json::Value> {
        self.req()
            .post(&format!("users/{}/observer_pairing_codes", self.id), &[])
            .await
    }

    /// Stream authentication events (login/logout log) for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/audit/authentication/users/:id`
    pub fn get_authentication_events(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("audit/authentication/users/{}", self.id),
            vec![],
        )
    }

    /// Stream all feature flags for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/features`
    pub fn get_features(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/features", self.id),
            vec![],
        )
    }

    /// List enabled features for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/features/enabled`
    pub async fn get_enabled_features(&self) -> Result<Vec<String>> {
        self.req()
            .get(&format!("users/{}/features/enabled", self.id), &[])
            .await
    }

    /// Export content for this user.
    ///
    /// # Canvas API
    /// `POST /api/v1/users/:id/content_exports`
    pub async fn export_content(&self, export_type: &str) -> Result<serde_json::Value> {
        let params = vec![("export_type".to_string(), export_type.to_string())];
        self.req()
            .post(&format!("users/{}/content_exports", self.id), &params)
            .await
    }

    /// Stream content exports for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/content_exports`
    pub fn get_content_exports(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/content_exports", self.id),
            vec![],
        )
    }

    /// Stream ePortfolios for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/eportfolios`
    pub fn get_eportfolios(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/eportfolios", self.id),
            vec![],
        )
    }

    /// Stream open poll sessions for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/poll_sessions/opened`
    pub fn get_open_poll_sessions(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/poll_sessions/opened", self.id),
            vec![],
        )
    }

    /// Stream closed poll sessions for this user.
    ///
    /// # Canvas API
    /// `GET /api/v1/users/:id/poll_sessions/closed`
    pub fn get_closed_poll_sessions(&self) -> PageStream<serde_json::Value> {
        PageStream::new(
            Arc::clone(self.req()),
            &format!("users/{}/poll_sessions/closed", self.id),
            vec![],
        )
    }
}

/// The currently authenticated user (extends User with additional fields).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CurrentUser {
    pub id: u64,
    pub name: Option<String>,
    pub sortable_name: Option<String>,
    pub short_name: Option<String>,
    pub sis_user_id: Option<String>,
    pub login_id: Option<String>,
    pub email: Option<String>,
    pub avatar_url: Option<String>,
    pub locale: Option<String>,
    pub last_login: Option<DateTime<Utc>>,
    pub time_zone: Option<String>,
    pub bio: Option<String>,
    pub effective_locale: Option<String>,
}

/// A user display stub (id + name only) used in nested contexts.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UserDisplay {
    pub id: u64,
    pub display_name: Option<String>,
    pub avatar_image_url: Option<String>,
    pub html_url: Option<String>,
}

/// Identifies a user by numeric ID or as the currently authenticated user.
pub enum UserId {
    Id(u64),
    Current,
}

impl UserId {
    pub(crate) fn to_path_segment(&self) -> String {
        match self {
            UserId::Id(id) => id.to_string(),
            UserId::Current => "self".to_string(),
        }
    }
}