11use isolang:: Language ;
2+ use reqwest:: Url ;
23use std:: path:: Path ;
34use std:: path:: PathBuf ;
45
56use std:: env;
6- use tmc_client:: {
7- ClientError , Course , CourseDetails , CourseExercise , ExercisesDetails , NewSubmission ,
8- Organization , SubmissionFinished , TmcClient , Token ,
7+ use tmc_client:: response :: {
8+ Course , CourseDetails , CourseExercise , ExercisesDetails , NewSubmission , Organization ,
9+ SubmissionFinished ,
910} ;
11+ use tmc_client:: { ClientError , TmcClient , Token } ;
1012use tmc_langs:: Credentials ;
1113use tmc_langs:: DownloadOrUpdateCourseExercisesResult ;
1214use tmc_langs:: DownloadResult ;
@@ -33,26 +35,26 @@ pub trait Client {
3335 fn list_courses ( & mut self ) -> Result < Vec < Course > , String > ;
3436 fn get_organizations ( & mut self ) -> Result < Vec < Organization > , String > ;
3537 fn logout ( & mut self ) ;
36- fn wait_for_submission ( & self , submission_url : & str ) -> Result < SubmissionFinished , ClientError > ;
38+ fn wait_for_submission ( & self , submission_url : Url ) -> Result < SubmissionFinished , ClientError > ;
3739 fn submit (
3840 & self ,
3941 projects_dir : & Path ,
4042 course_slug : & str ,
4143 exercise_slug : & str ,
4244 locale : Option < Language > ,
4345 ) -> Result < NewSubmission , LangsError > ;
44- fn get_course_exercises ( & mut self , course_id : usize ) -> Result < Vec < CourseExercise > , String > ;
46+ fn get_course_exercises ( & mut self , course_id : u32 ) -> Result < Vec < CourseExercise > , String > ;
4547 fn get_exercise_details (
4648 & mut self ,
47- exercise_ids : Vec < usize > ,
49+ exercise_ids : Vec < u32 > ,
4850 ) -> Result < Vec < ExercisesDetails > , String > ;
4951 fn download_or_update_exercises (
5052 & mut self ,
51- download_params : & [ usize ] ,
53+ download_params : & [ u32 ] ,
5254 path : & Path ,
5355 ) -> Result < DownloadResult , LangsError > ;
5456 fn is_test_mode ( & mut self ) -> bool ;
55- fn get_course_details ( & self , course_id : usize ) -> Result < CourseDetails , ClientError > ;
57+ fn get_course_details ( & self , course_id : u32 ) -> Result < CourseDetails , ClientError > ;
5658 fn get_organization ( & self , organization_slug : & str ) -> Result < Organization , ClientError > ;
5759 fn update_exercises (
5860 & mut self ,
@@ -69,10 +71,11 @@ pub trait Client {
6971}
7072
7173static SERVER_ADDRESS : & str = "https://tmc.mooc.fi" ;
74+
7275impl ClientProduction {
7376 pub fn new ( test_mode : bool ) -> Self {
7477 let ( tmc_client, _credentials) = tmc_langs:: init_tmc_client_with_credentials (
75- SERVER_ADDRESS . to_string ( ) ,
78+ Url :: parse ( SERVER_ADDRESS ) . expect ( "Server address should always be correct." ) ,
7679 PLUGIN ,
7780 "1.0.0" ,
7881 )
@@ -165,12 +168,11 @@ impl Client for ClientProduction {
165168 }
166169
167170 if let Some ( credentials) = get_credentials ( ) {
168- match self . tmc_client . set_token ( credentials. token ( ) ) {
169- Ok ( ( ) ) => return Ok ( ( ) ) ,
170- _ => return Err ( "Setting login token failed" . to_string ( ) ) ,
171- }
171+ self . tmc_client . set_token ( credentials. token ( ) ) ;
172+ Ok ( ( ) )
173+ } else {
174+ Err ( "No login found. You need to be logged in to use this command" . to_string ( ) )
172175 }
173- Err ( "No login found. You need to be logged in to use this command" . to_string ( ) )
174176 }
175177
176178 fn try_login ( & mut self , username : String , password : String ) -> Result < String , String > {
@@ -256,7 +258,7 @@ impl Client for ClientProduction {
256258 }
257259 Ok ( course_list)
258260 }
259- Err ( ClientError :: NotLoggedIn ) => {
261+ Err ( ClientError :: NotAuthenticated ) => {
260262 Err ( "Login token is invalid. Please try logging in again." . to_string ( ) )
261263 }
262264 _ => Err ( "Unknown error. Please try again." . to_string ( ) ) ,
@@ -318,8 +320,8 @@ impl Client for ClientProduction {
318320 credentials. remove ( ) . unwrap ( ) ;
319321 }
320322
321- fn wait_for_submission ( & self , submission_url : & str ) -> Result < SubmissionFinished , ClientError > {
322- self . tmc_client . wait_for_submission ( submission_url)
323+ fn wait_for_submission ( & self , submission_url : Url ) -> Result < SubmissionFinished , ClientError > {
324+ self . tmc_client . wait_for_submission_at ( submission_url)
323325 }
324326 fn update_exercises (
325327 & mut self ,
@@ -359,7 +361,7 @@ impl Client for ClientProduction {
359361 )
360362 }
361363
362- fn get_course_exercises ( & mut self , course_id : usize ) -> Result < Vec < CourseExercise > , String > {
364+ fn get_course_exercises ( & mut self , course_id : u32 ) -> Result < Vec < CourseExercise > , String > {
363365 if self . test_mode {
364366 return Ok ( vec ! [ CourseExercise {
365367 id: 0 ,
@@ -376,7 +378,7 @@ impl Client for ClientProduction {
376378 }
377379 match self . tmc_client . get_course_exercises ( course_id) {
378380 Ok ( exercises) => Ok ( exercises) ,
379- Err ( ClientError :: NotLoggedIn ) => {
381+ Err ( ClientError :: NotAuthenticated ) => {
380382 Err ( "Login token is invalid. Please try logging in again." . to_string ( ) )
381383 }
382384 _ => Err ( "Unknown error. Please try again." . to_string ( ) ) ,
@@ -385,14 +387,15 @@ impl Client for ClientProduction {
385387
386388 fn get_exercise_details (
387389 & mut self ,
388- exercise_ids : Vec < usize > ,
390+ exercise_ids : Vec < u32 > ,
389391 ) -> Result < Vec < ExercisesDetails > , String > {
390392 if self . test_mode {
391393 return Ok ( vec ! [ ExercisesDetails {
392394 id: 0 ,
393395 course_name: "test_course" . to_string( ) ,
394396 exercise_name: "test_exercise" . to_string( ) ,
395397 checksum: "test_checksum" . to_string( ) ,
398+ hide_submission_results: false ,
396399 } ] ) ;
397400 }
398401 match self . tmc_client . get_exercises_details ( & exercise_ids) {
@@ -403,7 +406,7 @@ impl Client for ClientProduction {
403406
404407 fn download_or_update_exercises (
405408 & mut self ,
406- exercise_ids : & [ usize ] ,
409+ exercise_ids : & [ u32 ] ,
407410 path : & Path ,
408411 ) -> Result < DownloadResult , LangsError > {
409412 if self . test_mode {
@@ -416,7 +419,7 @@ impl Client for ClientProduction {
416419 tmc_langs:: download_or_update_course_exercises ( & self . tmc_client , path, exercise_ids, true )
417420 }
418421
419- fn get_course_details ( & self , course_id : usize ) -> Result < CourseDetails , ClientError > {
422+ fn get_course_details ( & self , course_id : u32 ) -> Result < CourseDetails , ClientError > {
420423 if self . test_mode {
421424 let course = Course {
422425 id : 0 ,
@@ -496,11 +499,11 @@ pub fn set_organization(org: &str) -> Result<(), String> {
496499 Ok ( ( ) )
497500}
498501
499- /// Returns course id as: Ok(Some(usize )) or Ok(None) if not found, Err(msg) if could not get id list
502+ /// Returns course id as: Ok(Some(u32 )) or Ok(None) if not found, Err(msg) if could not get id list
500503pub fn get_course_id_by_name (
501504 client : & mut dyn Client ,
502505 course_name : String ,
503- ) -> Result < Option < usize > , String > {
506+ ) -> Result < Option < u32 > , String > {
504507 match client. list_courses ( ) {
505508 Ok ( courses) => {
506509 for course in courses {
0 commit comments