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 error;
56pub mod events;
57pub mod library;
58pub mod server;
59pub mod sse;
60pub mod transport;
61
62#[cfg(feature = "http")]
63pub mod http;
64
65#[cfg(all(feature = "desktop-auth", not(target_arch = "wasm32")))]
66pub mod google;
67
68#[cfg(test)]
69mod test_support;
70
71pub use client::{
72    ClientOptions, CloudTokenSource, EventOptions, RedseatClient, ServerEventStream,
73    ServerTokenSource,
74};
75pub use cloud::{CloudClient, CloudServer, CloudSession, CloudUser, ServerToken};
76pub use error::{Error, ErrorKind, Result};
77pub use events::ServerEvent;
78pub use library::{LibraryApi, MediaItem, MediaPage};
79pub use server::ServerApi;
80pub use transport::Transport;
81
82#[cfg(test)]
83mod tests {
84    use super::types::library::{LibraryType, ServerLibrary};
85
86    #[test]
87    fn reads_a_library_as_the_server_sends_it() {
88        let library: ServerLibrary = serde_json::from_value(serde_json::json!({
89            "id": "lib1",
90            "name": "Photos",
91            "source": "PathProvider",
92            "type": "photos",
93            "settings": {}
94        }))
95        .unwrap();
96        assert_eq!(library.kind, LibraryType::Photos);
97    }
98}