1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::cookie::Cookie;
use crate::{Response, Result};
use http::Method;
use reqwest_cookie_store::CookieStoreMutex;
use serde_json::Value;
use std::sync::Arc;
pub struct Client {
base_url: Option<String>,
cookie_store: Arc<CookieStoreMutex>,
client: reqwest::Client,
}
pub fn new_client(base_url: impl Into<BaseUrl>) -> Result<Client> {
let base_url = base_url.into().into();
let cookie_store = Arc::new(CookieStoreMutex::default());
let client = reqwest::Client::builder().cookie_provider(cookie_store.clone()).build()?;
Ok(Client {
base_url,
cookie_store,
client,
})
}
impl Client {
pub async fn do_get(&self, url: &str) -> Result<Response> {
let url = self.compose_url(url);
let reqwest_res = self.client.get(&url).send().await?;
self.capture_response(Method::GET, url, reqwest_res).await
}
pub async fn do_delete(&self, url: &str) -> Result<Response> {
let url = self.compose_url(url);
let reqwest_res = self.client.delete(&url).send().await?;
self.capture_response(Method::DELETE, url, reqwest_res).await
}
pub async fn do_post(&self, url: &str, content: impl Into<PostContent>) -> Result<Response> {
self.do_push(Method::POST, url, content.into()).await
}
pub async fn do_put(&self, url: &str, content: impl Into<PostContent>) -> Result<Response> {
self.do_push(Method::PUT, url, content.into()).await
}
pub async fn do_patch(&self, url: &str, content: impl Into<PostContent>) -> Result<Response> {
self.do_push(Method::PUT, url, content.into()).await
}
async fn do_push(&self, method: Method, url: &str, content: PostContent) -> Result<Response> {
let url = self.compose_url(url);
let reqwest_res = match content {
PostContent::Json(value) => self.client.post(&url).json(&value).send().await?,
PostContent::Text { content_type, body } => {
self.client
.post(&url)
.body(body)
.header("content-type", content_type)
.send()
.await?
}
};
self.capture_response(method, url, reqwest_res).await
}
#[allow(clippy::await_holding_lock)] async fn capture_response(
&self,
request_method: Method,
url: String,
reqwest_res: reqwest::Response,
) -> Result<Response> {
let cookie_store = self.cookie_store.lock().unwrap();
let client_cookies: Vec<Cookie> = cookie_store
.iter_any()
.map(|c| Cookie {
name: c.name().to_string(),
value: c.value().to_string(),
})
.collect();
Response::from_reqwest_response(request_method, url, client_cookies, reqwest_res).await
}
fn compose_url(&self, url: &str) -> String {
match &self.base_url {
Some(base_url) => format!("{base_url}{url}"),
None => url.to_string(),
}
}
}
pub enum PostContent {
Json(Value),
Text { body: String, content_type: &'static str },
}
impl From<Value> for PostContent {
fn from(val: Value) -> Self {
PostContent::Json(val)
}
}
impl From<String> for PostContent {
fn from(val: String) -> Self {
PostContent::Text {
content_type: "text/plain",
body: val,
}
}
}
impl From<&String> for PostContent {
fn from(val: &String) -> Self {
PostContent::Text {
content_type: "text/plain",
body: val.to_string(),
}
}
}
impl From<&str> for PostContent {
fn from(val: &str) -> Self {
PostContent::Text {
content_type: "text/plain",
body: val.to_string(),
}
}
}
impl From<(String, &'static str)> for PostContent {
fn from((body, content_type): (String, &'static str)) -> Self {
PostContent::Text { body, content_type }
}
}
impl From<(&str, &'static str)> for PostContent {
fn from((body, content_type): (&str, &'static str)) -> Self {
PostContent::Text {
body: body.to_string(),
content_type,
}
}
}
pub struct BaseUrl(Option<String>);
impl From<&str> for BaseUrl {
fn from(val: &str) -> Self {
BaseUrl(Some(val.to_string()))
}
}
impl From<String> for BaseUrl {
fn from(val: String) -> Self {
BaseUrl(Some(val))
}
}
impl From<&String> for BaseUrl {
fn from(val: &String) -> Self {
BaseUrl(Some(val.to_string()))
}
}
impl From<BaseUrl> for Option<String> {
fn from(val: BaseUrl) -> Self {
val.0
}
}
impl From<Option<String>> for BaseUrl {
fn from(val: Option<String>) -> Self {
BaseUrl(val)
}
}