Simplify execute HTTP client method.

This commit is contained in:
jcamiel 2023-02-13 15:55:51 +01:00
parent a6fddfe13f
commit 21920dd78e
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC

View File

@ -83,19 +83,25 @@ impl Client {
let mut redirect_count = 0; let mut redirect_count = 0;
loop { loop {
let (request, response) = self.execute(&request_spec, options, logger)?; let (request, response) = self.execute(&request_spec, options, logger)?;
calls.push((request.clone(), response.clone())); let base_url = request.base_url()?;
if !options.follow_location { let redirect_url = self.get_follow_location(&response, &base_url);
calls.push((request, response));
if !options.follow_location || redirect_url.is_none() {
break; break;
} }
let redirect_url = redirect_url.unwrap();
let base_url = request.base_url()?;
if let Some(url) = self.get_follow_location(&response, &base_url) {
logger.debug(""); logger.debug("");
logger.debug(format!("=> Redirect to {url}").as_str()); logger.debug(format!("=> Redirect to {redirect_url}").as_str());
logger.debug(""); logger.debug("");
redirect_count += 1;
if let Some(max_redirect) = options.max_redirect {
if redirect_count > max_redirect {
return Err(HttpError::TooManyRedirect);
}
}
request_spec = RequestSpec { request_spec = RequestSpec {
method: Method::Get, method: Method::Get,
url, url: redirect_url,
headers: vec![], headers: vec![],
querystring: vec![], querystring: vec![],
form: vec![], form: vec![],
@ -104,16 +110,6 @@ impl Client {
body: Body::Binary(vec![]), body: Body::Binary(vec![]),
content_type: None, content_type: None,
}; };
redirect_count += 1;
if let Some(max_redirect) = options.max_redirect {
if redirect_count > max_redirect {
return Err(HttpError::TooManyRedirect);
}
}
} else {
break;
}
} }
Ok(calls) Ok(calls)
} }