@@ -14,11 +14,11 @@ use reqwest::{
1414} ;
1515use serde:: de:: DeserializeOwned ;
1616use serde_json:: Value ;
17- use std:: collections:: HashMap ;
1817use std:: path:: Path ;
1918use std:: time:: SystemTime ;
19+ use std:: { collections:: HashMap , io:: Write } ;
2020use tmc_langs_plugins:: Language ;
21- use tmc_langs_util:: { file_util , FileError } ;
21+ use tmc_langs_util:: FileError ;
2222use url:: Url ;
2323
2424/// Provides a wrapper for reqwest Response's json that deserializes into Response<T> and converts it into a result
@@ -121,15 +121,14 @@ impl TmcClient {
121121 . json_res ( )
122122 }
123123
124- fn download ( & self , url_tail : & str , target : & Path ) -> Result < ( ) , ClientError > {
124+ fn download ( & self , url_tail : & str , mut target : impl Write ) -> Result < ( ) , ClientError > {
125125 let url = self
126126 . 0
127127 . api_url
128128 . join ( url_tail)
129129 . map_err ( |e| ClientError :: UrlParse ( url_tail. to_string ( ) , e) ) ?;
130130
131131 // download zip
132- let mut target_file = file_util:: create_file ( target) ?;
133132 log:: debug!( "downloading {}" , url) ;
134133 let mut response = self
135134 . 0
@@ -168,24 +167,26 @@ impl TmcClient {
168167 }
169168 } else {
170169 response
171- . copy_to ( & mut target_file )
172- . map_err ( |e| ClientError :: HttpWriteResponse ( target . to_path_buf ( ) , e ) ) ?;
170+ . copy_to ( & mut target )
171+ . map_err ( ClientError :: HttpWriteResponse ) ?;
173172 Ok ( ( ) )
174173 }
175174 }
176175
177- pub ( crate ) fn download_from ( & self , url : Url , target : & Path ) -> Result < ( ) , ClientError > {
178- // download zip
179- let mut target_file = file_util:: create_file ( target) ?;
176+ pub ( crate ) fn download_from (
177+ & self ,
178+ url : Url ,
179+ mut target : impl Write ,
180+ ) -> Result < ( ) , ClientError > {
180181 log:: debug!( "downloading {}" , url) ;
181182 self . 0
182183 . client
183184 . get ( url. clone ( ) )
184185 . tmc_headers ( self )
185186 . send ( )
186187 . map_err ( |e| ClientError :: ConnectionError ( Method :: GET , url, e) ) ?
187- . copy_to ( & mut target_file )
188- . map_err ( |e| ClientError :: HttpWriteResponse ( target . to_path_buf ( ) , e ) ) ?;
188+ . copy_to ( & mut target )
189+ . map_err ( ClientError :: HttpWriteResponse ) ?;
189190 Ok ( ( ) )
190191 }
191192
@@ -578,7 +579,7 @@ impl TmcClient {
578579 organization_slug : & str ,
579580 course_name : & str ,
580581 exercise_name : & str ,
581- target : & Path ,
582+ target : impl Write ,
582583 ) -> Result < ( ) , ClientError > {
583584 if self . 0 . token . is_none ( ) {
584585 return Err ( ClientError :: NotLoggedIn ) ;
@@ -639,7 +640,11 @@ impl TmcClient {
639640 todo ! ( "needs admin?" ) ;
640641 }
641642
642- pub fn download_exercise ( & self , exercise_id : usize , target : & Path ) -> Result < ( ) , ClientError > {
643+ pub fn download_exercise (
644+ & self ,
645+ exercise_id : usize ,
646+ target : impl Write ,
647+ ) -> Result < ( ) , ClientError > {
643648 let url_tail = format ! ( "core/exercises/{}/download" , exercise_id) ;
644649 self . download ( & url_tail, target)
645650 }
@@ -686,7 +691,7 @@ impl TmcClient {
686691 pub ( super ) fn download_solution (
687692 & self ,
688693 exercise_id : usize ,
689- target : & Path ,
694+ target : impl Write ,
690695 ) -> Result < ( ) , ClientError > {
691696 if self . 0 . token . is_none ( ) {
692697 return Err ( ClientError :: NotLoggedIn ) ;
@@ -800,7 +805,7 @@ impl TmcClient {
800805 pub ( super ) fn download_submission (
801806 & self ,
802807 submission_id : usize ,
803- target : & Path ,
808+ target : impl Write ,
804809 ) -> Result < ( ) , ClientError > {
805810 if self . 0 . token . is_none ( ) {
806811 return Err ( ClientError :: NotLoggedIn ) ;
0 commit comments