canvas_lms_connector 0.1.7

This library provides a range of functionalities for interacting with the Canvas Learning Management System 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
use crate::connection::{send_http_request, HttpMethod, SYNC_ATTEMPT};
use crate::{
    course, Assignment, AssignmentInfo, CanvasCredentials, Course, CourseInfo, Student,
    StudentInfo, Submission,
};
use course::parse_course_name;
use dialoguer::theme::ColorfulTheme;
use dialoguer::Select;
use reqwest::blocking::multipart::{Form, Part};
use reqwest::blocking::Client;
use serde_json::Value;
use std::collections::HashMap;
use std::error::Error;
use std::sync::Arc;
use std::thread::sleep;
use serde_json::json;

/// Enum to represent the result of fetching multiple courses.
///
/// This enum provides a structured way to handle the outcomes of attempting to fetch a list of courses
/// from the Canvas LMS, distinguishing between successful retrieval, connection errors, and credential errors.
pub enum CanvasResultCourses {
    Ok(Vec<Course>),        // Success case with a vector of Course objects.
    ErrConnection(String),  // Connection error with a descriptive message.
    ErrCredentials(String), // Credential error with a descriptive message.
}

/// Enum to represent the result of fetching a single course.
///
/// Similar to `CanvasResultCourses`, but tailored for scenarios where only a single course is being fetched.
/// Distinguishes between success, connection errors, and credential errors.
pub enum CanvasResultSingleCourse {
    Ok(Course),             // Success case with a single Course object.
    ErrConnection(String),  // Connection error with a descriptive message.
    ErrCredentials(String), // Credential error with a descriptive message.
}

/// Main interface for interacting with the Canvas LMS.
///
/// `Canvas` struct is designed as a centralized point for accessing Canvas LMS functionalities.
/// It enables operations like fetching courses and handling authentication, encapsulating the logic
/// for Canvas API interactions.
///
/// Example:
/// ```
/// let canvas_credentials = CanvasCredentials { /* initialization */ };
/// let canvas = Canvas { /* fields initialization */ };
/// match canvas.fetch_courses_with_credentials(&canvas_credentials) {
///     CanvasResultCourses::Ok(courses) => println!("Courses: {:?}", courses),
///     CanvasResultCourses::ErrConnection(err) => eprintln!("Connection error: {}", err),
///     CanvasResultCourses::ErrCredentials(err) => eprintln!("Credentials error: {}", err),
/// }
/// ```
pub struct Canvas {
    // info: Arc<CanvasInfo>,
}

