use crate::*;
impl RequestBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn post(&mut self, url: &str) -> &mut Self {
self.http_request.methods = Arc::new(Method::Post);
self.url(url);
self
}
pub fn get(&mut self, url: &str) -> &mut Self {
self.http_request.methods = Arc::new(Method::Get);
self.url(url);
self
}
fn url(&mut self, url: &str) -> &mut Self {
self.http_request.url = Arc::new(url.to_owned());
self
}
pub fn http1_1_only(&mut self) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.http_version = HttpVersion::Http1_1;
}
self
}
pub fn http2_only(&mut self) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.http_version = HttpVersion::Http2;
}
self
}
pub fn headers<K, V>(&mut self, header: HashMapXxHash3_64<K, V>) -> &mut Self
where
K: ToString,
V: ToString,
{
if let Some(tmp_header) = Arc::get_mut(&mut self.http_request.header) {
for (key, value) in header {
let key_str: String = key.to_string();
let value_str: String = value.to_string();
let mut found_existing: bool = false;
let mut existing_key: Option<String> = None;
for existing_key_ref in tmp_header.keys() {
if existing_key_ref.eq_ignore_ascii_case(&key_str) {
existing_key = Some(existing_key_ref.clone());
found_existing = true;
break;
}
}
if found_existing && let Some(existing_key) = existing_key {
tmp_header.remove(&existing_key);
}
let mut value_deque: VecDeque<String> = VecDeque::new();
value_deque.push_front(value_str);
tmp_header.insert(key_str, value_deque);
}
}
self
}
pub fn json(&mut self, body: serde_json::Value) -> &mut Self {
if let serde_json::Value::Object(map) = body {
let mut res_body: HashMapXxHash3_64<String, serde_json::Value> = hash_map_xx_hash3_64();
for (k, v) in map.iter() {
res_body.insert(k.to_string(), v.clone());
}
self.http_request.body = Arc::new(Body::Json(res_body));
}
self
}
pub fn text<T: ToString>(&mut self, body: T) -> &mut Self {
self.http_request.body = Arc::new(Body::Text(body.to_string()));
self
}
pub fn body<T: Into<Vec<u8>>>(&mut self, body: T) -> &mut Self {
self.http_request.body = Arc::new(Body::Binary(body.into()));
self
}
pub fn timeout(&mut self, timeout: u64) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.timeout = timeout;
}
self
}
pub fn redirect(&mut self) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.redirect = true;
}
self
}
pub fn unredirect(&mut self) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.redirect = false;
};
self
}
pub fn max_redirect_times(&mut self, num: usize) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.max_redirect_times = num;
}
self
}
pub fn buffer(&mut self, buffer: usize) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.buffer = buffer;
}
self
}
pub fn decode(&mut self) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.decode = true;
}
self
}
pub fn undecode(&mut self) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.decode = false;
}
self
}
pub fn http_proxy(&mut self, host: &str, port: u16) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.proxy = Some(ProxyConfig {
proxy_type: ProxyType::Http,
host: host.to_string(),
port,
username: None,
password: None,
});
}
self
}
pub fn https_proxy(&mut self, host: &str, port: u16) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.proxy = Some(ProxyConfig {
proxy_type: ProxyType::Https,
host: host.to_string(),
port,
username: None,
password: None,
});
}
self
}
pub fn socks5_proxy(&mut self, host: &str, port: u16) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.proxy = Some(ProxyConfig {
proxy_type: ProxyType::Socks5,
host: host.to_string(),
port,
username: None,
password: None,
});
}
self
}
pub fn http_proxy_auth(
&mut self,
host: &str,
port: u16,
username: &str,
password: &str,
) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.proxy = Some(ProxyConfig {
proxy_type: ProxyType::Http,
host: host.to_string(),
port,
username: Some(username.to_string()),
password: Some(password.to_string()),
});
}
self
}
pub fn https_proxy_auth(
&mut self,
host: &str,
port: u16,
username: &str,
password: &str,
) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.proxy = Some(ProxyConfig {
proxy_type: ProxyType::Https,
host: host.to_string(),
port,
username: Some(username.to_string()),
password: Some(password.to_string()),
});
}
self
}
pub fn socks5_proxy_auth(
&mut self,
host: &str,
port: u16,
username: &str,
password: &str,
) -> &mut Self {
if let Ok(mut config) = self.http_request.config.write() {
config.proxy = Some(ProxyConfig {
proxy_type: ProxyType::Socks5,
host: host.to_string(),
port,
username: Some(username.to_string()),
password: Some(password.to_string()),
});
}
self
}
pub fn build_async(&mut self) -> BoxAsyncRequestTrait {
self.builder = self.http_request.clone();
self.http_request = HttpRequest::default();
Box::new(self.builder.clone())
}
pub fn build_sync(&mut self) -> BoxRequestTrait {
self.builder = self.http_request.clone();
self.http_request = HttpRequest::default();
Box::new(self.builder.clone())
}
}