Skip to content

Commit

Permalink
feat: [#472] add timeout to API client requests
Browse files Browse the repository at this point in the history
both in testing and production clients.
  • Loading branch information
josecelano committed Feb 9, 2024
1 parent 9ff4de4 commit 26422fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/web/api/client/v1/connection_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{fmt, str::FromStr};
use std::fmt;
use std::str::FromStr;

use reqwest::Url;

Expand Down
17 changes: 16 additions & 1 deletion src/web/api/client/v1/http.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use reqwest::multipart;
use serde::Serialize;

Expand Down Expand Up @@ -65,12 +67,18 @@ impl From<QueryParam> for ReqwestQueryParam {
/// Generic HTTP Client
pub struct Http {
connection_info: ConnectionInfo,
/// The timeout is applied from when the request starts connecting until the
/// response body has finished.
timeout: Duration,
}

impl Http {
#[must_use]
pub fn new(connection_info: ConnectionInfo) -> Self {
Self { connection_info }
Self {
connection_info,
timeout: Duration::from_secs(5),
}
}

/// # Panics
Expand All @@ -80,6 +88,7 @@ impl Http {
pub async fn get(&self, path: &str, params: Query) -> TextResponse {
let response = match &self.connection_info.token {
Some(token) => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand All @@ -89,6 +98,7 @@ impl Http {
.await
.unwrap(),
None => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand All @@ -107,6 +117,7 @@ impl Http {
pub async fn get_binary(&self, path: &str, params: Query) -> BinaryResponse {
let response = match &self.connection_info.token {
Some(token) => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand All @@ -116,6 +127,7 @@ impl Http {
.await
.unwrap(),
None => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand All @@ -141,6 +153,7 @@ impl Http {
/// This method fails it can't build a `reqwest` client.
pub async fn inner_get(&self, path: &str) -> Result<reqwest::Response, reqwest::Error> {
reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand Down Expand Up @@ -178,6 +191,7 @@ impl Http {
pub async fn post_multipart(&self, path: &str, form: multipart::Form) -> TextResponse {
let response = match &self.connection_info.token {
Some(token) => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.post(self.base_url(path).clone())
Expand All @@ -187,6 +201,7 @@ impl Http {
.await
.expect("failed to send multipart request with token"),
None => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.post(self.base_url(path).clone())
Expand Down
17 changes: 16 additions & 1 deletion tests/common/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use reqwest::multipart;
use serde::Serialize;

Expand Down Expand Up @@ -158,16 +160,23 @@ impl Client {
/// Generic HTTP Client
struct Http {
connection_info: ConnectionInfo,
/// The timeout is applied from when the request starts connecting until the
/// response body has finished.
timeout: Duration,
}

impl Http {
pub fn new(connection_info: ConnectionInfo) -> Self {
Self { connection_info }
Self {
connection_info,
timeout: Duration::from_secs(5),
}
}

pub async fn get(&self, path: &str, params: Query) -> TextResponse {
let response = match &self.connection_info.token {
Some(token) => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand All @@ -177,6 +186,7 @@ impl Http {
.await
.unwrap(),
None => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand All @@ -191,6 +201,7 @@ impl Http {
pub async fn get_binary(&self, path: &str, params: Query) -> BinaryResponse {
let response = match &self.connection_info.token {
Some(token) => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand All @@ -200,6 +211,7 @@ impl Http {
.await
.unwrap(),
None => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand All @@ -217,6 +229,7 @@ impl Http {

pub async fn inner_get(&self, path: &str) -> Result<reqwest::Response, reqwest::Error> {
reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.get(self.base_url(path).clone())
Expand Down Expand Up @@ -246,6 +259,7 @@ impl Http {
pub async fn post_multipart(&self, path: &str, form: multipart::Form) -> TextResponse {
let response = match &self.connection_info.token {
Some(token) => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.post(self.base_url(path).clone())
Expand All @@ -255,6 +269,7 @@ impl Http {
.await
.expect("failed to send multipart request with token"),
None => reqwest::Client::builder()
.timeout(self.timeout)
.build()
.unwrap()
.post(self.base_url(path).clone())
Expand Down

0 comments on commit 26422fc

Please sign in to comment.