/// Implementation block for the `Canvas` struct.
///
/// This section provides various methods to interact with the Canvas LMS, encapsulating the logic
/// necessary for operations like fetching course lists, retrieving individual course details,
/// managing Canvas credentials, and other API interactions specific to the Canvas system.
///
/// The methods are designed to streamline the process of communicating with the Canvas API,
/// handling authentication, data retrieval, and error management.
///
/// # Methods
///
/// - `fetch_courses_with_credentials`: Fetches a list of courses using specific Canvas credentials.
///   It's useful when you have multiple Canvas accounts or need to access courses under different credentials.
///
/// - `fetch_single_course_with_credentials`: Retrieves detailed information about a specific course,
///   identified by its ID, using the provided Canvas credentials. This method is particularly useful for
///   applications or services that need to focus on a single course at a time, such as a course management
///   dashboard or a student information system.
///
/// - `convert_json_to_course`: A utility function within the `Canvas` context, used by other methods to
///   transform JSON data received from the Canvas API into a structured `Course` object. This function
///   encapsulates the parsing logic, ensuring consistent conversion across different parts of the application.
///
/// Each of these methods is designed to target a specific aspect of Canvas LMS interaction, ensuring that
/// the `Canvas` struct can be used flexibly in various application contexts.
impl Canvas {
    /// Fetches a list of courses using provided Canvas credentials.
    ///
    /// Communicates with the Canvas API to retrieve accessible courses. Requires valid Canvas API credentials.
    /// Handles pagination to ensure all courses are fetched.
    ///
    /// Arguments:
    /// - `info`: Reference to `CanvasCredentials` containing API URL and token.
    ///
    /// Returns:
    /// - `CanvasResultCourses`: Enum indicating success with course list or an error.
    ///
    /// Example:
    /// ```
    /// let canvas_info = CanvasCredentials { /* initialization */ };
    /// match Canvas::fetch_courses_with_credentials(&canvas_info) {
    ///     CanvasResultCourses::Ok(courses) => /* handle courses */,
    ///     // Handle errors...
    /// }
    /// ```
    pub fn fetch_courses_with_credentials(info: &CanvasCredentials) -> CanvasResultCourses {
        let canvas_info_arc = Arc::new((*info).clone());

        let url = format!("{}/courses", info.url_canvas);
        let mut all_courses = Vec::new();
        let mut page = 1;
        let client = &Client::new();

        loop {
            let params = vec![
                (
                    "enrollment_role".to_string(),
                    "TeacherEnrollment".to_string(),
                ),
                ("page".to_string(), page.to_string()),
                ("per_page".to_string(), "100".to_string()),
            ];
            match send_http_request(&client, HttpMethod::Get, &url, &info, params) {
                Ok(response) => {
                    if response.status().is_success() {
                        match response.text() {
                            Ok(text) => {
                                // println!("Response Text: {}", text);

                                // Se precisar processar como JSON, converta novamente
                                match serde_json::from_str::<Vec<serde_json::Value>>(&text) {
                                    Ok(courses) => {
                                        if courses.is_empty() {
                                            break; // Sai do loop se nenhum curso for retornado
                                        }
                                        all_courses.extend(courses.iter().filter_map(|course| {
                                            Canvas::convert_json_to_course(&canvas_info_arc, course)
                                        }));
                                        page += 1; // Incrementa o número da página
                                    }
                                    Err(e) => {
                                        // error!("Failed to parse courses JSON with error: {}", e);
                                        return CanvasResultCourses::ErrCredentials(format!(
                                            "Failed to parse courses JSON with error: {}",
                                            e
                                        ));
                                    }
                                }
                            }
                            Err(e) => {
                                // error!("Failed to read response text with error: {}", e);
                                return CanvasResultCourses::ErrCredentials(format!(
                                    "Failed to read response text with error: {}",
                                    e
                                ));
                            }
                        }
                    } else {
                        // error!("Failed to fetch courses with status: {}", response.status());
                        return CanvasResultCourses::ErrCredentials(format!(
                            "Failed to fetch courses with status: {}",
                            response.status()
                        ));
                    }
                }
                Err(e) => {
                    // error!("Failed to fetch courses with error: {}", e);
                    return CanvasResultCourses::ErrConnection(format!(
                        "Failed to fetch courses with error: {}",
                        e
                    ));
                }
            }
        }

        CanvasResultCourses::Ok(all_courses)
    }

    /// Fetches a specific course using provided credentials.
    ///
    /// Retrieves details of a single course based on its ID. Utilizes Canvas credentials for authentication.
    ///
    /// Arguments:
    /// - `info`: Canvas API credentials.
    /// - `course_id`: ID of the course to fetch.
    ///
    /// Returns:
    /// - `CanvasResultSingleCourse`: Enum indicating success with the course or an error.
    ///
    /// Example:
    /// ```
    /// let canvas_info = CanvasCredentials { /* initialization */ };
    /// let course_id = 123;
    /// match Canvas::fetch_single_course_with_credentials(&canvas_info, course_id) {
    ///     CanvasResultSingleCourse::Ok(course) => /* handle course */,
    ///     // Handle errors...
    /// }
    /// ```
    pub fn fetch_single_course_with_credentials(
        info: &CanvasCredentials,
        course_id: u64,
    ) -> CanvasResultSingleCourse {
        let canvas_info_arc = Arc::new((*info).clone());
        let url = format!("{}/courses/{}", info.url_canvas, course_id);

        match send_http_request(
            &Client::new(),
            HttpMethod::Get,
            &url,
            info,
            Vec::new(), // No additional parameters for this request
        ) {
            Ok(response) => {
                if response.status().is_success() {
                    let course: serde_json::Value = response.json().unwrap();
                    if let Some(course) = Canvas::convert_json_to_course(&canvas_info_arc, &course)
                    {
                        return CanvasResultSingleCourse::Ok(course);
                    } else {
                        return CanvasResultSingleCourse::ErrConnection(
                            "Failed to parse course data".to_string(),
                        );
                    }
                } else {
                    CanvasResultSingleCourse::ErrConnection(format!(
                        "Failed to fetch course: HTTP Status {}",
                        response.status()
                    ))
                }
            }
            Err(e) => {
                CanvasResultSingleCourse::ErrConnection(format!("HTTP request failed: {}", e))
            }
        }
    }

