smbcloud_networking/
lib.rs

1pub mod constants;
2pub mod environment;
3pub mod network;
4
5use anyhow::{anyhow, Result};
6use constants::{SMB_CLIENT_ID, SMB_CLIENT_SECRET};
7use environment::Environment;
8use log::debug;
9use std::path::{Path, PathBuf};
10use url_builder::URLBuilder;
11
12pub async fn get_smb_token(env: Environment) -> Result<String> {
13    if let Some(path) = smb_token_file_path(env) {
14        std::fs::read_to_string(path).map_err(|e| {
15            debug!("Error while reading token: {}", &e);
16            anyhow!("Error while reading token. Are you logged in?")
17        })
18    } else {
19        Err(anyhow!("Failed to get home directory."))
20    }
21}
22
23pub fn smb_token_file_path(env: Environment) -> Option<PathBuf> {
24    match home::home_dir() {
25        Some(home_path) => {
26            let token_path = [&env.smb_dir(), "/token"].join("");
27            let token_file = home_path.join(Path::new(&token_path));
28            if token_file.exists() && token_file.is_file() {
29                return Some(token_file);
30            }
31            None
32        }
33        None => {
34            debug!("Failed to get home directory.");
35            None
36        }
37    }
38}
39
40pub fn smb_base_url_builder(env: Environment) -> URLBuilder {
41    let mut url_builder = URLBuilder::new();
42    url_builder
43        .set_protocol(&env.api_protocol())
44        .set_host(&env.api_host())
45        .add_param("client_id", SMB_CLIENT_ID)
46        .add_param("client_secret", SMB_CLIENT_SECRET);
47    url_builder
48}