Skip to main content

headless_engine/browser/
builder.rs

1use crate::browser::engine::BrowserEngine;
2use crate::browser::tab::BrowserTab;
3use crate::network::client::NetworkClient;
4use crate::network::fingerprint::DeviceProfile;
5use anyhow::Result;
6use std::time::Duration;
7
8#[derive(Debug, Clone)]
9pub struct BrowserBuilder {
10    pub profile: DeviceProfile,
11    pub proxy_url: Option<String>,
12    pub timeout: Duration,
13    pub custom_user_agent: Option<String>,
14    pub max_redirects: usize,
15}
16
17impl Default for BrowserBuilder {
18    fn default() -> Self {
19        Self {
20            profile: DeviceProfile::ChromeWindows,
21            proxy_url: None,
22            timeout: Duration::from_secs(30),
23            custom_user_agent: None,
24            max_redirects: 10,
25        }
26    }
27}
28
29impl BrowserBuilder {
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    pub fn profile(mut self, profile: DeviceProfile) -> Self {
35        self.profile = profile;
36        self
37    }
38
39    pub fn proxy<S: Into<String>>(mut self, proxy_url: S) -> Self {
40        self.proxy_url = Some(proxy_url.into());
41        self
42    }
43
44    pub fn timeout(mut self, timeout: Duration) -> Self {
45        self.timeout = timeout;
46        self
47    }
48
49    pub fn custom_user_agent<S: Into<String>>(mut self, ua: S) -> Self {
50        self.custom_user_agent = Some(ua.into());
51        self
52    }
53
54    pub fn max_redirects(mut self, redirects: usize) -> Self {
55        self.max_redirects = redirects;
56        self
57    }
58
59    pub fn build(self) -> Result<BrowserTab> {
60        let network = NetworkClient::with_builder_config(
61            self.profile,
62            self.proxy_url.as_deref(),
63            self.timeout,
64            self.max_redirects,
65            self.custom_user_agent.as_deref(),
66        )?;
67        BrowserTab::from_network(network)
68    }
69
70    pub fn build_engine(self) -> Result<BrowserEngine> {
71        BrowserEngine::with_builder(self)
72    }
73}