    /// Converts a JSON object to a `Course`.
    ///
    /// Parses JSON data from the Canvas API to construct a `Course` object.
    ///
    /// Arguments:
    /// - `canvas_info`: Shared credentials reference.
    /// - `course`: JSON object representing a course.
    ///
    /// Returns:
    /// - `Option<Course>`: A course if successful, or `None` if conversion fails.
    ///
    /// Example:
    /// ```
    /// let canvas_info = Arc::new(CanvasCredentials { /* initialization */ });
    /// let course_json = serde_json::json!({ /* JSON data */ });
    /// let course = Canvas::convert_json_to_course(&canvas_info, &course_json);
    /// ```
    fn convert_json_to_course(
        canvas_info: &Arc<CanvasCredentials>,
        course: &serde_json::Value,
    ) -> Option<Course> {
        let id = course["id"].as_u64()?;
        let name = course["name"].as_str().map(String::from)?;
        let course_code = course["course_code"].as_str().map(String::from)?;
        Some(Course {
            info: Arc::new(CourseInfo {
                id,
                name: name.clone(),
                course_code: course_code.clone(),
                canvas_info: Arc::clone(canvas_info),
                abbreviated_name: parse_course_name(name.as_str(), course_code.as_str()), // Parse the course name
            }),
        })
    }

    pub fn choose_course() -> Option<Course> {
        let mut menu_str = Vec::new();
        let mut menu_course = Vec::new();

        let credentials = CanvasCredentials::credentials();
        println!("Fetching courses...");
        match Canvas::fetch_courses_with_credentials(&credentials) {
            CanvasResultCourses::Ok(courses) => {
                for course in courses {
                    if let Some(course_details_name) = parse_course_name(
                        course.info.name.as_str(),
                        course.info.course_code.as_str(),
                    ) {
                        menu_str.push(course_details_name.abbreviated_name);
                        menu_course.push(course);
                    }
                }
            }
            CanvasResultCourses::ErrConnection(msg) => {
                eprintln!("Connection error: {}", msg);
                std::process::exit(1);
            }
            CanvasResultCourses::ErrCredentials(msg) => {
                eprintln!("Credential error: {}", msg);
                std::process::exit(1);
            }
        }

        // Add EXIT at the end of the list
        menu_str.push("EXIT".to_string());

        let selection = Select::with_theme(&ColorfulTheme::default())
            .with_prompt("Choose a course")
            .items(&menu_str)
            .default(0)
            .interact()
            .unwrap();

        if selection == menu_str.len() - 1 {
            return None;
        }

        Some(menu_course[selection].clone())
    }
}

/// Returns the current year and semester as a tuple of strings.
///
/// The year is represented as a four-digit number (e.g., "2023"). The semester
/// is determined based on the current month and day: "1" for dates on or before
/// July 15 (first semester), and "2" for dates after July 15 (second semester).
///
/// # Examples
///
/// ```
/// let (year, semester) = get_current_year_and_semester();
/// println!("Year: {}, Semester: {}", year, semester);
/// ```
///
/// # Errors
///
/// This function does not return any errors. It will always provide the current year and
/// the calculated semester based on the current date.
pub fn get_current_year_and_semester() -> (String, String) {
    use chrono::{Datelike, Utc};

    let current_date = Utc::now();
    let year = current_date.year().to_string();
    let semester =
        if current_date.month() < 7 || (current_date.month() == 7 && current_date.day() <= 15) {
            "1".to_string()
        } else {
            "2".to_string()
        };

    (year, semester)
}

/// Adds a comment to a student's assignment submission.
///
/// This function sends an HTTP PUT request to add a comment to a specific
/// assignment submission by a student. Optionally, it can include file IDs
/// associated with the comment.
///
/// Arguments:
/// - `client`: HTTP client for executing requests.
/// - `assignment_id`: ID of the assignment.
/// - `user_id`: ID of the user (student).
/// - `comment_text`: Text content of the comment.
/// - `file_ids`: Optional vector of file IDs to be attached to the comment.
///
/// Returns:
/// - `Result<(), Box<dyn Error>>`: Success or an error detailing any issues encountered.
///
/// Example:
/// ```
/// let client = Client::new();
/// let course = Course { /* ... */ };
/// match course.add_comment(&client, "assignment_id", "user_id", "Great work!", None) {
///     Ok(_) => /* handle success */,
///     Err(e) => /* handle error */,
/// }
/// ```
fn add_comment(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_id: &str,
    user_id: &str,
    comment_text: &str,
    file_ids: Option<Vec<i64>>,
) -> Result<(), Box<dyn Error>> {
    let url = format!(
        "{}/courses/{}/assignments/{}/submissions/{}",
        canvas_info.url_canvas, course_id, assignment_id, user_id
    );

    let mut body = serde_json::json!({
        "comment": {
            "text_comment": comment_text
        }
    });

    if let Some(file_ids) = file_ids {
        body["comment"]["file_ids"] = serde_json::json!(file_ids);
    }

    send_http_request(client, HttpMethod::Put(body), &url, &canvas_info, vec![])
        .map_err(|e| format!("Failed to add comment: {}", e))?;
    Ok(())
}

