bt_http_utils 0.8.1

A simple HTTP wrapper to simplify POST and GET calls. Default headers with set and get headers. Support cookies. Request generic function for GET, POST, PUT, PATCH, and DELETE.
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
/// Defines a HttpClient struct and its associated methods, which provides a simple and efficient way to make HTTP requests.
/// It includes methods to set custom headers and retrieve default headers, as well as handling cookies if needed.
/// It also defines an HttpResponse struct to represent the response from a HTTP request.
mod ext_certs;
pub mod stream_response;

pub const DANGER_ACCEPT_INVALID_HOSTNAMES: &str = "danger_accept_invalid_hostnames";
pub const DANGER_ACCEPT_INVALID_CERTS: &str     = "danger_accept_invalid_certs";

use std::{
    collections::HashMap, str::FromStr, sync::Arc
};

use bt_any_error::any_err::AnyErr;
use bt_logger::{get_error, log_error, log_verbose, log_warning};
use ext_certs::get_local_certificates;
use reqwest::{
    Client, Method, Response, StatusCode, Url, cookie::Jar, header::{self, HeaderMap, HeaderName, HeaderValue}
};
use stream_response::HttpStreamResponse;

///HttpClient:
///client: A Client instance from the reqwest crate for making HTTP requests.
///headers: A HeaderMap to store custom headers.
pub struct HttpClient {
    client: Client,
    headers: HeaderMap,
}

///HttpResponse: Represents the response from an HTTP request.
/// status_code: The status code of the HTTP response.
/// header: A HashMap containing the headers from the response.
/// body: The body content of the HTTP response as a string.
#[derive(Clone, Debug)]
pub struct HttpResponse {
    pub status_code: u16,
    pub header: HashMap<String, String>,
    pub body: String,
    pub remote_address: String
}


///ContentType: An enum to specify the content type of the request or response. Currently supports JSON and TEXT.
#[derive(Debug)]
pub enum ContentType {
    JSON,
    TEXT,
}

impl HttpClient {
    ///Constructor new: 
    /// The new method is used to create a new instance of the HttpClient struct
    /// It takes two boolean parameters: use_hickory_dns and use_cookies.
    /// If use_cookies is true, it enables cookie support in the client. It creates a cookie store using Arc, sets the client to use cookies with a default custom user agent.
    /// If use_cookies is false, it builds a client without cookie support but still sets a default user agent.
    /// If use_hickory_dns is true, it enables Hickory DNS resolution in the client.
    /// danger_accept_invalid: If true removes any validation to digital certificates. Useful with some self-signed certificate sites or when hostname doesn't match the certificate.
    ///                         Possible values: const DANGER_ACCEPT_INVALID_HOSTNAMES: &str = "danger_accept_invalid_hostnames" OR
    ///                                          const DANGER_ACCEPT_INVALID_CERTS: &str = "danger_accept_invalid_certs" OR
    pub fn new(use_hickory_dns: bool, use_cookies: bool, danger_accept_invalid: Option<Vec<(String,bool)>>) -> Self {
        let tls_conn = get_local_certificates(danger_accept_invalid);
        let mut cb = Client::builder();

        if use_cookies {
            let cookie_store = Arc::new(Jar::default());
            if let Some (reqwest_tc) = tls_conn{
                cb = cb
                    .use_native_tls()
                    .use_preconfigured_tls(reqwest_tc);
            }
                cb = cb.cookie_provider(cookie_store.clone())
                /*.hickory_dns(use_hickory_dns)
                .build()
                .unwrap()*/
        } else {
            if let Some (reqwest_tc) = tls_conn{
                cb = cb
                    .use_native_tls()
                    .use_preconfigured_tls(reqwest_tc);
            }
            cb = cb.cookie_store(false)
                /*.hickory_dns(use_hickory_dns)
                .build()
                .unwrap()*/
        };

        let c = cb
        .connection_verbose(true)
        //.danger_accept_invalid_certs(true)
        //.danger_accept_invalid_hostnames(true)
        .hickory_dns(use_hickory_dns)
        .build()
        .unwrap();

        let mut h = HeaderMap::new();
        h.insert(
            header::USER_AGENT,
            HeaderValue::from_static("Mozilla/5.0 (compatible; BachueTech/1.0)"),
        );

        Self {
            client: c,
            headers: h,
        }
    }

