http_request/websocket/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 {
32 if let Some(existing_key) = existing_key {
33 tmp_header.remove(&existing_key);
34 }
35 }
36 let mut value_deque: VecDeque<String> = VecDeque::new();
37 value_deque.push_front(value_str);
38 tmp_header.insert(key_str, value_deque);
39 }
40 }
41 self
42 }
43
44 pub fn timeout(&mut self, timeout: u64) -> &mut Self {
45 if let Ok(mut config) = self.websocket.config.write() {
46 config.timeout = timeout;
47 }
48 self
49 }
50
51 pub fn buffer(&mut self, buffer: usize) -> &mut Self {
52 if let Ok(mut config) = self.websocket.config.write() {
53 config.buffer = buffer;
54 }
55 self
56 }
57
58 pub fn protocols(&mut self, protocols: &[&str]) -> &mut Self {
59 if let Ok(mut config) = self.websocket.config.write() {
60 config.protocols = protocols.iter().map(|s| s.to_string()).collect();
61 }
62 self
63 }
64
65 pub fn http_proxy(&mut self, host: &str, port: u16) -> &mut Self {
66 if let Ok(mut config) = self.websocket.config.write() {
67 config.proxy = Some(ProxyConfig {
68 proxy_type: ProxyType::Http,
69 host: host.to_string(),
70 port,
71 username: None,
72 password: None,
73 });
74 }
75 self
76 }
77
78 pub fn https_proxy(&mut self, host: &str, port: u16) -> &mut Self {
79 if let Ok(mut config) = self.websocket.config.write() {
80 config.proxy = Some(ProxyConfig {
81 proxy_type: ProxyType::Https,
82 host: host.to_string(),
83 port,
84 username: None,
85 password: None,
86 });
87 }
88 self
89 }
90
91 pub fn socks5_proxy(&mut self, host: &str, port: u16) -> &mut Self {
92 if let Ok(mut config) = self.websocket.config.write() {
93 config.proxy = Some(ProxyConfig {
94 proxy_type: ProxyType::Socks5,
95 host: host.to_string(),
96 port,
97 username: None,
98 password: None,
99 });
100 }
101 self
102 }
103
104 pub fn http_proxy_auth(
105 &mut self,
106 host: &str,
107 port: u16,
108 username: &str,
109 password: &str,
110 ) -> &mut Self {
111 if let Ok(mut config) = self.websocket.config.write() {
112 config.proxy = Some(ProxyConfig {
113 proxy_type: ProxyType::Http,
114 host: host.to_string(),
115 port,
116 username: Some(username.to_string()),
117 password: Some(password.to_string()),
118 });
119 }
120 self
121 }
122
123 pub fn https_proxy_auth(
124 &mut self,
125 host: &str,
126 port: u16,
127 username: &str,
128 password: &str,
129 ) -> &mut Self {
130 if let Ok(mut config) = self.websocket.config.write() {
131 config.proxy = Some(ProxyConfig {
132 proxy_type: ProxyType::Https,
133 host: host.to_string(),
134 port,
135 username: Some(username.to_string()),
136 password: Some(password.to_string()),
137 });
138 }
139 self
140 }
141
142 pub fn socks5_proxy_auth(
143 &mut self,
144 host: &str,
145 port: u16,
146 username: &str,
147 password: &str,
148 ) -> &mut Self {
149 if let Ok(mut config) = self.websocket.config.write() {
150 config.proxy = Some(ProxyConfig {
151 proxy_type: ProxyType::Socks5,
152 host: host.to_string(),
153 port,
154 username: Some(username.to_string()),
155 password: Some(password.to_string()),
156 });
157 }
158 self
159 }
160
161 pub fn build_sync(&mut self) -> WebSocket {
162 self.builder = self.websocket.clone();
163 self.websocket = WebSocket::default();
164 self.builder.clone()
165 }
166
167 pub fn build_async(&mut self) -> WebSocket {
168 self.builder = self.websocket.clone();
169 self.websocket = WebSocket::default();
170 self.builder.clone()
171 }
172}