/// Requests an upload token from the Canvas LMS.
///
/// This function sends an HTTP POST request to the Canvas LMS to request an upload token
/// for uploading a file. It requires details about the assignment, user, file name, and file size.
///
/// Arguments:
/// - `client`: HTTP client for executing requests.
/// - `assignment_id`: ID of the assignment.
/// - `user_id`: ID of the user (student).
/// - `file_name`: Name of the file to be uploaded.
/// - `file_size`: Size of the file to be uploaded.
///
/// Returns:
/// - `Result<(String, HashMap<String, String>), Box<dyn Error>>`: Tuple containing the upload URL
///   and a map of upload parameters if successful, or an error detailing any issues encountered.
///
/// Example:
/// ```
/// let client = Client::new();
/// let course = Course { /* ... */ };
/// match course.request_upload_token(&client, "assignment_id", "user_id", "test.pdf", 12345) {
///     Ok((upload_url, upload_params)) => /* handle success */,
///     Err(e) => /* handle error */,
/// }
/// ```
pub fn request_upload_token(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_id: &str,
    user_id: &str,
    file_name: &str,
    file_size: u64,
) -> Result<(String, HashMap<String, String>), Box<dyn Error>> {
    // Construindo a URL de solicitação
    let url = format!(
        "{}/courses/{}/assignments/{}/submissions/{}/comments/files",
        canvas_info.url_canvas, course_id, assignment_id, user_id
    );

    // Construindo o corpo da requisição
    let body = serde_json::json!({
        "name": file_name,
        "size": file_size
    });

    // Enviando a solicitação HTTP
    match send_http_request(
        client,
        HttpMethod::Post(body), // Usar a variante HttpMethod::Post com corpo JSON
        &url,
        &canvas_info,
        vec![], // POST request não necessita de params
    ) {
        Ok(response) => {
            // Verificando se a resposta foi bem-sucedida
            if response.status().is_success() {
                // Parseando a resposta JSON
                let json_response: serde_json::Value = response.json()?;
                let upload_url = json_response["upload_url"]
                    .as_str()
                    .ok_or("Missing upload_url")?
                    .to_string();
                let upload_params = json_response["upload_params"]
                    .as_object()
                    .ok_or("Missing upload_params")?;

                let mut params = HashMap::new();
                for (key, value) in upload_params {
                    let value_str = value.as_str().ok_or("Invalid param value")?;
                    params.insert(key.clone(), value_str.to_string());
                }

                Ok((upload_url, params))
            } else {
                Err(Box::new(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!(
                        "Failed to request upload token with status: {}",
                        response.status()
                    ),
                )))
            }
        }
        Err(e) => Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::Other,
            format!("Failed to request upload token with error: {}", e),
        ))),
    }
}

/// Uploads a file to the Canvas LMS.
///
/// This function handles the file upload process by first requesting an upload token
/// and then using that token to upload the file. It reads the file content and sends it
/// as a multipart/form-data request to the provided upload URL.
///
/// Arguments:
/// - `client`: HTTP client for executing requests.
/// - `assignment_id`: ID of the assignment.
/// - `user_id`: ID of the user (student).
/// - `file_path`: Path to the file to be uploaded.
///
/// Returns:
/// - `Result<i64, Box<dyn Error>>`: File ID if the upload is successful, or an error detailing
///   any issues encountered.
///
/// Example:
/// ```
/// let client = Client::new();
/// let course = Course { /* ... */ };
/// match course.upload_file(&client, "assignment_id", "user_id", "path/to/file.pdf") {
///     Ok(file_id) => /* handle success */,
///     Err(e) => /* handle error */,
/// }
/// ```
fn upload_file(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_id: &str,
    user_id: &str,
    file_path: &str,
) -> Result<i64, Box<dyn Error>> {
    use std::fs::File;
    use std::io::Read;

    let file_name = std::path::Path::new(file_path)
        .file_name()
        .and_then(std::ffi::OsStr::to_str)
        .ok_or("Invalid file name")?;

    let file_size = std::fs::metadata(file_path)?.len();

    match request_upload_token(
        client,
        canvas_info,
        course_id,
        assignment_id,
        user_id,
        file_name,
        file_size,
    ) {
        Ok((upload_url, upload_params)) => {
            // println!("Received upload token: {}", upload_url);
            // println!("Received upload params: {:?}", upload_params);

            let mut file = File::open(file_path)?;
            let mut file_content = Vec::new();
            file.read_to_end(&mut file_content)?;

            let mut form = Form::new();
            for (key, value) in upload_params {
                form = form.text(key, value);
            }
            form = form.file("file", file_path)?;

            let response = client
                .post(&upload_url)
                .multipart(form)
                .send()
                .map_err(|e| format!("Failed to upload file: {}", e))?;

            let json: Value = response
                .json()
                .map_err(|e| format!("Failed to parse upload file response: {}", e))?;

            // println!("Upload response JSON: {:?}", json);

            let file_id = json["id"]
                .as_i64()
                .ok_or("Missing id in upload file response")?;

            Ok(file_id)
        }
        Err(e) => {
            return Err(format!("Failed to request upload token: {}", e).into());
        }
    }
}

