jellyfin_sdk_rust/
jellyfin_sdk.rs

1use crate::{JellyfinAPI, JellyfinSDKError, JellyfinSDKResult};
2
3/// Main structure representing the Jellyfin instance.
4///
5/// Must be used to interact with Jellyfin server across different ways:
6///
7/// - API calls
8/// - Servers discovery
9/// - WebSockets calls
10pub struct JellyfinSDK {
11    /// Handles interactions with Jellyfin API.
12    api: Option<JellyfinAPI>,
13    // Discovers new Jellyfin servers.
14    // discovery: Option<JellyfinDiscovery>,
15    // Handles interactions with Jellyfin via Web sockets.
16    // websocket: Option<JellyfinWebSocket>,
17    // Holds all client informations
18    // client_infos: Option<ClientInfos>,
19}
20
21impl JellyfinSDK {
22    /// Instantiates a new `JellyfinSDK` instance.
23    pub fn new() -> Self {
24        Self::default()
25    }
26
27    /// Creates a new `JellyfinAPI` instance.
28    pub fn create_api<S: Into<String>>(&mut self, base_addr: S, token: Option<&str>) {
29        self.api = Some(JellyfinAPI::new(base_addr, token))
30    }
31
32    /// Tries getting the [`JellyfinAPI`] instance.
33    ///
34    /// Returns an [`JellyfinSDKError`] if no [`JellyfinAPI`] has been initialized before calling this method.
35    pub fn get_api(&self) -> JellyfinSDKResult<&JellyfinAPI> {
36        self.api
37            .as_ref()
38            .ok_or(JellyfinSDKError::NoJellyfinAPICreated)
39    }
40
41    /// Force getting the [`JellyfinAPI`] instance.
42    ///
43    ///
44    /// Must be used carefully.
45    /// Panics if no instance has been initialized before calling this method.
46    pub fn get_api_unchecked(&self) -> &JellyfinAPI {
47        self.api
48            .as_ref()
49            .expect("Cannot get JellyfinAPI before initializing it.")
50    }
51}
52
53impl Default for JellyfinSDK {
54    fn default() -> Self {
55        JellyfinSDK {
56            api: None,
57            // discovery: None,
58            // websocket: None,
59            // client_infos: None,
60        }
61    }
62}