Use Option<String> instead of String for get_token

This commit is contained in:
Mystikfluu 2023-04-01 17:05:06 +02:00
parent d28913b141
commit eaf04675e3

View File

@ -27,28 +27,35 @@ struct Saved {
_errorcode: u64 _errorcode: u64
} }
fn get_token() -> String { fn get_token() -> Option<String> {
let mut token: String = String::new(); let mut token: String = String::new();
//check if file exists //check if file exists
if !std::path::Path::new(&(get_ipass_folder()+"token.ipasst")).exists() { if !std::path::Path::new(&(get_ipass_folder()+"token.ipasst")).exists() {
return "".to_string(); return None;
} }
File::open(get_ipass_folder()+"token.ipasst").unwrap().read_to_string(&mut token).unwrap(); File::open(get_ipass_folder()+"token.ipasst").unwrap().read_to_string(&mut token).unwrap();
token Some(token)
} }
pub async fn isync_get() -> bool { pub async fn isync_get() -> bool {
let token = get_token(); let token = get_token();
let client = reqwest::Client::builder().https_only(true).build().unwrap(); match token {
let req = client.get("https://ipass.ipost.rocks/saved").header("ipass-auth-token", token).build().unwrap(); Some(token) => {
let res = client.execute(req).await.unwrap(); let client = reqwest::Client::builder().https_only(true).build().unwrap();
let body = res.json::<Saved>().await.unwrap(); let req = client.get("https://ipass.ipost.rocks/saved").header("ipass-auth-token", token).build().unwrap();
if body.success { let res = client.execute(req).await.unwrap();
File::create(get_ipass_folder()+"temp.ipassx").unwrap().write_all(&body.data).unwrap(); let body = res.json::<Saved>().await.unwrap();
import_file(&(get_ipass_folder()+"temp.ipassx")); if body.success {
std::fs::remove_file(get_ipass_folder()+"temp.ipassx").unwrap(); File::create(get_ipass_folder()+"temp.ipassx").unwrap().write_all(&body.data).unwrap();
return true; import_file(&(get_ipass_folder()+"temp.ipassx"));
std::fs::remove_file(get_ipass_folder()+"temp.ipassx").unwrap();
return true;
}
},
None => {
return false;
}
} }
false false
@ -59,15 +66,22 @@ pub async fn isync_save() -> bool {
match possible_data { match possible_data {
Some(data) => { Some(data) => {
let token = get_token(); let token = get_token();
let client = reqwest::Client::builder().https_only(true).build().unwrap(); match token {
let req = client.post("https://ipass.ipost.rocks/saved") Some(token) => {
.header("ipass-auth-token", token) let client = reqwest::Client::builder().https_only(true).build().unwrap();
.body(data) let req = client.post("https://ipass.ipost.rocks/saved")
.build() .header("ipass-auth-token", token)
.unwrap(); .body(data)
let _res = client.execute(req).await.unwrap(); .build()
//sent data .unwrap();
true let _res = client.execute(req).await.unwrap();
//sent data
true
},
None => {
false
}
}
}, },
None => { None => {
false false