Skip to content

Commit

Permalink
refactor: remove unnecessary cloning of maybe unused input (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Dec 27, 2023
1 parent dd3922d commit cbfab5d
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions hitt-request/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,37 @@ pub async fn send_request(
input: &HittRequest,
) -> Result<HittResponse, reqwest::Error> {
let url = input.uri.to_string();
let method = input.method.to_string();

let req = http_client
.request(input.method.clone(), &url)
.headers(input.headers.clone())
.body(input.body.clone().unwrap_or_default())
.version(input.http_version.unwrap_or(reqwest::Version::HTTP_11))
.build()?;
let mut partial_req = http_client.request(input.method.clone(), &url);

if let Some(http_version) = input.http_version {
partial_req = partial_req.version(http_version);
}

if !input.headers.is_empty() {
partial_req = partial_req.headers(input.headers.clone());
}

if input.body.is_some() {
if let Some(body) = input.body.clone() {
partial_req = partial_req.body(body);
}
}

let req = partial_req.build()?;

// TODO: implement more precise benchmark?
let start = std::time::Instant::now();
let res = http_client.execute(req).await?;
let duration = start.elapsed();

let status_code = res.status();
let headers = res.headers().to_owned();

let http_version = res.version();

let body = res.text().await.unwrap_or_default();

Ok(HittResponse {
url,
method,
status_code,
headers,
body,
http_version,
method: input.method.to_string(),
status_code: res.status(),
headers: res.headers().to_owned(),
http_version: res.version(),
duration,
body: res.text().await.unwrap_or_default(),
})
}

0 comments on commit cbfab5d

Please sign in to comment.