requests_rs 0.1.7

A simple library for sending GET/POST requests and parsing jsons, included with an async file downloader. Intended for mostly small projects which need to make quick GET/POST requests or download files.
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


extern crate serde_json;
extern crate jsonxf;


pub mod requests {

    pub mod api_referencer {

        use colored::*;
        use std::path::Path;
        use std::fs::{self, File};

        use serde_json::Value as JsonValue;


        /// # Test function which sends a synchronous get request to any api and returns a response object which can be then parsed into json
        /// 
        /// Recommended to use the ```requests_rs::requests::api_referencer::get_and_save_json``` function instead of this.
        /// 
        /// ```
        /// use requests::requests::api_referencer::sync_get;
        /// 
        /// sync_get("https://api-url.com");
        /// 
        /// ```
        /// 
        pub fn sync_get(url: &str) -> reqwest::blocking::Response {
            reqwest::blocking::get(url).expect("Error referencing data")

        }


        /// ### Sends a get request to an api ,parses the json response and returns the json object
        /// 
        /// ```
        /// use requests_rs::requests::api_referencer::get_and_save_json;
        /// 
        /// Example 1
        /// 
        /// get_and_save_json("https://api-url.com", true, false).expect("Some error message!")
        /// 
        /// ```
        ///
        /// Having save=true will parse the json value and save it to a json file. 
        /// 
        /// Having silent_mode = false will pretty-print out the json response(Useful for debugging purposes?).
        /// 
        /// This function is asynchrounous so many get requests can be sent at a time(although idk why someone would need to do that.)
        /// 
        #[tokio::main]
        pub async fn get_and_save_json(url: &str, save: bool, silent_mode: bool) -> Result<JsonValue, reqwest::Error>{

            #[cfg(windows)] {
                control::set_virtual_terminal(true).unwrap();
            }

            
            let client = reqwest::Client::new()
                .get(url)
                .send()
                .await?;
                

            let status_code = client.status();

            let return_json: serde_json::value::Value = client.json().await?;

            let raw_json = return_json.to_string();

            let ugly_json = raw_json.as_str();

            let mut pretty_json = jsonxf::pretty_print(ugly_json).unwrap();

            if !silent_mode {

                // execute as intended

                if !save && status_code.is_success() {

                    print!("{}", "OK".green());
    
                    println!("[{}]", status_code.as_str().green() );
    
                    println!();
    
                    println!("{}", pretty_json);
                }
                else if save && status_code.is_success() { // if save just simplifies to if save == true
                    
                    // file_downloader::async_download_file(url, "./").expect("Failed to download file!");
    
                    print!("{}", "OK".green());
    
                    println!("[{}]", status_code.as_str().green() );
    
                    println!();
    
                    let file_name = Path::new(url).file_stem().unwrap().to_str().unwrap(); // file name
    
                    let file_extension = Path::new(url).extension().unwrap().to_str().unwrap(); // file extension
    
                    let download_path = "./";
    
                    File::create(format!("{}/{}.{}", download_path, file_name, file_extension)).expect("Error downloading file!");
                    fs::write(format!("{}/{}.{}", download_path, file_name, file_extension), &mut pretty_json).expect("Failed to write to file!");
    
                    print!("{}", "Saved => ".green());
    
                    print!("{}.{}", file_name, file_extension);
    
                    print!("{}", " at ".green());
    
                    println!("{}", download_path);
    
    
                    println!("{}", pretty_json.green());
    
                }
    
                else if status_code.is_client_error() || status_code.is_server_error() {
                    println!("{}", "Error fetching data!".red());
    
                    print!("{}", "Error code ".red());
    
                    print!("{}", status_code.as_str().red())
                }
    
                else {
                    print!("{}", "Error code ".red());
    
                    print!("{}", status_code.as_str().red());
                }
            }

            else if silent_mode {

                // print only status code and saves data.

                if save && status_code.is_success() {

                    print!("{}", "OK".green());
    
                    println!("[{}]", status_code.as_str().green() );
    
                    println!();

                    let file_name = Path::new(url).file_stem().unwrap().to_str().unwrap(); // file name
        
                    let file_extension = Path::new(url).extension().unwrap().to_str().unwrap(); // file extension
        
                    let download_path = "./";
        
                    File::create(format!("{}/{}.{}", download_path, file_name, file_extension)).expect("Error downloading file!");
                    fs::write(format!("{}/{}.{}", download_path, file_name, file_extension), &mut pretty_json).expect("Failed to write to file!");
                    
            
                }

                else if !save && status_code.is_success() {

                    // prints only the status code.

                    print!("{}", "OK".green());
    
                    println!("[{}]", status_code.as_str().green() );
    
                    println!();
                }

                else if status_code.is_client_error() || status_code.is_server_error() {
                    
                    println!("{}", "Error fetching data!".red());

                    print!("{}", "Error code ".red());
    
                    print!("{}", status_code.as_str().red())
                }
                
                else {
                    print!("{}", "Error code ".red());
    
                    print!("{}", status_code.as_str().red());
                }

            }

            Ok(return_json)
            
        }

