@@ -13,12 +13,8 @@ use url::Url;
1313
1414use std:: { collections:: HashMap , path:: PathBuf , time:: Duration } ;
1515
16- #[ cfg( feature = "reqwest-client" ) ]
1716pub use reqwest:: header;
1817
19- #[ cfg( not( feature = "reqwest-client" ) ) ]
20- pub use attohttpc:: header;
21-
2218use header:: { HeaderName , HeaderValue } ;
2319
2420#[ derive( Deserialize ) ]
@@ -73,13 +69,6 @@ impl ClientBuilder {
7369 }
7470
7571 /// Builds the Client.
76- #[ cfg( not( feature = "reqwest-client" ) ) ]
77- pub fn build ( self ) -> crate :: api:: Result < Client > {
78- Ok ( Client ( self ) )
79- }
80-
81- /// Builds the Client.
82- #[ cfg( feature = "reqwest-client" ) ]
8372 pub fn build ( self ) -> crate :: api:: Result < Client > {
8473 let mut client_builder = reqwest:: Client :: builder ( ) ;
8574
@@ -101,146 +90,9 @@ impl ClientBuilder {
10190}
10291
10392/// The HTTP client based on [`reqwest`].
104- #[ cfg( feature = "reqwest-client" ) ]
10593#[ derive( Debug , Clone ) ]
10694pub struct Client ( reqwest:: Client ) ;
10795
108- /// The HTTP client.
109- #[ cfg( not( feature = "reqwest-client" ) ) ]
110- #[ derive( Debug , Clone ) ]
111- pub struct Client ( ClientBuilder ) ;
112-
113- #[ cfg( not( feature = "reqwest-client" ) ) ]
114- impl Client {
115- /// Executes an HTTP request.
116- ///
117- /// # Examples
118- ///
119- /// ```rust,no_run
120- /// use tauri::api::http::{ClientBuilder, HttpRequestBuilder, ResponseType};
121- /// async fn run_request() {
122- /// let client = ClientBuilder::new().build().unwrap();
123- /// let response = client.send(
124- /// HttpRequestBuilder::new("GET", "https://www.rust-lang.org")
125- /// .unwrap()
126- /// .response_type(ResponseType::Binary)
127- /// ).await;
128- /// if let Ok(response) = response {
129- /// let bytes = response.bytes();
130- /// }
131- /// }
132- /// ```
133- pub async fn send ( & self , request : HttpRequestBuilder ) -> crate :: api:: Result < Response > {
134- let method = Method :: from_bytes ( request. method . to_uppercase ( ) . as_bytes ( ) ) ?;
135-
136- let mut request_builder = attohttpc:: RequestBuilder :: try_new ( method, & request. url ) ?;
137-
138- if let Some ( query) = request. query {
139- request_builder = request_builder. params ( & query) ;
140- }
141-
142- if let Some ( headers) = & request. headers {
143- for ( name, value) in headers. 0 . iter ( ) {
144- request_builder = request_builder. header ( name, value) ;
145- }
146- }
147-
148- if let Some ( max_redirections) = self . 0 . max_redirections {
149- if max_redirections == 0 {
150- request_builder = request_builder. follow_redirects ( false ) ;
151- } else {
152- request_builder = request_builder. max_redirections ( max_redirections as u32 ) ;
153- }
154- }
155-
156- if let Some ( timeout) = request. timeout {
157- request_builder = request_builder. timeout ( timeout) ;
158- }
159-
160- let response = if let Some ( body) = request. body {
161- match body {
162- Body :: Bytes ( data) => request_builder. body ( attohttpc:: body:: Bytes ( data) ) . send ( ) ?,
163- Body :: Text ( text) => request_builder. body ( attohttpc:: body:: Bytes ( text) ) . send ( ) ?,
164- Body :: Json ( json) => request_builder. json ( & json) ?. send ( ) ?,
165- Body :: Form ( form_body) => {
166- #[ allow( unused_variables) ]
167- fn send_form (
168- request_builder : attohttpc:: RequestBuilder ,
169- headers : & Option < HeaderMap > ,
170- form_body : FormBody ,
171- ) -> crate :: api:: Result < attohttpc:: Response > {
172- #[ cfg( feature = "http-multipart" ) ]
173- if matches ! (
174- headers
175- . as_ref( )
176- . and_then( |h| h. 0 . get( "content-type" ) )
177- . map( |v| v. as_bytes( ) ) ,
178- Some ( b"multipart/form-data" )
179- ) {
180- let mut multipart = attohttpc:: MultipartBuilder :: new ( ) ;
181- let mut byte_cache: HashMap < String , Vec < u8 > > = Default :: default ( ) ;
182-
183- for ( name, part) in & form_body. 0 {
184- if let FormPart :: File { file, .. } = part {
185- byte_cache. insert ( name. to_string ( ) , file. clone ( ) . try_into ( ) ?) ;
186- }
187- }
188- for ( name, part) in & form_body. 0 {
189- multipart = match part {
190- FormPart :: File {
191- file,
192- mime,
193- file_name,
194- } => {
195- // safe to unwrap: always set by previous loop
196- let mut file =
197- attohttpc:: MultipartFile :: new ( name, byte_cache. get ( name) . unwrap ( ) ) ;
198- if let Some ( mime) = mime {
199- file = file. with_type ( mime) ?;
200- }
201- if let Some ( file_name) = file_name {
202- file = file. with_filename ( file_name) ;
203- }
204- multipart. with_file ( file)
205- }
206- FormPart :: Text ( value) => multipart. with_text ( name, value) ,
207- } ;
208- }
209- return request_builder
210- . body ( multipart. build ( ) ?)
211- . send ( )
212- . map_err ( Into :: into) ;
213- }
214-
215- let mut form = Vec :: new ( ) ;
216- for ( name, part) in form_body. 0 {
217- match part {
218- FormPart :: File { file, .. } => {
219- let bytes: Vec < u8 > = file. try_into ( ) ?;
220- form. push ( ( name, serde_json:: to_string ( & bytes) ?) )
221- }
222- FormPart :: Text ( value) => form. push ( ( name, value) ) ,
223- }
224- }
225- request_builder. form ( & form) ?. send ( ) . map_err ( Into :: into)
226- }
227-
228- send_form ( request_builder, & request. headers , form_body) ?
229- }
230- }
231- } else {
232- request_builder. send ( ) ?
233- } ;
234-
235- Ok ( Response (
236- request. response_type . unwrap_or ( ResponseType :: Json ) ,
237- response,
238- request. url ,
239- ) )
240- }
241- }
242-
243- #[ cfg( feature = "reqwest-client" ) ]
24496impl Client {
24597 /// Executes an HTTP request
24698 ///
@@ -557,13 +409,8 @@ impl HttpRequestBuilder {
557409}
558410
559411/// The HTTP response.
560- #[ cfg( feature = "reqwest-client" ) ]
561412#[ derive( Debug ) ]
562413pub struct Response ( ResponseType , reqwest:: Response ) ;
563- /// The HTTP response.
564- #[ cfg( not( feature = "reqwest-client" ) ) ]
565- #[ derive( Debug ) ]
566- pub struct Response ( ResponseType , attohttpc:: Response , Url ) ;
567414
568415impl Response {
569416 /// Get the [`StatusCode`] of this Response.
@@ -579,20 +426,10 @@ impl Response {
579426 /// Reads the response as raw bytes.
580427 pub async fn bytes ( self ) -> crate :: api:: Result < RawResponse > {
581428 let status = self . status ( ) . as_u16 ( ) ;
582- #[ cfg( feature = "reqwest-client" ) ]
583429 let data = self . 1 . bytes ( ) . await ?. to_vec ( ) ;
584- #[ cfg( not( feature = "reqwest-client" ) ) ]
585- let data = self . 1 . bytes ( ) ?;
586430 Ok ( RawResponse { status, data } )
587431 }
588432
589- #[ cfg( not( feature = "reqwest-client" ) ) ]
590- #[ allow( dead_code) ]
591- pub ( crate ) fn reader ( self ) -> attohttpc:: ResponseReader {
592- let ( _, _, reader) = self . 1 . split ( ) ;
593- reader
594- }
595-
596433 // Convert the response into a Stream of [`bytes::Bytes`] from the body.
597434 //
598435 // # Examples
@@ -612,7 +449,6 @@ impl Response {
612449 // # Ok(())
613450 // # }
614451 // ```
615- #[ cfg( feature = "reqwest-client" ) ]
616452 #[ allow( dead_code) ]
617453 pub ( crate ) fn bytes_stream (
618454 self ,
@@ -625,10 +461,7 @@ impl Response {
625461 ///
626462 /// Note that the body is serialized to a [`Value`].
627463 pub async fn read ( self ) -> crate :: api:: Result < ResponseData > {
628- #[ cfg( feature = "reqwest-client" ) ]
629464 let url = self . 1 . url ( ) . clone ( ) ;
630- #[ cfg( not( feature = "reqwest-client" ) ) ]
631- let url = self . 2 ;
632465
633466 let mut headers = HashMap :: new ( ) ;
634467 let mut raw_headers = HashMap :: new ( ) ;
@@ -650,20 +483,12 @@ impl Response {
650483 }
651484 let status = self . 1 . status ( ) . as_u16 ( ) ;
652485
653- #[ cfg( feature = "reqwest-client" ) ]
654486 let data = match self . 0 {
655487 ResponseType :: Json => self . 1 . json ( ) . await ?,
656488 ResponseType :: Text => Value :: String ( self . 1 . text ( ) . await ?) ,
657489 ResponseType :: Binary => serde_json:: to_value ( & self . 1 . bytes ( ) . await ?) ?,
658490 } ;
659491
660- #[ cfg( not( feature = "reqwest-client" ) ) ]
661- let data = match self . 0 {
662- ResponseType :: Json => self . 1 . json ( ) ?,
663- ResponseType :: Text => Value :: String ( self . 1 . text ( ) ?) ,
664- ResponseType :: Binary => serde_json:: to_value ( self . 1 . bytes ( ) ?) ?,
665- } ;
666-
667492 Ok ( ResponseData {
668493 url,
669494 status,
0 commit comments