/// Adds a file comment to a student's assignment submission.
///
/// This function first uploads a file to the Canvas LMS and then attaches it as a comment
/// to a specific assignment submission by a student. It also adds text content to the comment.
///
/// Arguments:
/// - `client`: HTTP client for executing requests.
/// - `assignment_id`: ID of the assignment.
/// - `student_id`: ID of the student.
/// - `file_path`: Optional path to the file to be uploaded.
/// - `comment_text`: Text content of the comment.
///
/// Returns:
/// - `Result<(), Box<dyn Error>>`: Success or an error detailing any issues encountered.
///
/// Example:
/// ```
/// let client = Client::new();
/// let course = Course { /* ... */ };
/// match course.add_file_comment(&client, assignment_id, student_id, Some("path/to/file.pdf"), "Great work!") {
///     Ok(_) => /* handle success */,
///     Err(e) => /* handle error */,
/// }
/// ```
pub fn comment_with_file(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_id: u64,
    student_id: u64,
    file_path: Option<&str>,
    comment_text: &str,
) -> Result<(), Box<dyn Error>> {
    // println!("Course ID: {}", self.info.id);
    // println!("Assignment ID: {}", assignment_id);
    // println!("Student ID: {}", student_id);

    let user_id = student_id.to_string();
    let assignment_id_str = assignment_id.to_string();

    let file_ids = if let Some(path) = file_path {
        let file_id = upload_file(
            client,
            canvas_info,
            course_id,
            &assignment_id_str,
            &user_id,
            path,
        )
        .map_err(|e| format!("Error in upload_file: {}", e))?;
        Some(vec![file_id])
    } else {
        None
    };

    add_comment(
        client,
        canvas_info,
        course_id,
        &assignment_id_str,
        &user_id,
        comment_text,
        file_ids,
    )
    .map_err(|e| format!("Error in add_comment: {}", e))?;

    Ok(())
}

/// Retrieves all submissions for a specific assignment for all students in a course.
///
/// This function sends an HTTP GET request to the Canvas LMS API to retrieve all submissions
/// for a given assignment in a specified course.
///
/// Arguments:
/// - `client`: HTTP client for executing requests.
/// - `canvas_info`: Reference to Canvas credentials and configuration.
/// - `course_id`: ID of the course.
/// - `assignment_id`: ID of the assignment.
///
/// Returns:
/// - `Result<serde_json::Value, Box<dyn Error>>`: JSON response containing the submissions
///   or an error detailing any issues encountered.
///
/// Example:
/// ```
/// let client = Client::new();
/// let canvas_info = CanvasCredentials { /* ... */ };
/// match get_all_submissions(&client, &canvas_info, 32451, "174964") {
///     Ok(submissions) => /* handle success */,
///     Err(e) => /* handle error */,
/// }
/// ```
pub fn get_all_submissions(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_id: u64,
) -> Result<Vec<Value>, Box<dyn Error>> {
    let url = format!(
        "{}/courses/{}/assignments/{}/submissions",
        canvas_info.url_canvas, course_id, assignment_id
    );

    let mut all_submissions = Vec::new();
    let mut page = 1;
    loop {
        let params = vec![("page", page.to_string()), ("per_page", "100".to_string())];

        // Convertendo (&str, String) para (String, String)
        let converted_params: Vec<(String, String)> = params
            .into_iter()
            .map(|(key, value)| (key.to_string(), value))
            .collect();

        match send_http_request(
            client,
            HttpMethod::Get,
            &url,
            canvas_info,
            converted_params, // Passando o Vec<(String, String)> diretamente
        ) {
            Ok(response) => {
                if response.status().is_success() {
                    let submissions_page: Vec<Value> = response.json()?;
                    if submissions_page.is_empty() {
                        break; // Sai do loop se não há mais submissões
                    }
                    all_submissions.extend(submissions_page);
                    page += 1; // Incrementa o número da página para a próxima iteração
                } else {
                    return Err(Box::new(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        format!(
                            "Failed to fetch submissions with status: {}",
                            response.status()
                        ),
                    )));
                }
            }
            Err(e) => {
                return Err(Box::new(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!("Failed to fetch submissions with error: {}", e),
                )));
            }
        }
    }
    Ok(all_submissions)
}

