library integration
This commit is contained in:
+59
-40
@@ -36,6 +36,11 @@ struct UserDto {
|
||||
id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ViewsResponse {
|
||||
#[serde(rename = "Items", default)]
|
||||
pub items: Vec<JfItem>,
|
||||
}
|
||||
|
||||
impl JellyfinClient {
|
||||
pub fn from_config(cfg: &Config) -> Result<Self, String> {
|
||||
@@ -60,45 +65,67 @@ impl JellyfinClient {
|
||||
}
|
||||
|
||||
pub fn login_by_name(&self, username: &str, password: &str) -> Result<LoginResult, String> {
|
||||
let url = format!("{}/Users/AuthenticateByName", self.base_url);
|
||||
let url = format!("{}/Users/AuthenticateByName", self.base_url);
|
||||
|
||||
let auth_header = format!(
|
||||
"MediaBrowser Client=\"cj-jftui\", Device=\"kitty\", DeviceId=\"cj-jftui\", Version=\"{}\"",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
);
|
||||
let auth_header = format!(
|
||||
"MediaBrowser Client=\"cj-jftui\", Device=\"kitty\", DeviceId=\"cj-jftui\", Version=\"{}\"",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
);
|
||||
|
||||
let body = AuthenticateUserByName { username, pw: password };
|
||||
let body = AuthenticateUserByName { username, pw: password };
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.post(url)
|
||||
.header("X-Emby-Authorization", auth_header)
|
||||
.json(&body)
|
||||
.send()
|
||||
.map_err(|e| format!("request failed: {e}"))?;
|
||||
let resp = self
|
||||
.client
|
||||
.post(url)
|
||||
.header("X-Emby-Authorization", auth_header)
|
||||
.json(&body)
|
||||
.send()
|
||||
.map_err(|e| format!("request failed: {e}"))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
let status = resp.status();
|
||||
let text = resp.text().unwrap_or_default();
|
||||
return Err(format!("login failed: {status} {text}"));
|
||||
if !resp.status().is_success() {
|
||||
let status = resp.status();
|
||||
let text = resp.text().unwrap_or_default();
|
||||
return Err(format!("login failed: {status} {text}"));
|
||||
}
|
||||
|
||||
let parsed: AuthenticationResult = resp
|
||||
.json()
|
||||
.map_err(|e| format!("bad response json: {e}"))?;
|
||||
|
||||
let token = parsed
|
||||
.access_token
|
||||
.ok_or_else(|| "missing AccessToken in response".to_string())?;
|
||||
|
||||
let user_id = parsed.user.and_then(|u| u.id);
|
||||
|
||||
Ok(LoginResult {
|
||||
access_token: token,
|
||||
user_id,
|
||||
})
|
||||
}
|
||||
|
||||
let parsed: AuthenticationResult = resp
|
||||
.json()
|
||||
.map_err(|e| format!("bad response json: {e}"))?;
|
||||
pub fn list_views(&self, user_id: &str) -> Result<Vec<JfItem>, String> {
|
||||
let url = format!("{}/Users/{}/Views", self.base_url, user_id);
|
||||
|
||||
let token = parsed
|
||||
.access_token
|
||||
.ok_or_else(|| "missing AccessToken in response".to_string())?;
|
||||
let resp = self
|
||||
.client
|
||||
.get(url)
|
||||
.header("X-Emby-Authorization", self.auth_header_value())
|
||||
.send()
|
||||
.map_err(|e| format!("Views request failed: {e}"))?;
|
||||
|
||||
let user_id = parsed.user.and_then(|u| u.id);
|
||||
if !resp.status().is_success() {
|
||||
let status = resp.status();
|
||||
let text = resp.text().unwrap_or_default();
|
||||
return Err(format!("Views failed: {status} {text}"));
|
||||
}
|
||||
|
||||
Ok(LoginResult {
|
||||
access_token: token,
|
||||
user_id,
|
||||
})
|
||||
}
|
||||
let parsed: ViewsResponse = resp
|
||||
.json()
|
||||
.map_err(|e| format!("Views parse failed: {e}"))?;
|
||||
|
||||
Ok(parsed.items)
|
||||
}
|
||||
|
||||
fn auth_header_value(&self) -> String {
|
||||
// Identify as a client and include the token.
|
||||
@@ -128,12 +155,14 @@ impl JellyfinClient {
|
||||
}
|
||||
}
|
||||
|
||||
let recursive = if query.recursive { "true" } else { "false" };
|
||||
|
||||
let mut req = self
|
||||
.client
|
||||
.get(url)
|
||||
.header("X-Emby-Authorization", self.auth_header_value())
|
||||
.query(&[
|
||||
("Recursive", "true"),
|
||||
("Recursive", recursive),
|
||||
("StartIndex", &start_index.to_string()),
|
||||
("Limit", &limit.to_string()),
|
||||
("Fields", "PrimaryImageAspectRatio,SortName,ProductionYear,IndexNumber,ParentIndexNumber"),
|
||||
@@ -154,16 +183,6 @@ impl JellyfinClient {
|
||||
req = req.query(&[("SearchTerm", term)]);
|
||||
}
|
||||
|
||||
|
||||
// apply query (movie/series/etc)
|
||||
if let Some(types) = query.include_item_types.as_deref() {
|
||||
req = req.query(&[("IncludeItemTypes", types)]);
|
||||
}
|
||||
|
||||
if let Some(term) = query.search_term.as_deref() {
|
||||
req = req.query(&[("SearchTerm", term)]);
|
||||
}
|
||||
|
||||
let resp = req.send().map_err(|e| format!("request failed: {e}"))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
|
||||
Reference in New Issue
Block a user