Skip to main content

smbcloud_network/
network.rs

1use {
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//use std::time::Duration;
16#[cfg(debug_assertions)]
17const LOG_RESPONSE_BODY: bool = true; // You know what to do here.
18#[cfg(not(debug_assertions))]
19const LOG_RESPONSE_BODY: bool = false;
20
21/// Check if there is an active internet connection.
22///
23/// Native clients can afford a preflight connectivity check. Browser/wasm
24/// clients should skip it and rely on the real request, otherwise the check
25/// itself becomes a separate cross-origin failure surface.
26#[cfg(not(target_arch = "wasm32"))]
27pub async fn check_internet_connection() -> bool {
28    debug!("Checking internet connection");
29    let client = reqwest::Client::builder()
30        //.timeout(Duration::from_secs(5)) Does not work on wasm32
31        .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    // The parsing itself is succeed.
95    // Why is this an ErrorResponse.
96    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            // Login successful, let's get the access token for real.
123            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            // Silent login from oauth. Need improvement.
136            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            // Account not found
160            Ok(AccountStatus::NotFound)
161        }
162        (StatusCode::UNPROCESSABLE_ENTITY, _) => {
163            // Account found but email not verified / password not set.
164            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            // println!("Result: {:#?}", &result);
174            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        (StatusCode::UNAUTHORIZED, _) => {
186            let result: SmbAuthorization = match response.json().await {
187                Ok(res) => res,
188                Err(_) => {
189                    return Err(ErrorResponse::Error {
190                        error_code: ErrorCode::NetworkError,
191                        message: ErrorCode::NetworkError.message(None).to_string(),
192                    });
193                }
194            };
195            let error_code = match result.error_code {
196                Some(code) => code,
197                None => {
198                    return Err(ErrorResponse::Error {
199                        error_code: ErrorCode::NetworkError,
200                        message: ErrorCode::NetworkError.message(None).to_string(),
201                    });
202                }
203            };
204            Err(ErrorResponse::Error {
205                error_code: ErrorCode::Unauthorized,
206                message: error_code.to_string(),
207            })
208        }
209        (status, _) => parse_error_response(response)
210            .await
211            .map_err(|_| ErrorResponse::Error {
212                error_code: ErrorCode::NetworkError,
213                message: format!("Unexpected login response status: {}", status),
214            }),
215    }
216}
217
218pub async fn request<R: DeserializeOwned>(builder: RequestBuilder) -> Result<R, ErrorResponse> {
219    // Check internet connection before making the request
220    if !check_internet_connection().await {
221        error!("No internet connection available");
222        return Err(ErrorResponse::Error {
223            error_code: ErrorCode::NetworkError,
224            message: "No internet connection. Please check your network settings and try again."
225                .to_string(),
226        });
227    }
228
229    let response = builder.send().await;
230    let response = match response {
231        Ok(response) => response,
232        Err(e) => {
233            error!("Failed to get response: {:?}", e);
234            return Err(ErrorResponse::Error {
235                error_code: ErrorCode::NetworkError,
236                message: ErrorCode::NetworkError.message(None).to_string(),
237            });
238        }
239    };
240    let response = match response.status() {
241        reqwest::StatusCode::OK | reqwest::StatusCode::CREATED => response,
242        status => {
243            error!("Failed to get response: {:?}", status);
244            // This should handle parsing the error response.
245            return parse_error_response(response).await;
246        }
247    };
248
249    let response_body = match response.text().await {
250        Ok(body) => body,
251        Err(e) => {
252            error!("Failed to get response body: {:?}", e);
253            return Err(ErrorResponse::Error {
254                error_code: ErrorCode::NetworkError,
255                message: ErrorCode::NetworkError.message(None).to_string(),
256            });
257        }
258    };
259
260    if LOG_RESPONSE_BODY {
261        println!();
262        println!("Parse >>>>");
263        println!("{:?}", serde_json::to_string_pretty(&response_body));
264        println!("Parse >>>>");
265        println!();
266    }
267
268    let response = match serde_json::from_str::<R>(&response_body) {
269        Ok(response) => response,
270        Err(e) => {
271            error!("Failed to parse response: {:?}", e);
272            return Err(ErrorResponse::Error {
273                error_code: ErrorCode::ParseError,
274                message: e.to_string(),
275            });
276        }
277    };
278
279    Ok(response)
280}