    ///Method set_header: Allows adding custom headers to the HTTP client dynamically.
    pub fn set_header(&mut self, header_name: &str, header_value: &str) {
        self.headers.insert(
            HeaderName::from_str(header_name).unwrap(),
            HeaderValue::from_str(header_value).unwrap(),
        );
    }

    ///Method get_default_headers: Converts the internal HeaderMap to a HashMap for easy access and manipulation.
    pub fn get_default_headers(&self) -> HashMap<String, String> {
        convert_headers(&self.headers)
    }

    ///Helper Method: Merge current/default headers with extra headers
    //fn get_extra_headers(&self, extra_headers: Option<HashMap<&str, &str>>) -> HeaderMap {
    fn get_extra_headers(&self, extra_headers: Option<HashMap<String, String>>) -> HeaderMap {
        let mut local_headers = self.headers.clone();
        if let Some(new_headers) = extra_headers {
            // Add headers from HashMap into the existing HeaderMap
            for (key, value) in new_headers {
                local_headers.insert(
                    HeaderName::from_str(&key).unwrap(),
                    HeaderValue::from_str(&value).unwrap(),
                );
            }
        }

        local_headers
    }

///Method: get
///The get method is used to make a GET request to a specific URL
///It takes two parameters: url and extra_headers. If extra_headers is Some, it adds the headers to the existing headers in the client. 
/// The method returns an HttpResponse instance containing the response from the GET request. 
//    pub async fn get( &self, url: &str, extra_headers: Option<HashMap<&str, &str>>, ) -> Result<HttpResponse, Error> {
    pub async fn get( &self, url: &str, extra_headers: Option<HashMap<String, String>>, ) -> Result<HttpResponse, AnyErr> {
        let local_headers = self.get_extra_headers(extra_headers);
        let resp = self.client.get(url).headers(local_headers).send().await?;
        Ok(Self::extract_response(resp, url, "GET").await)
        /*match self.client.get(url).headers(local_headers).send().await {
            Ok(resp) => return Ok(Self::extract_response(resp, url, "GET").await),
            Err(e) => {
                Err(get_error!( "get", "Failed to get response from GET: {}. Error: {}", url, e).into())
            }
        }*/
    }

///Method: post
///The post method is used to make a POST request to a specific URL
///It takes four parameters: url, extra_headers, body_request, and content_type. 
/// The method returns an HttpResponse instance containing the response from the POST request. 
//    pub async fn post( &self, url: &str, extra_headers: Option<HashMap<&str, &str>>, body_request: &str, content_type: ContentType, ) -> Result<HttpResponse, Error> {
    pub async fn post( &self, url: &str, extra_headers: Option<HashMap<String, String>>, body_request: &str, content_type: ContentType, ) 
                        -> Result<HttpResponse,  AnyErr> {
        //log_verbose!("post", "Getting {} with payload: {}", url, body_request);
        let mut local_headers = self.get_extra_headers(extra_headers); //self.headers.clone();
        match content_type {
            ContentType::JSON => {
                local_headers.insert(
                    header::CONTENT_TYPE,
                    HeaderValue::from_str("application/json")?,
                );
            }
            ContentType::TEXT => {
                local_headers.insert(
                    header::CONTENT_TYPE,
                    HeaderValue::from_str("application/text")?,
                );
            }
        }

        let resp = self
            .client
            .post(url)
            .headers(local_headers)
            .body(body_request.to_string())
            .send()
            .await?;

        return Ok(Self::extract_response(resp, url, "POST").await)

        /*match self
            .client
            .post(url)
            .headers(local_headers)
            .body(body_request.to_string())
            .send()
            .await
        {
            Ok(resp) => return Ok(Self::extract_response(resp, url, "POST").await),
            Err(e) => {
                Err(get_error!( "post", "Failed to get response from POST ({:?}): {}. Error: {}", content_type, url, e ).into() )
            }
        }*/
    }

