//! The Scropple HTTP backend contract (final_track, ircd's ScrobbleServ,
//! scropple-android's `HttpBackend`): `POST {url}/api/v1/now-playing` with
//! one play, `POST {url}/api/v1/scrobble` with an array, bearer token.
use serde::Serialize;
use super::{Play, SendError, classify, http_client};
pub struct HttpBackend {
pub name: String,
base: String,
token: String,
client: reqwest::Client,
}
impl HttpBackend {
pub fn new(name: &str, base: &str, token: &str) -> Self {
Self {
name: name.to_string(),
base: base.trim_end_matches('/').to_string(),
token: token.to_string(),
client: http_client(),
}
}
pub fn has_token(&self) -> bool {
!self.token.is_empty()
}
pub async fn now_playing(&self, play: &Play) -> Result<(), SendError> {
self.post("/api/v1/now-playing", play).await
}
pub async fn scrobble(&self, plays: &[Play]) -> Result<(), SendError> {
self.post("/api/v1/scrobble", plays).await
}
async fn post<B: Serialize + ?Sized>(&self, path: &str, body: &B) -> Result<(), SendError> {
let resp = self
.client
.post(format!("{}{}", self.base, path))
.bearer_auth(&self.token)
.json(body)
.send()
.await
.map_err(|e| SendError::Retry(e.to_string()))?;
classify(resp.status().as_u16(), || {
format!("HTTP {} from {}{}", resp.status(), self.base, path)
})
}
}
neiam
/scropple-linux
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
1.5 KiB · text
History
0463cca