diff options
author | metamuffin <metamuffin@disroot.org> | 2023-08-02 01:03:16 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-08-02 01:03:16 +0200 |
commit | 14e01792e33631d134fe895018d3bef5ea74a958 (patch) | |
tree | 5c4f0105140541cd479a58c1abb598caaee2d94b /client/src | |
parent | 6eafafe5297c54aa5cb38790c45ba189b47d755e (diff) | |
download | jellything-14e01792e33631d134fe895018d3bef5ea74a958.tar jellything-14e01792e33631d134fe895018d3bef5ea74a958.tar.bz2 jellything-14e01792e33631d134fe895018d3bef5ea74a958.tar.zst |
move client code to its own crate
Diffstat (limited to 'client/src')
-rw-r--r-- | client/src/lib.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/client/src/lib.rs b/client/src/lib.rs new file mode 100644 index 0000000..a4d0b01 --- /dev/null +++ b/client/src/lib.rs @@ -0,0 +1,70 @@ +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::<Vec<_>>() + .join(","), + if webm { "1" } else { "0" }, + session.session_param() + ) +} + +pub async fn login( + instance: &Instance, + username: String, + password: String, + expire: Duration, +) -> anyhow::Result<Session> { + 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)) +} |