pub fn fetch_submissions_for_assignments<F>(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    user_id: u64,
    assignment_ids: &[u64],
    interaction: F,
) -> Result<Vec<Submission>, Box<dyn std::error::Error>>
where
    F: Fn(),
{
    let mut submissions = Vec::new();

    for &assignment_id in assignment_ids {
        // update_carrossel();
        let url = format!(
            "{}/courses/{}/assignments/{}/submissions/{}",
            canvas_info.url_canvas, course_id, assignment_id, user_id
        );

        // Não são necessários parâmetros adicionais para esta chamada de API específica
        let params = Vec::new(); // Sem parâmetros adicionais para a requisição GET

        interaction();
        // Try to send the HTTP request SYNC_ATTEMPT times
        for attempt in 0..SYNC_ATTEMPT {
            let response = send_http_request(
                client,
                HttpMethod::Get, // Método GET
                &url,            // URL da API
                &canvas_info,    // Token de acesso
                params.clone(),  // Parâmetros da requisição
            );
            match response {
                Ok(response) => {
                    if response.status().is_success() {
                        let submission: Submission = response.json()?; // Deserializar a resposta JSON para um objeto Submission
                        submissions.push(submission);
                    } else {
                        let error_message = response.text()?;
                        return Err(Box::new(std::io::Error::new(
                            std::io::ErrorKind::Other,
                            format!(
                                "Failed to fetch submissions with error: {} (a)",
                                error_message
                            ),
                        )));
                    }
                    break;
                }
                Err(e) => {
                    if attempt == SYNC_ATTEMPT - 1 {
                        return Err(Box::new(std::io::Error::new(
                            std::io::ErrorKind::Other,
                            format!("Failed to fetch submissions with error: {} (b)", e),
                        )));
                    } else {
                        sleep(std::time::Duration::from_millis(100));
                    }
                }
            }
        }
    }
    Ok(submissions)
}

pub fn fetch_students(course: &Course) -> Result<Vec<Student>, Box<dyn Error>> {
    let url = format!(
        "{}/courses/{}/users",
        course.info.canvas_info.url_canvas, course.info.id
    );

    /// Converts a JSON object to a `Student` structure.
    ///
    /// Parses a JSON representation of a student from the Canvas API into a `Student` object.
    /// Extracts student ID, name, and email and associates it with course information.
    pub fn convert_json_to_student(
        course_info: &Arc<CourseInfo>,
        student: &serde_json::Value,
    ) -> Option<Student> {
        let id = student["id"].as_u64()?;
        let name = student["name"].as_str().map(String::from)?;
        let email = student["email"].as_str().map(String::from)?;
        Some(Student {
            info: Arc::new(StudentInfo {
                id,
                name,
                email,
                course_info: Arc::clone(course_info),
            }),
        })
    }

    let mut all_students = Vec::new();
    let mut page = 1;
    let client = &Client::new();

    loop {
        let params = vec![
            ("enrollment_type[]", "student".to_string()),
            ("include[]", "email".to_string()),
            ("per_page", "150".to_string()),
            ("page", page.to_string()),
        ];

        // Convertendo (&str, String) para (String, String)
        let converted_params: Vec<(String, String)> = params
            .into_iter()
            .map(|(key, value)| (key.to_string(), value))
            .collect();

        // Passando HttpMethod::Get ao invés de "GET"
        match send_http_request(
            client,
            HttpMethod::Get, // Supondo que HttpMethod::Get é um enum definido em algum lugar
            &url,
            &course.info.canvas_info,
            converted_params, // Passando o Vec<(String, String)> diretamente
        ) {
            Ok(response) => {
                if response.status().is_success() {
                    let students_page: Vec<serde_json::Value> = response.json()?;
                    if students_page.is_empty() {
                        break; // Sai do loop se não há mais estudantes
                    }
                    all_students.extend(
                        students_page
                            .into_iter()
                            .filter_map(|student| convert_json_to_student(&course.info, &student)),
                    );
                    page += 1; // Incrementa o número da página para a próxima iteração
                } else {
                    return Err(Box::new(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        format!(
                            "Failed to fetch students with status: {}",
                            response.status()
                        ),
                    )));
                }
            }
            Err(e) => {
                return Err(Box::new(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!("Failed to fetch students with error: {}", e),
                )));
            }
        }
    }
    Ok(all_students)
}

