Define request::get() function

This commit is contained in:
Kitaiti Makoto 2021-11-29 09:51:06 +09:00
parent 388acd6738
commit a7d8d49faf

View File

@ -1,6 +1,9 @@
use chrono::{offset::Utc, DateTime};
use openssl::hash::{Hasher, MessageDigest};
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, DATE, USER_AGENT};
use reqwest::{
header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, DATE, HOST, USER_AGENT},
ClientBuilder, Proxy, Response, Url,
};
use std::ops::Deref;
use std::time::SystemTime;
use tracing::warn;
@ -164,6 +167,33 @@ pub fn signature(
)).map_err(|_| Error())
}
pub fn get(url_str: &str, sender: &dyn Signer, proxy: Option<Proxy>) -> Result<Response, Error> {
let mut headers = headers();
let url = Url::parse(url_str).map_err(|_| Error())?; // TODO: Define and use From trait
if !url.has_host() {
return Err(Error());
}
let host_header_value =
HeaderValue::from_str(url.host_str().expect("Unreachable")).map_err(|_| Error())?;
headers.insert(HOST, host_header_value);
if let Some(proxy) = proxy {
ClientBuilder::new().proxy(proxy)
} else {
ClientBuilder::new()
}
.connect_timeout(Some(std::time::Duration::from_secs(5)))
.build()
.map_err(|_| Error())?
.get(url_str)
.headers(headers.clone())
.header(
"Signature",
signature(sender, &headers, ("get", url.path(), url.query())).map_err(|_| Error())?,
)
.send()
.map_err(|_| Error())
}
#[cfg(test)]
mod tests {
use super::signature;