use reqwest::Client; use serde_json::json; use std::time::Duration; pub struct Instance { pub host: String, pub tls: bool, } pub struct Session(pub String); impl Instance { pub fn base(&self) -> String { format!( "{}://{}", if self.tls { "https" } else { "http" }, self.host ) } } impl Session { pub fn session_param(&self) -> String { format!("session={}", self.0) } } pub fn stream( instance: &Instance, session: &Session, id: &str, tracks: &[usize], webm: bool, ) -> String { format!( "{}/n/{}/stream?tracks={}&webm={}&{}", instance.base(), id, tracks .iter() .map(|v| format!("{v}")) .collect::>() .join(","), if webm { "1" } else { "0" }, session.session_param() ) } pub async fn login( instance: &Instance, username: String, password: String, expire: Duration, ) -> anyhow::Result { let p = serde_json::to_string(&json!({ "expire": expire.as_secs(), "password": password, "username": username, })) .unwrap(); let r = Client::builder() .build()? .post(format!("{}/api/account/login", instance.base())) .body(p) .send() .await? .json() .await?; Ok(Session(r)) }