        ///Returns XML data from an xml file as a string.
        /// Usage :
        /// ```
        /// use requests_rs::requests::api_referencer::get_and_save_xml;
        /// 
        /// Example 1
        /// 
        /// let xml_data = get_and_save_xml("https://xml-url.com").expect("Some error message!");
        /// 
        /// println!("{}", xml_data);
        /// 
        /// ```
        #[tokio::main]
        pub async fn get_and_save_xml(url: &str) -> Result<String, reqwest::Error> {

            #[cfg(windows)] {
                control::set_virtual_terminal(true).unwrap();
            }

            let client = reqwest::Client::new()
                .get(url)
                .send()
                .await?;

            let mut xml_data = "".to_string();

            if client.status().is_success() {
                let return_bytes = client.bytes().await?; // get the data in bytes

                xml_data = String::from_utf8(return_bytes.to_vec()).expect("Error parsing xml data from bytes!");

            }

            else {
                
                let status_code = client.status();

                println!("{}", status_code.as_str().red());


                println!("Url not reachable!");
            }
            
            Ok(xml_data)

        }
        

        
        // had a problem with async POST idk why

        // but this works anyways.

        // might add async post function in the future.


        /// # Sends a POST request to any api and returns the response json object
        /// 
        /// ```
        /// use requests_rs::requests::api_referencer::print_and_post_json;
        /// 
        /// Example 1
        /// 
        /// print_and_post_json("https://api-url.com", "path/to/json_file", true)
        /// 
        /// ```
        /// 
        /// If silent_mode is set to true then the function will silently send a POST request and return the response json object
        /// 
        /// If set to false then the function will send a POST request and pretty print out the response json, alongside returning it as a value as well
        ///
        pub fn print_and_post_json(url: &str, file_path:&str, silent_mode: bool) -> Result<JsonValue, reqwest::Error>{
            
            #[cfg(windows)] {
                control::set_virtual_terminal(true).unwrap();
            }
            let content = fs::read_to_string(file_path).expect("Error reading from file!");

            let json: serde_json::value::Value = serde_json::from_str(&content).expect("Error reading from content");

            let json_map = json.as_object().unwrap();

            let client = reqwest::blocking::Client::new()
                    .post(url)
                    .json(&json_map)
                    .send();
            
            let response = client.unwrap();

            let status = response.status();

            let response_json: serde_json::value::Value = response.json().unwrap();

            let response_text = response_json.to_string();

            
            if !silent_mode {

                // execute as intended 

                print!("{}", "Information to be posted: ".green());

                println!();

                println!("{}", content.green());

                println!();
                
                if status.is_success() {

                    print!("{}", "Response from api: ".green());

                    let ugly_response_str = response_text.as_str();

                    let pretty_json_str = jsonxf::pretty_print(ugly_response_str).unwrap();

                    println!("{}", pretty_json_str.green());
                }
                
                else  {
                    
                    print!("{}", "Error code ".red());
    
                    print!("{}", status.to_string().red());


                }
                
            }

            else if silent_mode {
                if status.is_success() {
                    print!("{}", "OK".green());
    
                    println!("[{}]", status.as_str().green() );
                }

                else {
                    print!("{}", "Error code ".red());
    
                    print!("{}", status.to_string().red());
                }
            }

            
               
        Ok(response_json)

        }
        

    }

    pub mod file_downloader {

        use std::path::Path;
        use std::fs::{self, File};

        use colored::*;


        /// ### Downloads any file asynchronously
        /// 
        /// ```
        /// use requests_rs::requests::file_downloader::async_download_file;
        /// 
        /// 
        /// async_download_file("https://download-the-file.exe", "your_download_path").expect("Some error message")
        /// ```
        #[tokio::main]
        pub async fn async_download_file(url: &str, download_path: &str) -> Result<(), reqwest::Error>{


            #[cfg(windows)] {
                control::set_virtual_terminal(true).unwrap();
            }

            
            let file_name = Path::new(url).file_stem().unwrap().to_str().unwrap(); // file name

            if Path::new(url).extension().is_some() {
                let file_extension = Path::new(url).extension().unwrap().to_str().unwrap(); // file extension

            

                print!("{}", "Downloading ".green());

                println!("{}.{}", file_name,file_extension);


                let client = reqwest::Client::new()
                    .get(url)
                    .send() // sends a GET request
                    .await?
                    .bytes() // receives it in bytes, for any file
                    .await?;



                File::create(format!("{}/{}.{}", download_path, file_name, file_extension)).expect("Error downloading file!");
                fs::write(format!("{}/{}.{}", download_path, file_name, file_extension), client).expect("Failed to write to file!");

                print!("{}", "Saved => ".green());

                print!("{}.{}", file_name, file_extension);

                print!("{}", " at ".green());

                println!("{}", download_path);

            }

            else if Path::new(url).extension().is_none() {

    
                print!("{}", "Downloading ".green());

                println!("{}", file_name);


                let client = reqwest::Client::new()
                    .get(url)
                    .send() // sends a GET request
                    .await?
                    .bytes() // receives it in bytes, for any file
                    .await?;




                File::create(format!("{}/{}", download_path, file_name)).expect("Error downloading file!");
                fs::write(format!("{}/{}", download_path, file_name), client).expect("Failed to write to file!");

                print!("{}", "Saved => ".green());

                print!("{}", file_name);

                print!("{}", " at ".green());

                println!("{}", download_path);

            }

            


            Ok(())
        }


    }

}