smbcloud_networking/
lib.rs

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