aboutsummaryrefslogtreecommitdiff
path: root/client/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/lib.rs')
-rw-r--r--client/src/lib.rs128
1 files changed, 82 insertions, 46 deletions
diff --git a/client/src/lib.rs b/client/src/lib.rs
index a4d0b01..4b111d1 100644
--- a/client/src/lib.rs
+++ b/client/src/lib.rs
@@ -1,14 +1,22 @@
-use reqwest::Client;
+use anyhow::Result;
+use jellycommon::Node;
+use reqwest::{
+ header::{HeaderMap, HeaderValue},
+ Client,
+};
use serde_json::json;
use std::time::Duration;
+#[derive(Debug, Clone)]
pub struct Instance {
pub host: String,
pub tls: bool,
}
-pub struct Session(pub String);
impl Instance {
+ pub fn new(host: String, tls: bool) -> Self {
+ Self { host, tls }
+ }
pub fn base(&self) -> String {
format!(
"{}://{}",
@@ -16,55 +24,83 @@ impl Instance {
self.host
)
}
-}
-impl Session {
- pub fn session_param(&self) -> String {
- format!("session={}", self.0)
+ pub async fn login(
+ self,
+ username: String,
+ password: String,
+ expire: Duration,
+ ) -> anyhow::Result<Session> {
+ let session_token = Client::builder()
+ .build()?
+ .post(format!("{}/api/create_session", self.base()))
+ .json(&json!({
+ "expire": expire.as_secs(),
+ "password": password,
+ "username": username,
+ }))
+ .send()
+ .await?
+ .json()
+ .await?;
+
+ let mut headers = HeaderMap::new();
+ headers.insert(
+ "Cookie",
+ HeaderValue::from_str(&format!("session={session_token}")).unwrap(),
+ );
+ headers.insert("Accept", HeaderValue::from_static("application/json"));
+
+ Ok(Session {
+ instance: self,
+ session_token,
+ client: Client::builder().default_headers(headers).build().unwrap(),
+ })
}
}
-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 struct Session {
+ client: Client,
+ instance: Instance,
+ session_token: String,
}
-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();
+impl Session {
+ fn session_param(&self) -> String {
+ format!("session={}", self.session_token)
+ }
+
+ pub async fn node(&self, id: &str) -> Result<Node> {
+ Ok(self
+ .client
+ .get(format!("{}/n/{id}", self.instance.base()))
+ .send()
+ .await?
+ .json()
+ .await?)
+ }
- let r = Client::builder()
- .build()?
- .post(format!("{}/api/account/login", instance.base()))
- .body(p)
- .send()
- .await?
- .json()
- .await?;
+ // pub async fn node_asset(&self, id: &str, role: AssetRole) -> Result<Node> {
+ // Ok(self
+ // .client
+ // .get(format!("/n/{id}"))
+ // .send()
+ // .await?
+ // .bytes()
+ // .await?)
+ // }
- Ok(Session(r))
+ pub fn stream(&self, id: &str, tracks: &[usize], webm: bool) -> String {
+ format!(
+ "{}/n/{}/stream?tracks={}&webm={}&{}",
+ self.instance.base(),
+ id,
+ tracks
+ .iter()
+ .map(|v| format!("{v}"))
+ .collect::<Vec<_>>()
+ .join(","),
+ if webm { "1" } else { "0" },
+ self.session_param()
+ )
+ }
}