#[derive(Clone)]
pub struct HttpClient {
pub client: reqwest::Client,
}
impl HttpClient {
pub fn build(timeout_sec: usize) -> Self {
let http_client = reqwest::Client::builder()
.use_rustls_tls()
.danger_accept_invalid_certs(true)
.timeout(std::time::Duration::from_secs(timeout_sec as u64))
.build()
.unwrap();
Self {
client: http_client,
}
}
pub async fn do_get(&self, url: &str) -> Result<String, reqwest::Error> {
match self.client.get(url).send().await {
Ok(res) => match res.error_for_status() {
Ok(o) => {
log::debug!(
"HttpRequest-Success: method=GET, url={}, StatusCode={}",
url,
o.status()
);
let text = o.text().await.unwrap();
Ok(text)
}
Err(e) => {
log::error!("HttpRequest-Failed: method=GET, url={}, error={:?}", url, e);
Err(e)
}
},
Err(e) => {
log::error!("HttpRequest-Error: method=GET, url={}, error={:?}", url, e);
Err(e)
}
}
}
pub async fn post_with_json_data<T>(&self, url: &str, body: &T) -> Result<(), reqwest::Error>
where
T: serde::Serialize,
{
match self
.client
.post(url)
.header("Content-Type", "application/json")
.json(body)
.send()
.await
{
Ok(res) => match res.error_for_status() {
Ok(o) => {
log::debug!(
"HttpRequest-Success: method=POST, url={}, StatusCode={}",
url,
o.status()
);
Ok(())
}
Err(e) => {
log::error!(
"HttpRequest-Failed: method=POST, url={}, error={:?}",
url,
e
);
Err(e)
}
},
Err(e) => {
log::error!("HttpRequest-Error: method=POST, url={}, error={:?}", url, e);
Err(e)
}
}
}
pub async fn put_with_json_data<T>(&self, url: &str, body: &T) -> Result<(), reqwest::Error>
where
T: serde::Serialize,
{
match self
.client
.put(url)
.header("Content-Type", "application/json")
.json(body)
.send()
.await
{
Ok(res) => match res.error_for_status() {
Ok(o) => {
log::debug!(
"HttpRequest-Success: method=PUT, url={}, StatusCode={}",
url,
o.status()
);
Ok(())
}
Err(e) => {
log::error!("HttpRequest-Failed: method=PUT, url={}, error={:?}", url, e);
Err(e)
}
},
Err(e) => {
log::error!("HttpRequest-Error: method=PUT, url={}, error={:?}", url, e);
Err(e)
}
}
}
pub async fn put_with_json_text(&self, url: &str, body: String) -> Result<(), reqwest::Error> {
match self
.client
.put(url)
.header("Content-Type", "application/json")
.body(body)
.send()
.await
{
Ok(res) => match res.error_for_status() {
Ok(o) => {
log::debug!(
"HttpRequest-Success: method=PUT, url={}, StatusCode={}",
url,
o.status()
);
Ok(())
}
Err(e) => {
log::error!("HttpRequest-Failed: method=PUT, url={}, error={:?}", url, e);
Err(e)
}
},
Err(e) => {
log::error!("HttpRequest-Error: method=PUT, url={}, error={:?}", url, e);
Err(e)
}
}
}
pub async fn post_with_json_data_string<T>(
&self,
url: &str,
body: &T,
) -> Result<String, reqwest::Error>
where
T: serde::Serialize,
{
match self
.client
.post(url)
.header("Content-Type", "application/json")
.json(body)
.send()
.await
{
Ok(res) => match res.error_for_status() {
Ok(o) => {
log::debug!(
"HttpRequest-Success: method=POST, url={}, StatusCode={}",
url,
o.status()
);
o.text().await
}
Err(e) => {
log::error!(
"HttpRequest-Failed: method=POST, url={}, error={:?}",
url,
e
);
Err(e)
}
},
Err(e) => {
log::error!("HttpRequest-Error: method=POST, url={}, error={:?}", url, e);
Err(e)
}
}
}
}