initial commit

This commit is contained in:
DaXcess committed 2022-10-18 22:59:32 +02:00
commit 6a77189343
28 files changed
+5184

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
use std::time::{SystemTime, UNIX_EPOCH};
pub mod spotify;
pub fn get_time() -> u64 {
let now = SystemTime::now();
let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
since_the_epoch.as_secs()
}
+36
View File
@@ -0,0 +1,36 @@
use log::{error, trace};
use serde_json::Value;
pub async fn get_username(token: impl Into<String>) -> Result<String, String> {
let token = token.into();
let client = reqwest::Client::new();
let response = match client
.get("https://api.spotify.com/v1/me")
.bearer_auth(token)
.send()
.await
{
Ok(response) => response,
Err(why) => {
error!("Failed to get username: {}", why);
return Err(format!("{}", why));
}
};
let body: Value = match response.json().await {
Ok(body) => body,
Err(why) => {
error!("Failed to parse body: {}", why);
return Err(format!("{}", why));
}
};
if let Value::String(username) = &body["id"] {
trace!("Got username: {}", username);
return Ok(username.clone());
}
error!("Missing 'id' field in body");
Err("Failed to parse body: Invalid body received".to_string())
}