Skip to content

Commit

Permalink
feat: Implementing token validation for listenBrainz
Browse files Browse the repository at this point in the history
  • Loading branch information
Losses committed Dec 18, 2024
1 parent 2d367ab commit a2af4eb
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions scrobbling/src/listen_brainz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ impl ListenBrainzClient {
#[async_trait]
impl ScrobblingClient for ListenBrainzClient {
async fn authenticate(&mut self, _username: &str, password: &str) -> Result<()> {
let response = self
.client
.get(format!("{}/1/validate-token", self.base_url))
.header(
"Authorization",
HeaderValue::from_str(&format!("Token {}", password))?,
)
.send()
.await?;

if response.status().is_success() {
let json: Value = response.json().await?;
if json["valid"].as_bool().unwrap_or(false) {
} else {
bail!(
"Token invalid: {:?}",
json["message"].as_str().unwrap_or("Unknown error")
)
}
} else {
bail!("Failed to validate token: {:?}", response.text().await?)
}

self.token = Some(password.to_string());
Ok(())
}
Expand Down

0 comments on commit a2af4eb

Please sign in to comment.