http_request/request/socket/websocket_builder/
impl.rs1use crate::*;
2
3impl WebSocketBuilder {
4 pub fn new() -> Self {
5 Self::default()
6 }
7
8 pub fn connect(&mut self, url: &str) -> &mut Self {
9 self.websocket.url = Arc::new(url.to_owned());
10 self
11 }
12
13 pub fn headers<K, V>(&mut self, header: HashMapXxHash3_64<K, V>) -> &mut Self
14 where
15 K: ToString,
16 V: ToString,
17 {
18 if let Some(tmp_header) = Arc::get_mut(&mut self.websocket.header) {
19 for (key, value) in header {
20 let key_str: String = key.to_string();
21 let value_str: String = value.to_string();
22 let mut found_existing: bool = false;
23 let mut existing_key: Option<String> = None;
24 for existing_key_ref in tmp_header.keys() {
25 if existing_key_ref.eq_ignore_ascii_case(&key_str) {
26 existing_key = Some(existing_key_ref.clone());
27 found_existing = true;
28 break;
29 }
30 }
31 if found_existing && let Some(existing_key) = existing_key {
32 tmp_header.remove(&existing_key);
33 }
34 let mut value_deque: VecDeque<String> = VecDeque::new();
35 value_deque.push_front(value_str);
36 tmp_header.insert(key_str, value_deque);
37 }
38 }
39 self
40 }
41
42 pub fn timeout(&mut self, timeout: u64) -> &mut Self {
43 if let Ok(mut config) = self.websocket.config.write() {
44 config.timeout = timeout;
45 }
46 self
47 }
48
49 pub fn buffer(&mut self, buffer: usize) -> &mut Self {
50 if let Ok(mut config) = self.websocket.config.write() {
51 config.buffer = buffer;
52 }
53 self
54 }
55
56 pub fn protocols(&mut self, protocols: &[&str]) -> &mut Self {
57 if let Ok(mut config) = self.websocket.config.write() {
58 config.protocols = protocols.iter().map(|s| s.to_string()).collect();
59 }
60 self
61 }
62
63 pub fn http_proxy(&mut self, host: &str, port: u16) -> &mut Self {
64 if let Ok(mut config) = self.websocket.config.write() {
65 config.proxy = Some(ProxyConfig {
66 proxy_type: ProxyType::Http,
67 host: host.to_string(),
68 port,
69 username: None,
70 password: None,
71 });
72 }
73 self
74 }
75
76 pub fn https_proxy(&mut self, host: &str, port: u16) -> &mut Self {
77 if let Ok(mut config) = self.websocket.config.write() {
78 config.proxy = Some(ProxyConfig {
79 proxy_type: ProxyType::Https,
80 host: host.to_string(),
81 port,
82 username: None,
83 password: None,
84 });
85 }
86 self
87 }
88
89 pub fn socks5_proxy(&mut self, host: &str, port: u16) -> &mut Self {
90 if let Ok(mut config) = self.websocket.config.write() {
91 config.proxy = Some(ProxyConfig {
92 proxy_type: ProxyType::Socks5,
93 host: host.to_string(),
94 port,
95 username: None,
96 password: None,
97 });
98 }
99 self
100 }
101
102 pub fn http_proxy_auth(
103 &mut self,
104 host: &str,
105 port: u16,
106 username: &str,
107 password: &str,
108 ) -> &mut Self {
109 if let Ok(mut config) = self.websocket.config.write() {
110 config.proxy = Some(ProxyConfig {
111 proxy_type: ProxyType::Http,
112 host: host.to_string(),
113 port,
114 username: Some(username.to_string()),
115 password: Some(password.to_string()),
116 });
117 }
118 self
119 }
120
121 pub fn https_proxy_auth(
122 &mut self,
123 host: &str,
124 port: u16,
125 username: &str,
126 password: &str,
127 ) -> &mut Self {
128 if let Ok(mut config) = self.websocket.config.write() {
129 config.proxy = Some(ProxyConfig {
130 proxy_type: ProxyType::Https,
131 host: host.to_string(),
132 port,
133 username: Some(username.to_string()),
134 password: Some(password.to_string()),
135 });
136 }
137 self
138 }
139
140 pub fn socks5_proxy_auth(
141 &mut self,
142 host: &str,
143 port: u16,
144 username: &str,
145 password: &str,
146 ) -> &mut Self {
147 if let Ok(mut config) = self.websocket.config.write() {
148 config.proxy = Some(ProxyConfig {
149 proxy_type: ProxyType::Socks5,
150 host: host.to_string(),
151 port,
152 username: Some(username.to_string()),
153 password: Some(password.to_string()),
154 });
155 }
156 self
157 }
158
159 pub fn build_sync(&mut self) -> WebSocket {
160 self.builder = self.websocket.clone();
161 self.websocket = WebSocket::default();
162 self.builder.clone()
163 }
164
165 pub fn build_async(&mut self) -> WebSocket {
166 self.builder = self.websocket.clone();
167 self.websocket = WebSocket::default();
168 self.builder.clone()
169 }
170}