Skip to content

Commit 2cf5f47

Browse files
committed
refactor: [#472] move generic HTTP client
to the generic HTTP mod.
1 parent 465c853 commit 2cf5f47

File tree

2 files changed

+219
-177
lines changed

2 files changed

+219
-177
lines changed

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

+2-177
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use reqwest::multipart;
2-
use serde::Serialize;
32

43
use super::connection_info::ConnectionInfo;
54
use super::contexts::category::forms::{AddCategoryForm, DeleteCategoryForm};
65
use super::contexts::tag::forms::{AddTagForm, DeleteTagForm};
76
use super::contexts::torrent::forms::UpdateTorrentForm;
87
use super::contexts::torrent::requests::InfoHash;
98
use super::contexts::user::forms::{LoginForm, RegistrationForm, TokenRenewalForm, TokenVerificationForm, Username};
10-
use super::http::{Query, ReqwestQuery};
11-
use super::responses::{self, BinaryResponse, TextResponse};
9+
use super::http::{Http, Query};
10+
use super::responses::{self, TextResponse};
1211

1312
/// API Client
1413
pub struct Client {
@@ -157,177 +156,3 @@ impl Client {
157156
self.http_client.delete(&format!("/user/ban/{}", &username.value)).await
158157
}
159158
}
160-
161-
/// Generic HTTP Client
162-
struct Http {
163-
connection_info: ConnectionInfo,
164-
}
165-
166-
impl Http {
167-
pub fn new(connection_info: ConnectionInfo) -> Self {
168-
Self { connection_info }
169-
}
170-
171-
pub async fn get(&self, path: &str, params: Query) -> TextResponse {
172-
let response = match &self.connection_info.token {
173-
Some(token) => reqwest::Client::builder()
174-
.build()
175-
.unwrap()
176-
.get(self.base_url(path).clone())
177-
.query(&ReqwestQuery::from(params))
178-
.bearer_auth(token)
179-
.send()
180-
.await
181-
.unwrap(),
182-
None => reqwest::Client::builder()
183-
.build()
184-
.unwrap()
185-
.get(self.base_url(path).clone())
186-
.query(&ReqwestQuery::from(params))
187-
.send()
188-
.await
189-
.unwrap(),
190-
};
191-
TextResponse::from(response).await
192-
}
193-
194-
pub async fn get_binary(&self, path: &str, params: Query) -> BinaryResponse {
195-
let response = match &self.connection_info.token {
196-
Some(token) => reqwest::Client::builder()
197-
.build()
198-
.unwrap()
199-
.get(self.base_url(path).clone())
200-
.query(&ReqwestQuery::from(params))
201-
.bearer_auth(token)
202-
.send()
203-
.await
204-
.unwrap(),
205-
None => reqwest::Client::builder()
206-
.build()
207-
.unwrap()
208-
.get(self.base_url(path).clone())
209-
.query(&ReqwestQuery::from(params))
210-
.send()
211-
.await
212-
.unwrap(),
213-
};
214-
// todo: If the response is a JSON, it returns the JSON body in a byte
215-
// array. This is not the expected behavior.
216-
// - Rename BinaryResponse to BinaryTorrentResponse
217-
// - Return an error if the response is not a bittorrent file
218-
BinaryResponse::from(response).await
219-
}
220-
221-
pub async fn inner_get(&self, path: &str) -> Result<reqwest::Response, reqwest::Error> {
222-
reqwest::Client::builder()
223-
.build()
224-
.unwrap()
225-
.get(self.base_url(path).clone())
226-
.send()
227-
.await
228-
}
229-
230-
pub async fn post<T: Serialize + ?Sized>(&self, path: &str, form: &T) -> TextResponse {
231-
let response = match &self.connection_info.token {
232-
Some(token) => reqwest::Client::new()
233-
.post(self.base_url(path).clone())
234-
.bearer_auth(token)
235-
.json(&form)
236-
.send()
237-
.await
238-
.unwrap(),
239-
None => reqwest::Client::new()
240-
.post(self.base_url(path).clone())
241-
.json(&form)
242-
.send()
243-
.await
244-
.unwrap(),
245-
};
246-
TextResponse::from(response).await
247-
}
248-
249-
pub async fn post_multipart(&self, path: &str, form: multipart::Form) -> TextResponse {
250-
let response = match &self.connection_info.token {
251-
Some(token) => reqwest::Client::builder()
252-
.build()
253-
.unwrap()
254-
.post(self.base_url(path).clone())
255-
.multipart(form)
256-
.bearer_auth(token)
257-
.send()
258-
.await
259-
.expect("failed to send multipart request with token"),
260-
None => reqwest::Client::builder()
261-
.build()
262-
.unwrap()
263-
.post(self.base_url(path).clone())
264-
.multipart(form)
265-
.send()
266-
.await
267-
.expect("failed to send multipart request without token"),
268-
};
269-
TextResponse::from(response).await
270-
}
271-
272-
pub async fn put<T: Serialize + ?Sized>(&self, path: &str, form: &T) -> TextResponse {
273-
let response = match &self.connection_info.token {
274-
Some(token) => reqwest::Client::new()
275-
.put(self.base_url(path).clone())
276-
.bearer_auth(token)
277-
.json(&form)
278-
.send()
279-
.await
280-
.unwrap(),
281-
None => reqwest::Client::new()
282-
.put(self.base_url(path).clone())
283-
.json(&form)
284-
.send()
285-
.await
286-
.unwrap(),
287-
};
288-
TextResponse::from(response).await
289-
}
290-
291-
async fn delete(&self, path: &str) -> TextResponse {
292-
let response = match &self.connection_info.token {
293-
Some(token) => reqwest::Client::new()
294-
.delete(self.base_url(path).clone())
295-
.bearer_auth(token)
296-
.send()
297-
.await
298-
.unwrap(),
299-
None => reqwest::Client::new()
300-
.delete(self.base_url(path).clone())
301-
.send()
302-
.await
303-
.unwrap(),
304-
};
305-
TextResponse::from(response).await
306-
}
307-
308-
async fn delete_with_body<T: Serialize + ?Sized>(&self, path: &str, form: &T) -> TextResponse {
309-
let response = match &self.connection_info.token {
310-
Some(token) => reqwest::Client::new()
311-
.delete(self.base_url(path).clone())
312-
.bearer_auth(token)
313-
.json(&form)
314-
.send()
315-
.await
316-
.unwrap(),
317-
None => reqwest::Client::new()
318-
.delete(self.base_url(path).clone())
319-
.json(&form)
320-
.send()
321-
.await
322-
.unwrap(),
323-
};
324-
TextResponse::from(response).await
325-
}
326-
327-
fn base_url(&self, path: &str) -> String {
328-
format!(
329-
"http://{}{}{path}",
330-
&self.connection_info.bind_address, &self.connection_info.base_path
331-
)
332-
}
333-
}

0 commit comments

Comments
 (0)