Skip to content

Commit 26422fc

Browse files
committed
feat: [#472] add timeout to API client requests
both in testing and production clients.
1 parent 9ff4de4 commit 26422fc

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/web/api/client/v1/connection_info.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::{fmt, str::FromStr};
1+
use std::fmt;
2+
use std::str::FromStr;
23

34
use reqwest::Url;
45

src/web/api/client/v1/http.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use reqwest::multipart;
24
use serde::Serialize;
35

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

7075
impl Http {
7176
#[must_use]
7277
pub fn new(connection_info: ConnectionInfo) -> Self {
73-
Self { connection_info }
78+
Self {
79+
connection_info,
80+
timeout: Duration::from_secs(5),
81+
}
7482
}
7583

7684
/// # Panics
@@ -80,6 +88,7 @@ impl Http {
8088
pub async fn get(&self, path: &str, params: Query) -> TextResponse {
8189
let response = match &self.connection_info.token {
8290
Some(token) => reqwest::Client::builder()
91+
.timeout(self.timeout)
8392
.build()
8493
.unwrap()
8594
.get(self.base_url(path).clone())
@@ -89,6 +98,7 @@ impl Http {
8998
.await
9099
.unwrap(),
91100
None => reqwest::Client::builder()
101+
.timeout(self.timeout)
92102
.build()
93103
.unwrap()
94104
.get(self.base_url(path).clone())
@@ -107,6 +117,7 @@ impl Http {
107117
pub async fn get_binary(&self, path: &str, params: Query) -> BinaryResponse {
108118
let response = match &self.connection_info.token {
109119
Some(token) => reqwest::Client::builder()
120+
.timeout(self.timeout)
110121
.build()
111122
.unwrap()
112123
.get(self.base_url(path).clone())
@@ -116,6 +127,7 @@ impl Http {
116127
.await
117128
.unwrap(),
118129
None => reqwest::Client::builder()
130+
.timeout(self.timeout)
119131
.build()
120132
.unwrap()
121133
.get(self.base_url(path).clone())
@@ -141,6 +153,7 @@ impl Http {
141153
/// This method fails it can't build a `reqwest` client.
142154
pub async fn inner_get(&self, path: &str) -> Result<reqwest::Response, reqwest::Error> {
143155
reqwest::Client::builder()
156+
.timeout(self.timeout)
144157
.build()
145158
.unwrap()
146159
.get(self.base_url(path).clone())
@@ -178,6 +191,7 @@ impl Http {
178191
pub async fn post_multipart(&self, path: &str, form: multipart::Form) -> TextResponse {
179192
let response = match &self.connection_info.token {
180193
Some(token) => reqwest::Client::builder()
194+
.timeout(self.timeout)
181195
.build()
182196
.unwrap()
183197
.post(self.base_url(path).clone())
@@ -187,6 +201,7 @@ impl Http {
187201
.await
188202
.expect("failed to send multipart request with token"),
189203
None => reqwest::Client::builder()
204+
.timeout(self.timeout)
190205
.build()
191206
.unwrap()
192207
.post(self.base_url(path).clone())

tests/common/client.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use reqwest::multipart;
24
use serde::Serialize;
35

@@ -158,16 +160,23 @@ impl Client {
158160
/// Generic HTTP Client
159161
struct Http {
160162
connection_info: ConnectionInfo,
163+
/// The timeout is applied from when the request starts connecting until the
164+
/// response body has finished.
165+
timeout: Duration,
161166
}
162167

163168
impl Http {
164169
pub fn new(connection_info: ConnectionInfo) -> Self {
165-
Self { connection_info }
170+
Self {
171+
connection_info,
172+
timeout: Duration::from_secs(5),
173+
}
166174
}
167175

168176
pub async fn get(&self, path: &str, params: Query) -> TextResponse {
169177
let response = match &self.connection_info.token {
170178
Some(token) => reqwest::Client::builder()
179+
.timeout(self.timeout)
171180
.build()
172181
.unwrap()
173182
.get(self.base_url(path).clone())
@@ -177,6 +186,7 @@ impl Http {
177186
.await
178187
.unwrap(),
179188
None => reqwest::Client::builder()
189+
.timeout(self.timeout)
180190
.build()
181191
.unwrap()
182192
.get(self.base_url(path).clone())
@@ -191,6 +201,7 @@ impl Http {
191201
pub async fn get_binary(&self, path: &str, params: Query) -> BinaryResponse {
192202
let response = match &self.connection_info.token {
193203
Some(token) => reqwest::Client::builder()
204+
.timeout(self.timeout)
194205
.build()
195206
.unwrap()
196207
.get(self.base_url(path).clone())
@@ -200,6 +211,7 @@ impl Http {
200211
.await
201212
.unwrap(),
202213
None => reqwest::Client::builder()
214+
.timeout(self.timeout)
203215
.build()
204216
.unwrap()
205217
.get(self.base_url(path).clone())
@@ -217,6 +229,7 @@ impl Http {
217229

218230
pub async fn inner_get(&self, path: &str) -> Result<reqwest::Response, reqwest::Error> {
219231
reqwest::Client::builder()
232+
.timeout(self.timeout)
220233
.build()
221234
.unwrap()
222235
.get(self.base_url(path).clone())
@@ -246,6 +259,7 @@ impl Http {
246259
pub async fn post_multipart(&self, path: &str, form: multipart::Form) -> TextResponse {
247260
let response = match &self.connection_info.token {
248261
Some(token) => reqwest::Client::builder()
262+
.timeout(self.timeout)
249263
.build()
250264
.unwrap()
251265
.post(self.base_url(path).clone())
@@ -255,6 +269,7 @@ impl Http {
255269
.await
256270
.expect("failed to send multipart request with token"),
257271
None => reqwest::Client::builder()
272+
.timeout(self.timeout)
258273
.build()
259274
.unwrap()
260275
.post(self.base_url(path).clone())

0 commit comments

Comments
 (0)