smbcloud_network/
network.rs1use {
2 log::error,
3 reqwest::{RequestBuilder, Response, StatusCode},
4 serde::de::DeserializeOwned,
5 smbcloud_model::{
6 account::SmbAuthorization,
7 error_codes::{ErrorCode, ErrorResponse},
8 login::AccountStatus,
9 },
10};
11
12#[cfg(not(target_arch = "wasm32"))]
13use log::debug;
14
15#[cfg(debug_assertions)]
17const LOG_RESPONSE_BODY: bool = true; #[cfg(not(debug_assertions))]
19const LOG_RESPONSE_BODY: bool = false;
20
21#[cfg(not(target_arch = "wasm32"))]
27pub async fn check_internet_connection() -> bool {
28 debug!("Checking internet connection");
29 let client = reqwest::Client::builder()
30 .build();
32
33 if let Err(e) = client {
34 error!("Failed to create client for connectivity check: {:?}", e);
35 return false;
36 }
37
38 if let Ok(client) = client {
39 match client.get("https://dns.google").send().await {
40 Ok(response) => {
41 debug!(
42 "Internet connection check successful: {}",
43 response.status()
44 );
45 response.status().is_success()
46 }
47 Err(e) => {
48 error!("Internet connection check failed: {:?}", e);
49 false
50 }
51 }
52 } else {
53 false
54 }
55}
56
57#[cfg(target_arch = "wasm32")]
58pub async fn check_internet_connection() -> bool {
59 true
60}
61
62pub async fn parse_error_response<T: DeserializeOwned>(
63 response: Response,
64) -> Result<T, ErrorResponse> {
65 let error_response_body = match response.text().await {
66 Ok(body) => body,
67 Err(e) => {
68 error!("Failed to get response body: {:?}", e);
69 return Err(ErrorResponse::Error {
70 error_code: ErrorCode::NetworkError,
71 message: ErrorCode::NetworkError.message(None).to_string(),
72 });
73 }
74 };
75
76 if LOG_RESPONSE_BODY {
77 println!();
78 println!("Parse Error >>>>");
79 println!("{:?}", serde_json::to_string_pretty(&error_response_body));
80 println!("Parse Error >>>>");
81 println!();
82 }
83
84 let error_response = match serde_json::from_str(&error_response_body) {
85 Ok(error_response) => error_response,
86 Err(e) => {
87 error!("Failed to parse error response: {:?}", e);
88 return Err(ErrorResponse::Error {
89 error_code: ErrorCode::ParseError,
90 message: ErrorCode::ParseError.message(None).to_string(),
91 });
92 }
93 };
94 Err(error_response)
97}
98
99pub async fn request_login(builder: RequestBuilder) -> Result<AccountStatus, ErrorResponse> {
100 let response = builder.send().await;
101 let response = match response {
102 Ok(response) => response,
103 Err(e) => {
104 error!("request_login: Failed to get response: {:?}", e);
105 return Err(ErrorResponse::Error {
106 error_code: ErrorCode::NetworkError,
107 message: ErrorCode::NetworkError.message(None).to_string(),
108 });
109 }
110 };
111
112 if LOG_RESPONSE_BODY {
113 println!();
114 println!("request_login: Parse >>>>");
115 println!("{:?}", &response.status());
116 println!("request_login: Parse >>>>");
117 println!();
118 }
119
120 match (response.status(), response.headers().get("Authorization")) {
121 (StatusCode::OK, Some(token)) => {
122 let access_token = match token.to_str() {
124 Ok(token) => token.to_string(),
125 Err(_) => {
126 return Err(ErrorResponse::Error {
127 error_code: ErrorCode::NetworkError,
128 message: ErrorCode::NetworkError.message(None).to_string(),
129 });
130 }
131 };
132 Ok(AccountStatus::Ready { access_token })
133 }
134 (StatusCode::OK, None) => {
135 let error_response = match parse_error_response::<ErrorResponse>(response).await {
137 Ok(error) => error,
138 Err(_) => return Ok(AccountStatus::NotFound),
139 };
140 match error_response {
141 ErrorResponse::Error {
142 error_code,
143 message: _,
144 } => match error_code {
145 ErrorCode::EmailNotVerified => Ok(AccountStatus::Incomplete {
146 status: smbcloud_model::account::ErrorCode::EmailUnverified,
147 }),
148 ErrorCode::PasswordNotSet => Ok(AccountStatus::Incomplete {
149 status: smbcloud_model::account::ErrorCode::PasswordNotSet,
150 }),
151 ErrorCode::Unknown => Ok(AccountStatus::Ready {
152 access_token: "tokenization".to_string(),
153 }),
154 _ => Ok(AccountStatus::NotFound),
155 },
156 }
157 }
158 (StatusCode::NOT_FOUND, _) => {
159 Ok(AccountStatus::NotFound)
161 }
162 (StatusCode::UNPROCESSABLE_ENTITY, _) => {
163 let result: SmbAuthorization = match response.json().await {
165 Ok(res) => res,
166 Err(_) => {
167 return Err(ErrorResponse::Error {
168 error_code: ErrorCode::NetworkError,
169 message: ErrorCode::NetworkError.message(None).to_string(),
170 });
171 }
172 };
173 let error_code = match result.error_code {
175 Some(code) => code,
176 None => {
177 return Err(ErrorResponse::Error {
178 error_code: ErrorCode::NetworkError,
179 message: ErrorCode::NetworkError.message(None).to_string(),
180 });
181 }
182 };
183 Ok(AccountStatus::Incomplete { status: error_code })
184 }
185 _ => Err(ErrorResponse::Error {
186 error_code: ErrorCode::NetworkError,
187 message: ErrorCode::NetworkError.message(None).to_string(),
188 }),
189 }
190}
191
192pub async fn request<R: DeserializeOwned>(builder: RequestBuilder) -> Result<R, ErrorResponse> {
193 if !check_internet_connection().await {
195 error!("No internet connection available");
196 return Err(ErrorResponse::Error {
197 error_code: ErrorCode::NetworkError,
198 message: "No internet connection. Please check your network settings and try again."
199 .to_string(),
200 });
201 }
202
203 let response = builder.send().await;
204 let response = match response {
205 Ok(response) => response,
206 Err(e) => {
207 error!("Failed to get response: {:?}", e);
208 return Err(ErrorResponse::Error {
209 error_code: ErrorCode::NetworkError,
210 message: ErrorCode::NetworkError.message(None).to_string(),
211 });
212 }
213 };
214 let response = match response.status() {
215 reqwest::StatusCode::OK | reqwest::StatusCode::CREATED => response,
216 status => {
217 error!("Failed to get response: {:?}", status);
218 return parse_error_response(response).await;
220 }
221 };
222
223 let response_body = match response.text().await {
224 Ok(body) => body,
225 Err(e) => {
226 error!("Failed to get response body: {:?}", e);
227 return Err(ErrorResponse::Error {
228 error_code: ErrorCode::NetworkError,
229 message: ErrorCode::NetworkError.message(None).to_string(),
230 });
231 }
232 };
233
234 if LOG_RESPONSE_BODY {
235 println!();
236 println!("Parse >>>>");
237 println!("{:?}", serde_json::to_string_pretty(&response_body));
238 println!("Parse >>>>");
239 println!();
240 }
241
242 let response = match serde_json::from_str::<R>(&response_body) {
243 Ok(response) => response,
244 Err(e) => {
245 error!("Failed to parse response: {:?}", e);
246 return Err(ErrorResponse::Error {
247 error_code: ErrorCode::ParseError,
248 message: e.to_string(),
249 });
250 }
251 };
252
253 Ok(response)
254}