99 lines
3.0 KiB
Rust
99 lines
3.0 KiB
Rust
//! Jellyfin library refresh after import. See DESIGN.md §7.5.
|
|
//!
|
|
//! Jellyfin's own filesystem watcher misses the hardlinked file, so import
|
|
//! asks it to rescan directly. A refresh failure must not fail the import —
|
|
//! the caller logs and continues (§7.5).
|
|
|
|
use std::time::Duration;
|
|
|
|
use reqwest::{Client, StatusCode};
|
|
|
|
const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum Error {
|
|
#[error("request: {0}")]
|
|
Request(#[from] reqwest::Error),
|
|
#[error("jellyfin returned {0}")]
|
|
Status(StatusCode),
|
|
}
|
|
|
|
/// A client for the one Jellyfin call this app makes.
|
|
#[derive(Debug, Clone)]
|
|
pub struct JellyfinClient {
|
|
client: Client,
|
|
base_url: String,
|
|
api_key: Option<String>,
|
|
}
|
|
|
|
impl JellyfinClient {
|
|
pub fn new(base_url: impl Into<String>, api_key: Option<String>) -> Result<Self, Error> {
|
|
let client = Client::builder().timeout(REQUEST_TIMEOUT).build()?;
|
|
Ok(Self {
|
|
client,
|
|
base_url: base_url.into(),
|
|
api_key,
|
|
})
|
|
}
|
|
|
|
/// Trigger a full library scan. Jellyfin exposes no per-library refresh
|
|
/// without knowing that library's ID, which this app never learns.
|
|
pub async fn refresh(&self) -> Result<(), Error> {
|
|
let url = format!("{}/Library/Refresh", self.base_url.trim_end_matches('/'));
|
|
let mut request = self.client.post(url);
|
|
if let Some(key) = &self.api_key {
|
|
request = request.header("X-Emby-Token", key);
|
|
}
|
|
let response = request.send().await?;
|
|
if !response.status().is_success() {
|
|
return Err(Error::Status(response.status()));
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[allow(clippy::unwrap_used)]
|
|
mod tests {
|
|
use wiremock::matchers::{header, method, path};
|
|
use wiremock::{Mock, MockServer, ResponseTemplate};
|
|
|
|
use super::*;
|
|
|
|
#[tokio::test]
|
|
async fn refresh_posts_to_the_library_refresh_endpoint_with_the_api_key() {
|
|
let server = MockServer::start().await;
|
|
Mock::given(method("POST"))
|
|
.and(path("/Library/Refresh"))
|
|
.and(header("X-Emby-Token", "secret"))
|
|
.respond_with(ResponseTemplate::new(204))
|
|
.mount(&server)
|
|
.await;
|
|
|
|
let client = JellyfinClient::new(server.uri(), Some("secret".to_string())).unwrap();
|
|
|
|
client.refresh().await.unwrap();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_non_success_status_is_an_error() {
|
|
let server = MockServer::start().await;
|
|
Mock::given(method("POST"))
|
|
.and(path("/Library/Refresh"))
|
|
.respond_with(ResponseTemplate::new(500))
|
|
.mount(&server)
|
|
.await;
|
|
|
|
let client = JellyfinClient::new(server.uri(), None).unwrap();
|
|
|
|
assert!(matches!(client.refresh().await, Err(Error::Status(_))));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_unreachable_jellyfin_is_an_error_the_caller_can_swallow() {
|
|
let client = JellyfinClient::new("http://127.0.0.1:1", None).unwrap();
|
|
|
|
assert!(matches!(client.refresh().await, Err(Error::Request(_))));
|
|
}
|
|
}
|