mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
fix cache
This commit is contained in:
parent
7faee59143
commit
0598a2909d
4 changed files with 27 additions and 12 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -3,3 +3,7 @@
|
|||
flamegraph.svg
|
||||
perf.data
|
||||
perf.data.old
|
||||
|
||||
# created by azalea-auth/examples/auth, defined in the main .gitignore because
|
||||
# the example could be run from anywhere
|
||||
example_cache.json
|
||||
|
|
|
@ -1,7 +1,17 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let auth_result = azalea_auth::auth("example@example.com", azalea_auth::AuthOpts::default())
|
||||
.await
|
||||
.unwrap();
|
||||
let cache_file = PathBuf::from("example_cache.json");
|
||||
|
||||
let auth_result = azalea_auth::auth(
|
||||
"example@example.com",
|
||||
azalea_auth::AuthOpts {
|
||||
cache_file: Some(cache_file),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
println!("{:?}", auth_result);
|
||||
}
|
||||
|
|
|
@ -149,7 +149,7 @@ pub struct XboxLiveAuthResponse {
|
|||
}
|
||||
|
||||
/// Just the important data
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct XboxLiveAuth {
|
||||
token: String,
|
||||
user_hash: String,
|
||||
|
@ -233,6 +233,7 @@ async fn interactive_get_auth_token(
|
|||
.json::<AccessTokenResponse>()
|
||||
.await
|
||||
{
|
||||
println!("access_token_response: {:?}", access_token_response);
|
||||
let expires_at = SystemTime::now()
|
||||
+ std::time::Duration::from_secs(access_token_response.expires_in);
|
||||
return Ok(ExpiringValue {
|
||||
|
|
|
@ -17,7 +17,7 @@ pub enum CacheError {
|
|||
ParseError(serde_json::Error),
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct CachedAccount {
|
||||
pub email: String,
|
||||
/// Microsoft auth
|
||||
|
@ -30,7 +30,7 @@ pub struct CachedAccount {
|
|||
pub profile: crate::auth::ProfileResponse,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct ExpiringValue<T> {
|
||||
/// Seconds since the UNIX epoch
|
||||
pub expires_at: u64,
|
||||
|
@ -43,7 +43,7 @@ impl<T> ExpiringValue<T> {
|
|||
< SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as u64
|
||||
.as_secs() as u64
|
||||
}
|
||||
|
||||
/// Return the data if it's not expired, otherwise return `None`
|
||||
|
@ -59,11 +59,10 @@ impl<T> ExpiringValue<T> {
|
|||
async fn get_entire_cache(cache_file: &Path) -> Result<Vec<CachedAccount>, CacheError> {
|
||||
let mut cache: Vec<CachedAccount> = Vec::new();
|
||||
if cache_file.exists() {
|
||||
let cache_file = File::open(cache_file)
|
||||
let mut cache_file = File::open(cache_file)
|
||||
.await
|
||||
.map_err(CacheError::ReadError)?;
|
||||
// read the file into a string
|
||||
let mut cache_file = tokio::io::BufReader::new(cache_file);
|
||||
let mut contents = String::new();
|
||||
cache_file
|
||||
.read_to_string(&mut contents)
|
||||
|
@ -74,11 +73,12 @@ async fn get_entire_cache(cache_file: &Path) -> Result<Vec<CachedAccount>, Cache
|
|||
Ok(cache)
|
||||
}
|
||||
async fn set_entire_cache(cache_file: &Path, cache: Vec<CachedAccount>) -> Result<(), CacheError> {
|
||||
let cache_file = File::create(cache_file)
|
||||
println!("saving cache: {:?}", cache);
|
||||
|
||||
let mut cache_file = File::create(cache_file)
|
||||
.await
|
||||
.map_err(CacheError::WriteError)?;
|
||||
let mut cache_file = tokio::io::BufWriter::new(cache_file);
|
||||
let cache = serde_json::to_string(&cache).map_err(CacheError::ParseError)?;
|
||||
let cache = serde_json::to_string_pretty(&cache).map_err(CacheError::ParseError)?;
|
||||
cache_file
|
||||
.write_all(cache.as_bytes())
|
||||
.await
|
||||
|
|
Loading…
Add table
Reference in a new issue