pub fn fetch_assignments(course: &Course) -> Result<Vec<Assignment>, Box<dyn Error>> {
    /// Converts a JSON object into an `Assignment` structure.
    ///
    /// Transforms a JSON representation of an assignment into an `Assignment` object. Retrieves key
    /// details such as ID, name, and description, linking them with the course information.
    pub fn convert_json_to_assignment(
        course_info: &Arc<CourseInfo>,
        assignment: &serde_json::Value,
    ) -> Option<Assignment> {
        let id = assignment["id"].as_u64()?;
        let name = assignment["name"].as_str().map(String::from)?;
        let description = assignment["description"].as_str().map(String::from);
        Some(Assignment {
            info: Arc::new(AssignmentInfo {
                id,
                name,
                description,
                course_info: Arc::clone(course_info),
            }),
        })
    }

    let url = format!(
        "{}/courses/{}/assignments",
        course.info.canvas_info.url_canvas, course.info.id
    );

    let mut all_assignments = Vec::new();
    let mut page = 1;
    let client = &reqwest::blocking::Client::new();
    loop {
        // Construindo os parâmetros da requisição
        let params = vec![("page", page.to_string()), ("per_page", "100".to_string())];

        // Convertendo (&str, String) para (String, String)
        let converted_params: Vec<(String, String)> = params
            .into_iter()
            .map(|(key, value)| (key.to_string(), value))
            .collect();

        match send_http_request(
            client,
            HttpMethod::Get,
            &url,
            &course.info.canvas_info,
            converted_params, // Passando o Vec<(String, String)> diretamente
        ) {
            Ok(response) => {
                if response.status().is_success() {
                    let assignments_page: Vec<serde_json::Value> = response.json()?;
                    if assignments_page.is_empty() {
                        break; // Sai do loop se não há mais cursos
                    }
                    all_assignments.extend(assignments_page.into_iter().filter_map(|assignment| {
                        convert_json_to_assignment(&course.info, &assignment)
                    }));
                    page += 1; // Incrementa o número da página para a próxima iteração
                } else {
                    return Err(Box::new(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        format!(
                            "Failed to fetch assignments with status: {}",
                            response.status()
                        ),
                    )));
                }
            }
            Err(e) => {
                return Err(Box::new(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!("Failed to fetch assignments with error: {}", e),
                )));
            }
        }
    }
    Ok(all_assignments)
}

pub fn update_assignment_score(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_id: u64,
    student_id: u64,
    new_score: Option<f64>,
) -> Result<(), Box<dyn Error>> {
    let url = format!(
        "{}/courses/{}/assignments/{}/submissions/{}",
        canvas_info.url_canvas, course_id, assignment_id, student_id,
    );

    let body;
    if let Some(new_score) = new_score {
        body = serde_json::json!({
            "submission": {
                "posted_grade": new_score
            }
        });
    } else {
        body = serde_json::json!({
            "submission": {
                "posted_grade": ""
            }
        });
    }

    // Try to send the HTTP request SYNC_ATTEMPT times
    let mut attempt = SYNC_ATTEMPT;
    loop {
        match send_http_request(
            client,
            HttpMethod::Put(body.clone()), // Use HttpMethod::Put enum variant
            &url,
            &canvas_info,
            Vec::new(), // PUT request does not need params
        ) {
            Ok(response) => match response.status().is_success() {
                true => return Ok(()),
                false => {
                    if attempt == 0 {
                        return Err(Box::new(std::io::Error::new(
                            std::io::ErrorKind::Other,
                            format!("Failed to update score with status: {}", response.status()),
                        )));
                    }
                }
            },
            Err(e) => {
                if attempt == 0 {
                    return Err(Box::new(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        format!("Failed to update score with error: {}", e),
                    )));
                }
            }
        };
        attempt -= 1;
        sleep(std::time::Duration::from_millis(100));
    }
}