    pub async fn post_stream( &self, url: &str, extra_headers: Option<HashMap<String, String>>, body_request: &str, content_type: ContentType, ) -> Result<HttpStreamResponse,  AnyErr> {
        //log_verbose!("post", "Getting {} with payload: {}", url, body_request);
        let mut local_headers = self.get_extra_headers(extra_headers); //self.headers.clone();
        match content_type {
            ContentType::JSON => {
                local_headers.insert(
                    header::CONTENT_TYPE,
                    HeaderValue::from_str("application/json")?,
                );
            }
            ContentType::TEXT => {
                local_headers.insert(
                    header::CONTENT_TYPE,
                    HeaderValue::from_str("application/text")?,
                );
            }
        }

        let resp = self
            .client
            .post(url)
            .headers(local_headers)
            .body(body_request.to_string())
            .send()
            .await?;
        Ok(HttpStreamResponse::new(resp))

        /*match self
            .client
            .post(url)
            .headers(local_headers)
            .body(body_request.to_string())
            .send()
            .await
        {
            Ok(resp) => Ok(HttpStreamResponse::new(resp)),
            Err(e) => {
                Err(get_error!( "post_stream", "Failed to get stream response from POST ({:?}): {}. Error: {}", content_type, url, e ).into() )
            }
        }*/
    }

///Method: request
/// The request method is used to make a request to a specific URL using a specific HTTP method: currently tested, get, post, put, delete, patch, delete
/// It takes six parameters: request_method, url_with_ep_path (URL with endpoint: path, path parameters), extra_headers, body_params, query_params, and content_type. 
/// The method returns an HttpResponse instance containing the response from the request.
//    pub async fn request( &self, request_method: &str, url_with_ep_path: &str, extra_headers: Option<HashMap<&str, &str>>, body_params: Option<HashMap<String, String>>, 
    pub async fn request( &self, request_method: &str, url_with_ep_path: &str, extra_headers: Option<HashMap<String, String>>, body_params: Option<HashMap<String, String>>, 
                        query_params: Option<HashMap<String, String>>, content_type: ContentType, ) -> Result<HttpResponse, AnyErr> {
        let method = match request_method.to_uppercase().as_str() {
            "GET" => Method::GET,
            "POST" => Method::POST,
            "PUT" => Method::PUT,
            "DELETE" => Method::DELETE,
            "PATCH" => Method::PATCH,
            _ => return Err(get_error!("request", "Unsupported HTTP method: {}", &request_method).into()),
        };

        let mut url = url_with_ep_path.to_string();
        let mut qry_params: HashMap<String, String>;

        // Handle path parameters
        if let Some(path_params) = query_params {
            qry_params = path_params.clone();
            for path_param in path_params {
                if url.contains(&format!("{{{}}}", &path_param.0)) {
                    url = url.replace(&format!("{{{}}}", &path_param.0), &path_param.1);
                    qry_params.remove(&path_param.0); //Remove used path_param to use remaining params as query parameters
                } else {
                    log_verbose!("","Path parameter '{:?}' not provided. Parameter will be used as Query parameter", &path_param.0);
                }
            }
        }else{
            qry_params = HashMap::new();
        }


        let mut local_headers = self.get_extra_headers(extra_headers); //self.headers.clone();

        match content_type {
            ContentType::JSON => {
                local_headers.insert(
                    header::CONTENT_TYPE,
                    HeaderValue::from_str("application/json")?,
                );
            }
            ContentType::TEXT => {
                local_headers.insert(
                    header::CONTENT_TYPE,
                    HeaderValue::from_str("application/text")?,
                );
            }
        }

        //Removed 03/28/25: Cause issues!
        //if !url.ends_with('/') && qry_params.len() > 0 {
        //    url = format!("{}{}",url,"/");
        //}

        let request = 
        if method == Method::GET{
            let url_with_params = if !qry_params.is_empty() {
                                Url::parse_with_params(&url, &qry_params)? 
                            }else{
                                Url::parse(&url)? 
                            };
            self.client.get(url_with_params).headers(local_headers)                            
            //request = request.query(&qry_params); // Use remaining params as query parameters if any
        }else{
            let mut req = self.client.request(method.clone(), &url).headers(local_headers);
            if let Some(b_params) = body_params{
                    match content_type {
                        ContentType::JSON => req = req.json(&b_params),
                        _ => {let body_data = b_params
                                .iter()
                                .map(|(k, v)| format!("{}={}", k, v))
                                .collect::<Vec<String>>()
                                .join("&");
                                req = req.body(body_data)
                            }
                    }       
            }
            req
        };

        let resp = request.send().await?;
        Ok(Self::extract_response(resp, &url, request_method.to_uppercase().as_str()).await)

        /*match request
            .send()
            .await
        {
            Ok(resp) => Ok(Self::extract_response(resp, &url, request_method.to_uppercase().as_str()).await),
            Err(e) => {
                Err(get_error!( "request", "Failed to get response from {} ({:?}): {}. Error: {}", &method, content_type, url, e)
                                    .into())
            }
        }*/
    }

