Skip to main content

redseat_api/
lib.rs

1//! Rust client for Redseat media servers.
2//!
3//! - [`CloudClient`]: the Redseat cloud: sign-in with a Google ID token, the user's
4//!   servers, server tokens.
5//! - [`google`] (feature `desktop-auth`, not on wasm): Google sign-in in the browser
6//!   for desktop apps.
7//! - [`RedseatClient`]: requests to one server through a [`Transport`], with the server
8//!   token refreshed before it expires, and its events as a typed stream.
9//! - [`ServerApi`] and [`LibraryApi`]: libraries, medias, thumbnails.
10//! - [`http`] (feature `http`, default): the HTTP + SSE transport, and the choice of
11//!   the server address.
12//!
13//! The data types are the server's own, from `rs-plugin-common-interfaces`
14//! ([`types`]), and encrypted libraries use the server's own implementation, from
15//! `redseat-crypto` ([`crypto`]).
16//!
17//! ```no_run
18//! # #[cfg(feature = "http")]
19//! # async fn run(google_id_token: &str) -> redseat_api::Result<()> {
20//! use redseat_api::{CloudClient, RedseatClient, ServerApi};
21//! use redseat_api::types::media::MediaQuery;
22//!
23//! let cloud = CloudClient::http()?;
24//! let session = cloud.sign_in_with_google_id_token(google_id_token).await?;
25//! let servers = cloud.list_servers(&session.token).await?;
26//! let client =
27//!     RedseatClient::connect_http(&servers[0], cloud, &session.token, Default::default())
28//!         .await?;
29//! let server = ServerApi::new(client);
30//! let libraries = server.get_libraries().await?;
31//! let library = server.library(libraries[0].clone());
32//! let query = MediaQuery { limit: Some(10), ..Default::default() };
33//! let page = library.get_medias_page(&query).await?;
34//! let thumb = library.get_thumb(&page.medias[0].item).await?;
35//! # Ok(()) }
36//! ```
37
38#![cfg_attr(
39    not(test),
40    deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)
41)]
42// On wasm, transports and token sources are not `Send` (JS handles): the client still
43// shares them with `Arc`, so the code is the same on every target.
44#![cfg_attr(target_arch = "wasm32", allow(clippy::arc_with_non_send_sync))]
45
46/// Data exchanged with the server: libraries, medias, movies, series, events...
47pub use rs_plugin_common_interfaces::domain as types;
48
49/// Encryption of encrypted libraries, byte-compatible with the server.
50pub use redseat_crypto as crypto;
51
52pub mod client;
53pub mod cloud;
54pub mod direct;
55pub mod encrypted;
56pub mod error;
57pub mod events;
58pub mod library;
59#[cfg(all(feature = "mirror", not(target_arch = "wasm32")))]
60pub mod mirror;
61pub mod server;
62pub mod sse;
63pub mod transport;
64
65#[cfg(feature = "http")]
66pub mod http;
67
68#[cfg(all(feature = "desktop-auth", not(target_arch = "wasm32")))]
69pub mod google;
70
71#[cfg(test)]
72mod test_support;
73
74pub use client::{
75    AuthorizedUrl, ClientOptions, CloudTokenSource, EventOptions, RedseatClient, ServerEventStream,
76    ServerTokenSource,
77};
78pub use cloud::{CloudClient, CloudServer, CloudSession, CloudUser, ServerToken};
79pub use encrypted::EncryptedMediaReader;
80pub use error::{Error, ErrorKind, Result};
81pub use events::ServerEvent;
82pub use library::{LibraryApi, MediaItem, MediaPage};
83#[cfg(all(feature = "mirror", not(target_arch = "wasm32")))]
84pub use mirror::{Mirror, MirrorEvent, MirrorOptions};
85pub use server::ServerApi;
86pub use transport::Transport;
87
88#[cfg(test)]
89mod tests {
90    use super::types::library::{LibraryType, ServerLibrary};
91
92    #[test]
93    fn reads_a_library_as_the_server_sends_it() {
94        let library: ServerLibrary = serde_json::from_value(serde_json::json!({
95            "id": "lib1",
96            "name": "Photos",
97            "source": "PathProvider",
98            "type": "photos",
99            "settings": {}
100        }))
101        .unwrap();
102        assert_eq!(library.kind, LibraryType::Photos);
103    }
104}