http_request/request/socket/websocket_builder/
impl.rs1use super::*;
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
59 .iter()
60 .map(|protocol: &&str| protocol.to_string())
61 .collect();
62 }
63 self
64 }
65
66 pub fn http_proxy(&mut self, host: &str, port: u16) -> &mut Self {
67 if let Ok(mut config) = self.websocket.config.write() {
68 config.proxy = Some(ProxyConfig {
69 proxy_type: ProxyType::Http,
70 host: host.to_string(),
71 port,
72 username: None,
73 password: None,
74 });
75 }
76 self
77 }
78
79 pub fn https_proxy(&mut self, host: &str, port: u16) -> &mut Self {
80 if let Ok(mut config) = self.websocket.config.write() {
81 config.proxy = Some(ProxyConfig {
82 proxy_type: ProxyType::Https,
83 host: host.to_string(),
84 port,
85 username: None,
86 password: None,
87 });
88 }
89 self
90 }
91
92 pub fn socks5_proxy(&mut self, host: &str, port: u16) -> &mut Self {
93 if let Ok(mut config) = self.websocket.config.write() {
94 config.proxy = Some(ProxyConfig {
95 proxy_type: ProxyType::Socks5,
96 host: host.to_string(),
97 port,
98 username: None,
99 password: None,
100 });
101 }
102 self
103 }
104
105 pub fn http_proxy_auth(
106 &mut self,
107 host: &str,
108 port: u16,
109 username: &str,
110 password: &str,
111 ) -> &mut Self {
112 if let Ok(mut config) = self.websocket.config.write() {
113 config.proxy = Some(ProxyConfig {
114 proxy_type: ProxyType::Http,
115 host: host.to_string(),
116 port,
117 username: Some(username.to_string()),
118 password: Some(password.to_string()),
119 });
120 }
121 self
122 }
123
124 pub fn https_proxy_auth(
125 &mut self,
126 host: &str,
127 port: u16,
128 username: &str,
129 password: &str,
130 ) -> &mut Self {
131 if let Ok(mut config) = self.websocket.config.write() {
132 config.proxy = Some(ProxyConfig {
133 proxy_type: ProxyType::Https,
134 host: host.to_string(),
135 port,
136 username: Some(username.to_string()),
137 password: Some(password.to_string()),
138 });
139 }
140 self
141 }
142
143 pub fn socks5_proxy_auth(
144 &mut self,
145 host: &str,
146 port: u16,
147 username: &str,
148 password: &str,
149 ) -> &mut Self {
150 if let Ok(mut config) = self.websocket.config.write() {
151 config.proxy = Some(ProxyConfig {
152 proxy_type: ProxyType::Socks5,
153 host: host.to_string(),
154 port,
155 username: Some(username.to_string()),
156 password: Some(password.to_string()),
157 });
158 }
159 self
160 }
161
162 pub fn build_sync(&mut self) -> WebSocket {
163 self.builder = self.websocket.clone();
164 self.websocket = WebSocket::default();
165 self.builder.clone()
166 }
167
168 pub fn build_async(&mut self) -> WebSocket {
169 self.builder = self.websocket.clone();
170 self.websocket = WebSocket::default();
171 self.builder.clone()
172 }
173}