 ///Helper Method: extract_response
 /// The extract_response method is used to extract the response from a Response instance
 /// It takes three parameters: resp, url, and method. The method returns an HttpResponse instance containing the response from the request.
    async fn extract_response(mut resp: Response, url: &str, method: &str) -> HttpResponse {
        let ra = match resp.remote_addr() {
            Some(ip) => ip.ip().to_string(),
            None => {
                log_warning!("", "Remote Address not found in Response. Using default 0.0.0.0");
                "0.0.0.0".to_owned()
            },
        };

        if resp.status().is_client_error() || resp.status().is_server_error() || resp.status().as_u16() >= 600 {
            log_error!( "", "ERROR: Failed to get response from {}: {} Status Code: {}", method, url, resp.status() );
            HttpResponse {
                status_code: resp.status().as_u16(),
                header: convert_headers(resp.headers()),
                body: format!( "ERROR: Failed to get response from {}:{} -Error: {}", method, url, resp.status().canonical_reason().unwrap_or("UNKNOWN ERROR!") ),
                remote_address: ra
            }
        } else {
            let mut full_body = String::new();
            let mut error_count = 0;
            let rstatus = resp.status().as_u16();
            let rheader = convert_headers(resp.headers());

            if resp.status().is_success() {
                let mut read_resp: bool = true;
                // Process the response body as it's being streamed
                while read_resp {
                    match resp.chunk().await { 
                        Ok(r) => {
                            match r{
                                Some(chunk) => full_body.push_str(&String::from_utf8_lossy(&chunk)),
                                None => read_resp = false,
                            }
                        },
                        Err(e) => {
                            if error_count > 3{
                                log_error!("","Too many errors (>3 times) reading answer body. Stop Executing and return what was collected. Error {}",e);
                                return HttpResponse {
                                    status_code: resp.status().as_u16(),
                                    header: convert_headers(resp.headers()),
                                    body: match resp.text().await{
                                        //expect(full_body.as_str() ),
                                        Ok(b) => b,
                                        Err(e) => {
                                            log_error!("","ERROR: Failed to get payload from {}:{}. Error: {}",method,url,e);
                                            full_body
                                        },
                                                                            },
                                        //get_error!("","ERROR: Failed to get payload from {}:{}",method,url)
                                        //    .as_str(),
                                        //),
                                    remote_address: ra,
                                };
                            }
                            error_count += 1;
                            log_error!("","Error reading answer body (error count={}). Error {}",error_count,e);                
                        },
                    }
                }
            }else{
                full_body = match resp.text().await{
                    Ok(b) => b,
                    Err(e) => {
                        log_error!("","ERROR: Failed to get payload when status = {} from {}:{}. Error: {}",rstatus, method,url,e);                        
                        format!("ERROR: Failed to get payload when status = {} from {}",rstatus, method)
                    },
                };
            }
            HttpResponse {
                status_code: rstatus, // resp.status().as_u16(),
                header: rheader, //Self::convert_headers(resp.headers()),
                body: full_body,
                remote_address: ra,
            }
        }
    }


}

    ///Helper Method convert_headers: A private method to convert HeaderMap to HashMap.
    fn convert_headers(headers: &HeaderMap) -> HashMap<String, String> {
        headers
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or_default().to_string()))
            .collect()
    }
    
impl HttpResponse {
///The is_error method is used to check if the response is an error:    
    pub fn is_error(&self) -> bool {
        let sc = StatusCode::from_u16(self.status_code).unwrap_or(StatusCode::FORBIDDEN);
        sc.is_client_error() || sc.is_server_error()
    }
}