1use crate::rest::Error;
18use crate::util::to_base64;
19use hyper::body;
20use hyper::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
21use hyper::{Body, Client, Request};
22use hyper_timeout::TimeoutConnector;
23use serde::{Deserialize, Serialize};
24use std::time::Duration;
25use tokio::runtime::Builder;
26
27pub struct TimeOut {
29 pub connect: Duration,
30 pub read: Duration,
31 pub write: Duration,
32}
33
34impl TimeOut {
35 pub fn new(connect: u64, read: u64, write: u64) -> Self {
36 Self {
37 connect: Duration::from_secs(connect),
38 read: Duration::from_secs(read),
39 write: Duration::from_secs(write),
40 }
41 }
42}
43
44impl Default for TimeOut {
45 fn default() -> TimeOut {
46 TimeOut {
47 connect: Duration::from_secs(20),
48 read: Duration::from_secs(20),
49 write: Duration::from_secs(20),
50 }
51 }
52}
53
54pub fn get<T>(url: &str, api_secret: Option<String>) -> Result<T, Error>
61where
62 for<'de> T: Deserialize<'de>,
63{
64 handle_request(
65 build_request(url, "GET", api_secret, None)?,
66 TimeOut::default(),
67 )
68}
69
70pub async fn get_async<T>(url: &str, api_secret: Option<String>) -> Result<T, Error>
74where
75 for<'de> T: Deserialize<'de> + Send + 'static,
76{
77 handle_request_async(build_request(url, "GET", api_secret, None)?).await
78}
79
80pub fn get_no_ret(url: &str, api_secret: Option<String>) -> Result<(), Error> {
84 send_request(
85 build_request(url, "GET", api_secret, None)?,
86 TimeOut::default(),
87 )?;
88 Ok(())
89}
90
91pub fn post<IN, OUT>(
96 url: &str,
97 api_secret: Option<String>,
98 input: &IN,
99 timeout: TimeOut,
100) -> Result<OUT, Error>
101where
102 IN: Serialize,
103 for<'de> OUT: Deserialize<'de>,
104{
105 let req = create_post_request(url, api_secret, input)?;
106 handle_request(req, timeout)
107}
108
109pub async fn post_async<IN, OUT>(
114 url: &str,
115 input: &IN,
116 api_secret: Option<String>,
117) -> Result<OUT, Error>
118where
119 IN: Serialize,
120 OUT: Send + 'static,
121 for<'de> OUT: Deserialize<'de>,
122{
123 handle_request_async(create_post_request(url, api_secret, input)?).await
124}
125
126pub fn post_no_ret<IN>(url: &str, api_secret: Option<String>, input: &IN) -> Result<(), Error>
131where
132 IN: Serialize,
133{
134 send_request(
135 create_post_request(url, api_secret, input)?,
136 TimeOut::default(),
137 )?;
138 Ok(())
139}
140
141pub async fn post_no_ret_async<IN>(
146 url: &str,
147 api_secret: Option<String>,
148 input: &IN,
149) -> Result<(), Error>
150where
151 IN: Serialize,
152{
153 send_request_async(
154 create_post_request(url, api_secret, input)?,
155 TimeOut::default(),
156 )
157 .await?;
158 Ok(())
159}
160
161fn build_request(
162 url: &str,
163 method: &str,
164 api_secret: Option<String>,
165 body: Option<String>,
166) -> Result<Request<Body>, Error> {
167 let mut builder = Request::builder();
168 if let Some(api_secret) = api_secret {
169 let basic_auth = format!("Basic {}", to_base64(&format!("grin:{}", api_secret)));
170 builder = builder.header(AUTHORIZATION, basic_auth);
171 }
172
173 builder
174 .method(method)
175 .uri(url)
176 .header(USER_AGENT, "grin-client")
177 .header(ACCEPT, "application/json")
178 .header(CONTENT_TYPE, "application/json")
179 .body(match body {
180 None => Body::empty(),
181 Some(json) => json.into(),
182 })
183 .map_err(|e| Error::RequestError(format!("Bad request {} {}: {}", method, url, e)))
184}
185
186pub fn create_post_request<IN>(
187 url: &str,
188 api_secret: Option<String>,
189 input: &IN,
190) -> Result<Request<Body>, Error>
191where
192 IN: Serialize,
193{
194 let json = serde_json::to_string(input)
195 .map_err(|e| Error::Internal(format!("Could not serialize data to JSON: {}", e)))?;
196 build_request(url, "POST", api_secret, Some(json))
197}
198
199fn handle_request<T>(req: Request<Body>, timeout: TimeOut) -> Result<T, Error>
200where
201 for<'de> T: Deserialize<'de>,
202{
203 let data = send_request(req, timeout)?;
204 serde_json::from_str(&data)
205 .map_err(|e| Error::ResponseError(format!("Cannot parse response {}", e)))
206}
207
208async fn handle_request_async<T>(req: Request<Body>) -> Result<T, Error>
209where
210 for<'de> T: Deserialize<'de> + Send + 'static,
211{
212 let data = send_request_async(req, TimeOut::default()).await?;
213 let ser = serde_json::from_str(&data)
214 .map_err(|e| Error::ResponseError(format!("Cannot parse response {}", e)))?;
215 Ok(ser)
216}
217
218async fn send_request_async(req: Request<Body>, timeout: TimeOut) -> Result<String, Error> {
219 let https = hyper_rustls::HttpsConnector::new();
220 let (connect, read, write) = (
221 Some(timeout.connect),
222 Some(timeout.read),
223 Some(timeout.write),
224 );
225 let mut connector = TimeoutConnector::new(https);
226 connector.set_connect_timeout(connect);
227 connector.set_read_timeout(read);
228 connector.set_write_timeout(write);
229 let client = Client::builder().build::<_, Body>(connector);
230
231 let resp = client
232 .request(req)
233 .await
234 .map_err(|e| Error::RequestError(format!("Cannot make request: {}", e)))?;
235
236 if !resp.status().is_success() {
237 return Err(Error::RequestError(format!(
238 "Wrong response code: {} with data {:?}",
239 resp.status(),
240 resp.body()
241 ))
242 .into());
243 }
244
245 let raw = body::to_bytes(resp)
246 .await
247 .map_err(|e| Error::RequestError(format!("Cannot read response body: {}", e)))?;
248
249 Ok(String::from_utf8_lossy(&raw).to_string())
250}
251
252pub fn send_request(req: Request<Body>, timeout: TimeOut) -> Result<String, Error> {
253 let mut rt = Builder::new()
254 .basic_scheduler()
255 .enable_all()
256 .build()
257 .map_err(|e| Error::RequestError(format!("{}", e)))?;
258 rt.block_on(send_request_async(req, timeout))
259}