pub fn comment_with_binary_file(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_id: u64,
    student_id: u64,
    file_name: Option<&str>,
    file_content: Option<&Vec<u8>>,
    comment_text: &str,
) -> Result<(), Box<dyn Error>> {
    let user_id = student_id.to_string();
    let assignment_id_str = assignment_id.to_string();

    let file_ids = if let (Some(name), Some(content)) = (file_name, file_content) {
        let mut attempts = 0;
        let max_attempts = 3;
        loop {
            match upload_binary_file(
                client,
                canvas_info,
                course_id,
                &assignment_id_str,
                &user_id,
                name,
                content,
            ) {
                Ok(file_id) => break Some(vec![file_id]),
                Err(e) => {
                    attempts += 1;
                    if attempts >= max_attempts {
                        return Err(format!("Error in upload_binary_file after {} attempts: {}", attempts, e).into());
                    }
                    sleep(std::time::Duration::from_secs(1)); // Espera 1 segundo antes de tentar novamente
                }
            }
        }
    } else {
        None
    };

    add_comment(
        client,
        canvas_info,
        course_id,
        &assignment_id_str,
        &user_id,
        comment_text,
        file_ids,
    )
        .map_err(|e| format!("Error in add_comment: {}", e))?;

    Ok(())
}


fn upload_binary_file(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_id: &str,
    user_id: &str,
    file_name: &str,
    file_content: &Vec<u8>,
) -> Result<i64, Box<dyn Error>> {
    let file_size = file_content.len() as u64;

    match request_upload_token(
        client,
        canvas_info,
        course_id,
        assignment_id,
        user_id,
        file_name,
        file_size,
    ) {
        Ok((upload_url, upload_params)) => {
            let mut form = Form::new();
            for (key, value) in upload_params {
                form = form.text(key, value);
            }
            form = form.part(
                "file",
                Part::bytes(file_content.clone()).file_name(file_name.to_string()),
            );

            let response = client
                .post(&upload_url)
                .multipart(form)
                .send()
                .map_err(|e| format!("Failed to upload file: {}", e))?;

            let json: Value = response
                .json()
                .map_err(|e| format!("Failed to parse upload file response: {}", e))?;

            let file_id = json["id"]
                .as_i64()
                .ok_or("Missing id in upload file response")?;

            Ok(file_id)
        }
        Err(e) => {
            return Err(format!("Failed to request upload token: {}", e).into());
        }
    }
}

/// Cria uma nova atividade (assignment) em um curso no Canvas.
///
/// Esta função envia uma solicitação HTTP POST para a API do Canvas para criar uma nova atividade.
/// Requer o ID do curso, o nome da atividade e as credenciais do Canvas para autenticação.
///
/// Argumentos:
/// - `client`: Cliente HTTP para executar as requisições.
/// - `canvas_info`: Referência para as credenciais do Canvas.
/// - `course_id`: ID do curso onde a atividade será criada.
/// - `assignment_name`: Nome da nova atividade.
///
/// Retorna:
/// - `Result<(), Box<dyn Error>>`: Sucesso ou um erro detalhando quaisquer problemas encontrados.
///
/// Exemplo:
/// ```
/// let client = Client::new();
/// let canvas_info = CanvasCredentials { /* ... */ };
/// match create_assignment(&client, &canvas_info, 12345, "Nova Atividade") {
///     Ok(_) => println!("Atividade criada com sucesso!"),
///     Err(e) => eprintln!("Erro ao criar atividade: {}", e),
/// }
/// ```
pub fn create_assignment(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    assignment_name: &str,
) -> Result<(), Box<dyn Error>> {
    let url = format!("{}/courses/{}/assignments", canvas_info.url_canvas, course_id);

    let body = json!({
        "assignment": {
            "name": assignment_name,
            "points_possible": 10.0,
            "grading_type": "points",
            "submission_types": ["online_upload"],
            "published": true,
        }
    });

    match send_http_request(client, HttpMethod::Post(body), &url, canvas_info, vec![]) {
        Ok(response) => {
            if response.status().is_success() {
                println!("Atividade '{}' criada com sucesso!", assignment_name);
                Ok(())
            } else {
                Err(Box::new(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!(
                        "Falha ao criar atividade com status: {}",
                        response.status()
                    ),
                )))
            }
        }
        Err(e) => Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::Other,
            format!("Falha ao criar atividade com erro: {}", e),
        ))),
    }
}

pub fn create_announcement(
    client: &Client,
    canvas_info: &CanvasCredentials,
    course_id: u64,
    title: &str,
    message: &str,
) -> Result<(), Box<dyn Error>> {
    let url = format!("{}/courses/{}/discussion_topics", canvas_info.url_canvas, course_id);

    let body = json!({
        "title": title,
        "message": message,
        "is_announcement": true
    });

    match send_http_request(
        client,
        HttpMethod::Post(body),
        &url,
        canvas_info,
        vec![],
    ) {
        Ok(response) => {
            if response.status().is_success() {
                Ok(())
            } else {
                Err(Box::new(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!("Failed to create announcement with status: {}", response.status()),
                )))
            }
        }
        Err(e) => Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::Other,
            format!("Failed to create announcement with error: {}", e),
        ))),
    }
}