feat(router): reuse reqwest clients for requests made to connectors (#413)

This commit is contained in:
Sampras Lopes
2023-01-19 15:05:37 +05:30
committed by GitHub
parent d1d1e52b5d
commit 749c83a8e6
3 changed files with 187 additions and 183 deletions

View File

@ -1,7 +1,13 @@
mod client;
pub(crate) mod request;
use std::{collections::HashMap, fmt::Debug, future::Future, str, time::Instant};
use std::{
collections::HashMap,
fmt::Debug,
future::Future,
str,
time::{Duration, Instant},
};
use actix_web::{body, HttpRequest, HttpResponse, Responder};
use bytes::Bytes;
@ -210,17 +216,16 @@ async fn send_request(
let client = client::create_client(
&state.conf.proxy,
should_bypass_proxy,
crate::consts::REQUEST_TIME_OUT,
request.certificate,
request.certificate_key,
)?;
let headers = request.headers.construct_header_map()?;
match request.method {
Method::Get => client.get(url).add_headers(headers).send().await,
Method::Get => client.get(url),
Method::Post => {
let client = client.post(url).add_headers(headers);
let client = client.post(url);
match request.content_type {
Some(ContentType::Json) => client.json(&request.payload).send(),
Some(ContentType::Json) => client.json(&request.payload),
// Currently this is not used remove this if not required
// If using this then handle the serde_part
@ -236,26 +241,24 @@ async fn send_request(
})?;
logger::debug!(?url_encoded_payload);
client.body(url_encoded_payload).send()
client.body(url_encoded_payload)
}
// If payload needs processing the body cannot have default
None => client
.body(request.payload.expose_option().unwrap_or_default())
.send(),
None => client.body(request.payload.expose_option().unwrap_or_default()),
}
.await
}
Method::Put => {
client
.put(url)
.add_headers(headers)
.body(request.payload.expose_option().unwrap_or_default()) // If payload needs processing the body cannot have default
.send()
.await
}
Method::Delete => client.delete(url).add_headers(headers).send().await,
Method::Delete => client.delete(url),
}
.add_headers(headers)
.timeout(Duration::from_secs(crate::consts::REQUEST_TIME_OUT))
.send()
.await
.map_err(|error| match error {
error if error.is_timeout() => errors::ApiClientError::RequestTimeoutReceived,
_ => errors::ApiClientError::RequestNotSent(error.to_string()),