feat: Update to latest iced
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
pub mod jellyfin;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use ::tap::*;
|
||||
use reqwest::Method;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -12,6 +14,8 @@ pub enum JellyfinApiError {
|
||||
SerdeError(#[from] serde_json::Error),
|
||||
#[error("IO error: {0}")]
|
||||
IoError(#[from] std::io::Error),
|
||||
#[error("Unknown Jellyfin API error")]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
type Result<T, E = JellyfinApiError> = std::result::Result<T, E>;
|
||||
@@ -19,8 +23,8 @@ type Result<T, E = JellyfinApiError> = std::result::Result<T, E>;
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct JellyfinClient {
|
||||
client: reqwest::Client,
|
||||
access_token: Option<String>,
|
||||
config: JellyfinConfig,
|
||||
access_token: Option<Arc<str>>,
|
||||
config: Arc<JellyfinConfig>,
|
||||
}
|
||||
|
||||
impl JellyfinClient {
|
||||
@@ -28,13 +32,13 @@ impl JellyfinClient {
|
||||
JellyfinClient {
|
||||
client: reqwest::Client::new(),
|
||||
access_token: None,
|
||||
config,
|
||||
config: Arc::new(config),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn save_token(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
|
||||
if let Some(token) = &self.access_token {
|
||||
tokio::fs::write(path, token).await
|
||||
tokio::fs::write(path, &**token).await
|
||||
} else {
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
@@ -43,10 +47,17 @@ impl JellyfinClient {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn load_token(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
|
||||
pub async fn load_token(
|
||||
&mut self,
|
||||
path: impl AsRef<std::path::Path>,
|
||||
) -> std::io::Result<String> {
|
||||
let token = tokio::fs::read_to_string(path).await?;
|
||||
self.access_token = Some(token);
|
||||
Ok(())
|
||||
self.access_token = Some(token.clone().into());
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
pub async fn set_token(&mut self, token: impl AsRef<str>) {
|
||||
self.access_token = Some(token.as_ref().into());
|
||||
}
|
||||
|
||||
pub fn request_builder(
|
||||
@@ -59,7 +70,7 @@ impl JellyfinClient {
|
||||
.header("X-Emby-Authorization", format!("MediaBrowser Client=\"Jello\", Device=\"Jello\", DeviceId=\"{}\", Version=\"1.0.0\"", self.config.device_id))
|
||||
.pipe(|builder| {
|
||||
if let Some(token) = &self.access_token {
|
||||
builder.header("X-MediaBrowser-Token", token)
|
||||
builder.header("X-MediaBrowser-Token", &**token)
|
||||
} else {
|
||||
builder
|
||||
}
|
||||
@@ -118,20 +129,32 @@ impl JellyfinClient {
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
self.access_token = auth_result.access_token.clone();
|
||||
self.access_token = auth_result.access_token.clone().map(Into::into);
|
||||
Ok(auth_result)
|
||||
}
|
||||
|
||||
pub async fn authenticate_with_cached_token(
|
||||
&mut self,
|
||||
path: impl AsRef<std::path::Path>,
|
||||
) -> Result<()> {
|
||||
) -> Result<String> {
|
||||
let path = path.as_ref();
|
||||
if !self.load_token(path).await.is_ok() {
|
||||
if let Ok(token) = self
|
||||
.load_token(path)
|
||||
.await
|
||||
.inspect_err(|err| tracing::warn!("Failed to load cached token: {}", err))
|
||||
{
|
||||
self.authenticate().await?;
|
||||
self.save_token(path).await?;
|
||||
Ok(token)
|
||||
} else {
|
||||
let token = self
|
||||
.authenticate()
|
||||
.await?
|
||||
.access_token
|
||||
.ok_or_else(|| JellyfinApiError::Unknown)?;
|
||||
self.save_token(path).await?;
|
||||
Ok(token)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn raw_items(&self) -> Result<jellyfin::BaseItemDtoQueryResult> {
|
||||
@@ -148,7 +171,7 @@ impl JellyfinClient {
|
||||
|
||||
pub async fn items(
|
||||
&self,
|
||||
root: impl Into<Option<String>>,
|
||||
root: impl Into<Option<uuid::Uuid>>,
|
||||
) -> Result<Vec<jellyfin::BaseItemDto>> {
|
||||
let text = &self
|
||||
.request_builder(Method::GET, "Items")
|
||||
|
||||
Reference in New Issue
Block a user