|
1 | 1 | use reqwest::Response as ReqwestResponse; |
| 2 | +use serde::Serialize; |
2 | 3 |
|
| 4 | +use super::contexts::user::{LoginForm, RegistrationForm, TokenRenewalForm, TokenVerificationForm, Username}; |
3 | 5 | use crate::e2e::connection_info::ConnectionInfo; |
4 | 6 | use crate::e2e::http::{Query, ReqwestQuery}; |
5 | 7 | use crate::e2e::response::Response; |
6 | 8 |
|
7 | 9 | /// API Client |
8 | 10 | pub struct Client { |
9 | | - connection_info: ConnectionInfo, |
10 | | - base_path: String, |
| 11 | + http_client: Http, |
11 | 12 | } |
12 | 13 |
|
13 | 14 | impl Client { |
14 | 15 | pub fn new(connection_info: ConnectionInfo) -> Self { |
15 | 16 | Self { |
16 | | - connection_info, |
17 | | - base_path: "/".to_string(), |
| 17 | + http_client: Http::new(connection_info), |
18 | 18 | } |
19 | 19 | } |
20 | 20 |
|
21 | | - pub async fn root(&self) -> Response { |
22 | | - self.get("", Query::empty()).await |
23 | | - } |
| 21 | + // Context: about |
24 | 22 |
|
25 | 23 | pub async fn about(&self) -> Response { |
26 | | - self.get("about", Query::empty()).await |
| 24 | + self.http_client.get("about", Query::empty()).await |
27 | 25 | } |
28 | 26 |
|
29 | 27 | pub async fn license(&self) -> Response { |
30 | | - self.get("about/license", Query::empty()).await |
| 28 | + self.http_client.get("about/license", Query::empty()).await |
31 | 29 | } |
32 | 30 |
|
33 | | - pub async fn get(&self, path: &str, params: Query) -> Response { |
34 | | - self.get_request_with_query(path, params).await |
| 31 | + // Context: category |
| 32 | + |
| 33 | + pub async fn get_categories(&self) -> Response { |
| 34 | + self.http_client.get("category", Query::empty()).await |
35 | 35 | } |
36 | 36 |
|
37 | | - /* |
38 | | - pub async fn post(&self, path: &str) -> Response { |
39 | | - let response = reqwest::Client::new().post(self.base_url(path).clone()).send().await.unwrap(); |
40 | | - Response::from(response).await |
| 37 | + // Context: root |
| 38 | + |
| 39 | + pub async fn root(&self) -> Response { |
| 40 | + self.http_client.get("", Query::empty()).await |
41 | 41 | } |
42 | 42 |
|
43 | | - async fn delete(&self, path: &str) -> Response { |
44 | | - reqwest::Client::new() |
45 | | - .delete(self.base_url(path).clone()) |
| 43 | + // Context: user |
| 44 | + |
| 45 | + pub async fn register_user(&self, registration_form: RegistrationForm) -> Response { |
| 46 | + self.http_client.post("user/register", ®istration_form).await |
| 47 | + } |
| 48 | + |
| 49 | + pub async fn login_user(&self, registration_form: LoginForm) -> Response { |
| 50 | + self.http_client.post("user/login", ®istration_form).await |
| 51 | + } |
| 52 | + |
| 53 | + pub async fn verify_token(&self, token_verification_form: TokenVerificationForm) -> Response { |
| 54 | + self.http_client.post("user/token/verify", &token_verification_form).await |
| 55 | + } |
| 56 | + |
| 57 | + pub async fn renew_token(&self, token_verification_form: TokenRenewalForm) -> Response { |
| 58 | + self.http_client.post("user/token/renew", &token_verification_form).await |
| 59 | + } |
| 60 | + |
| 61 | + pub async fn ban_user(&self, username: Username) -> Response { |
| 62 | + self.http_client.delete(&format!("user/ban/{}", &username.value)).await |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// Generic HTTP Client |
| 67 | +struct Http { |
| 68 | + connection_info: ConnectionInfo, |
| 69 | + base_path: String, |
| 70 | +} |
| 71 | + |
| 72 | +impl Http { |
| 73 | + pub fn new(connection_info: ConnectionInfo) -> Self { |
| 74 | + Self { |
| 75 | + connection_info, |
| 76 | + base_path: "/".to_string(), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + pub async fn get(&self, path: &str, params: Query) -> Response { |
| 81 | + self.get_request_with_query(path, params).await |
| 82 | + } |
| 83 | + |
| 84 | + pub async fn post<T: Serialize + ?Sized>(&self, path: &str, form: &T) -> Response { |
| 85 | + let response = reqwest::Client::new() |
| 86 | + .post(self.base_url(path).clone()) |
| 87 | + .json(&form) |
46 | 88 | .send() |
47 | 89 | .await |
48 | | - .unwrap() |
| 90 | + .unwrap(); |
| 91 | + Response::from(response).await |
49 | 92 | } |
50 | 93 |
|
51 | | - pub async fn get_request(&self, path: &str) -> Response { |
52 | | - get(&self.base_url(path), None).await |
| 94 | + async fn delete(&self, path: &str) -> Response { |
| 95 | + let response = match &self.connection_info.token { |
| 96 | + Some(token) => reqwest::Client::new() |
| 97 | + .delete(self.base_url(path).clone()) |
| 98 | + .bearer_auth(token) |
| 99 | + .send() |
| 100 | + .await |
| 101 | + .unwrap(), |
| 102 | + None => reqwest::Client::new() |
| 103 | + .delete(self.base_url(path).clone()) |
| 104 | + .send() |
| 105 | + .await |
| 106 | + .unwrap(), |
| 107 | + }; |
| 108 | + Response::from(response).await |
53 | 109 | } |
54 | | - */ |
55 | 110 |
|
56 | 111 | pub async fn get_request_with_query(&self, path: &str, params: Query) -> Response { |
57 | 112 | get(&self.base_url(path), Some(params)).await |
|
0 commit comments