use crate::cookie::Cookie;
use crate::fingerprint::BrowserFingerprint;
use crate::proxy::ProxyConfig;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct SolveRequest {
pub url: String,
pub method: SolveMethod,
pub headers: Option<HashMap<String, String>>,
pub cookies: Option<Vec<Cookie>>,
pub max_timeout_ms: Option<u64>,
pub session_id: Option<String>,
pub proxy: Option<ProxyConfig>,
pub bypass_cache: bool,
pub fingerprint: Option<BrowserFingerprint>,
}
#[derive(Debug, Clone)]
pub enum SolveMethod {
Get,
Post { body: PostBody },
}
#[derive(Debug, Clone)]
pub enum PostBody {
Form(HashMap<String, String>),
Json(serde_json::Value),
Raw {
content_type: String,
body: Vec<u8>,
},
}
impl SolveRequest {
pub fn get(url: impl Into<String>) -> Self {
Self {
url: url.into(),
method: SolveMethod::Get,
headers: None,
cookies: None,
max_timeout_ms: None,
session_id: None,
proxy: None,
bypass_cache: false,
fingerprint: None,
}
}
pub fn post(url: impl Into<String>) -> Self {
Self {
url: url.into(),
method: SolveMethod::Post {
body: PostBody::Form(HashMap::new()),
},
headers: None,
cookies: None,
max_timeout_ms: None,
session_id: None,
proxy: None,
bypass_cache: false,
fingerprint: None,
}
}
pub fn form<K, V, I>(mut self, fields: I) -> Self
where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
{
let map = fields.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
self.method = SolveMethod::Post {
body: PostBody::Form(map),
};
self
}
pub fn json(mut self, value: serde_json::Value) -> Self {
self.method = SolveMethod::Post {
body: PostBody::Json(value),
};
self
}
pub fn raw_body(mut self, content_type: impl Into<String>, body: Vec<u8>) -> Self {
self.method = SolveMethod::Post {
body: PostBody::Raw {
content_type: content_type.into(),
body,
},
};
self
}
pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
self.headers = Some(headers);
self
}
pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers
.get_or_insert_with(HashMap::new)
.insert(name.into(), value.into());
self
}
pub fn with_cookies(mut self, cookies: Vec<Cookie>) -> Self {
self.cookies = Some(cookies);
self
}
pub fn with_cookie(mut self, cookie: Cookie) -> Self {
self.cookies.get_or_insert_with(Vec::new).push(cookie);
self
}
pub fn with_timeout_ms(mut self, ms: u64) -> Self {
self.max_timeout_ms = Some(ms);
self
}
pub fn with_session(mut self, session_id: impl Into<String>) -> Self {
self.session_id = Some(session_id.into());
self
}
pub fn with_proxy(mut self, proxy: ProxyConfig) -> Self {
self.proxy = Some(proxy);
self
}
pub fn bypass_cache(mut self) -> Self {
self.bypass_cache = true;
self
}
pub fn with_fingerprint(mut self, fingerprint: BrowserFingerprint) -> Self {
self.fingerprint = Some(fingerprint);
self
}
}