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
61#[cfg(feature = "app")]
62pub mod app;
63pub mod call;
64pub mod client;
65pub mod cloud;
66pub mod direct;
67pub mod encrypted;
68pub mod error;
69pub mod events;
70pub mod library;
71#[cfg(feature = "mirror")]
72pub mod mirror;
73pub mod server;
74pub mod sse;
75pub mod transport;
76
77#[cfg(feature = "http")]
78pub mod http;
79
80#[cfg(all(feature = "desktop-auth", not(target_arch = "wasm32")))]
81pub mod google;
82
83#[cfg(test)]
84mod test_support;
85
86pub use call::Call;
87pub use client::{
88    AuthorizedUrl, ClientOptions, CloudTokenSource, EventOptions, RedseatClient, ServerEventStream,
89    ServerTokenSource, set_clock,
90};
91pub use cloud::{CloudClient, CloudServer, CloudSession, CloudUser, ServerToken};
92pub use encrypted::EncryptedMediaReader;
93pub use error::{Error, ErrorKind, Result};
94pub use events::ServerEvent;
95pub use library::{LibraryApi, MediaItem, MediaPage, SearchStream};
96#[cfg(feature = "mirror")]
97pub use mirror::{Mirror, MirrorEvent, MirrorOptions};
98pub use server::ServerApi;
99pub use transport::Transport;
100
101#[cfg(test)]
102mod tests {
103    use super::types::library::{LibraryType, ServerLibrary};
104
105    #[test]
106    fn reads_a_library_as_the_server_sends_it() {
107        let library: ServerLibrary = serde_json::from_value(serde_json::json!({
108            "id": "lib1",
109            "name": "Photos",
110            "source": "PathProvider",
111            "type": "photos",
112            "settings": {}
113        }))
114        .unwrap();
115        assert_eq!(library.kind, LibraryType::Photos);
116    }
117}