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 and their files, tags, people,
10//!   series, movies, books, channels, watch history, requests and searches through
11//!   plugins.
12//! - [`http`] (feature `http`, default): the HTTP + SSE transport, and the choice of
13//!   the server address.
14//! - [`Call`] and the `calls` modules ([`server::calls`], [`library::calls`],
15//!   [`cloud::calls`]): each call without its I/O, a request and the reader of its
16//!   answer, for a host that sends requests itself.
17//!
18//! The data types are the server's own, from `rs-plugin-common-interfaces`
19//! ([`types`]), and encrypted libraries use the server's own implementation, from
20//! `redseat-crypto` ([`crypto`]).
21//!
22//! ```no_run
23//! # #[cfg(feature = "http")]
24//! # async fn run(google_id_token: &str) -> redseat_api::Result<()> {
25//! use redseat_api::{CloudClient, RedseatClient, ServerApi};
26//! use redseat_api::types::media::MediaQuery;
27//!
28//! let cloud = CloudClient::http()?;
29//! let session = cloud.sign_in_with_google_id_token(google_id_token).await?;
30//! let servers = cloud.list_servers(&session.token).await?;
31//! let client =
32//!     RedseatClient::connect_http(&servers[0], cloud, &session.token, Default::default())
33//!         .await?;
34//! let server = ServerApi::new(client);
35//! let libraries = server.get_libraries().await?;
36//! let library = server.library(libraries[0].clone());
37//! let query = MediaQuery { limit: Some(10), ..Default::default() };
38//! let page = library.get_medias_page(&query).await?;
39//! let thumb = library.get_thumb(&page.medias[0].item).await?;
40//! # Ok(()) }
41//! ```
42
43#![cfg_attr(
44    not(test),
45    deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)
46)]
47// On wasm, transports and token sources are not `Send` (JS handles): the client still
48// shares them with `Arc`, so the code is the same on every target.
49#![cfg_attr(target_arch = "wasm32", allow(clippy::arc_with_non_send_sync))]
50
51/// Data exchanged with the server: libraries, medias, movies, series, events...
52pub use rs_plugin_common_interfaces::domain as types;
53
54/// The crate the data types come from, for those outside [`types`]:
55/// `request::RsRequest` and `request::RsGroupDownload`, `MediaType`, `url::RsLink`...
56pub use rs_plugin_common_interfaces;
57
58/// Encryption of encrypted libraries, byte-compatible with the server.
59pub use redseat_crypto as crypto;
60
61pub mod call;
62pub mod client;
63pub mod cloud;
64pub mod direct;
65pub mod encrypted;
66pub mod error;
67pub mod events;
68pub mod library;
69#[cfg(feature = "mirror")]
70pub mod mirror;
71pub mod server;
72pub mod sse;
73pub mod transport;
74
75#[cfg(feature = "http")]
76pub mod http;
77
78#[cfg(all(feature = "desktop-auth", not(target_arch = "wasm32")))]
79pub mod google;
80
81#[cfg(test)]
82mod test_support;
83
84pub use call::Call;
85pub use client::{
86    AuthorizedUrl, ClientOptions, CloudTokenSource, EventOptions, RedseatClient, ServerEventStream,
87    ServerTokenSource,
88};
89pub use cloud::{CloudClient, CloudServer, CloudSession, CloudUser, ServerToken};
90pub use encrypted::EncryptedMediaReader;
91pub use error::{Error, ErrorKind, Result};
92pub use events::ServerEvent;
93pub use library::{LibraryApi, MediaItem, MediaPage, SearchStream};
94#[cfg(feature = "mirror")]
95pub use mirror::{Mirror, MirrorEvent, MirrorOptions};
96pub use server::ServerApi;
97pub use transport::Transport;
98
99#[cfg(test)]
100mod tests {
101    use super::types::library::{LibraryType, ServerLibrary};
102
103    #[test]
104    fn reads_a_library_as_the_server_sends_it() {
105        let library: ServerLibrary = serde_json::from_value(serde_json::json!({
106            "id": "lib1",
107            "name": "Photos",
108            "source": "PathProvider",
109            "type": "photos",
110            "settings": {}
111        }))
112        .unwrap();
113        assert_eq!(library.kind, LibraryType::Photos);
114    }
115}