smbcloud_networking/
lib.rs

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