1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::{JellyfinAPI, JellyfinSDKError, JellyfinSDKResult};

/// Main structure representing the Jellyfin instance.
///
/// Must be used to interact with Jellyfin server across different ways:
///
/// - API calls
/// - Servers discovery
/// - WebSockets calls
pub struct JellyfinSDK {
    /// Handles interactions with Jellyfin API.
    api: Option<JellyfinAPI>,
    // Discovers new Jellyfin servers.
    // discovery: Option<JellyfinDiscovery>,
    // Handles interactions with Jellyfin via Web sockets.
    // websocket: Option<JellyfinWebSocket>,
    // Holds all client informations
    // client_infos: Option<ClientInfos>,
}

impl JellyfinSDK {
    /// Instantiates a new `JellyfinSDK` instance.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new `JellyfinAPI` instance.
    pub fn create_api<S: Into<String>>(&mut self, base_addr: S, token: Option<&str>) {
        self.api = Some(JellyfinAPI::new(base_addr, token))
    }

    /// Tries getting the [`JellyfinAPI`] instance.
    ///
    /// Returns an [`JellyfinSDKError`] if no [`JellyfinAPI`] has been initialized before calling this method.
    pub fn get_api(&self) -> JellyfinSDKResult<&JellyfinAPI> {
        self.api
            .as_ref()
            .ok_or(JellyfinSDKError::NoJellyfinAPICreated)
    }

    /// Force getting the [`JellyfinAPI`] instance.
    ///
    ///
    /// Must be used carefully.
    /// Panics if no instance has been initialized before calling this method.
    pub fn get_api_unchecked(&self) -> &JellyfinAPI {
        self.api
            .as_ref()
            .expect("Cannot get JellyfinAPI before initializing it.")
    }
}

impl Default for JellyfinSDK {
    fn default() -> Self {
        JellyfinSDK {
            api: None,
            // discovery: None,
            // websocket: None,
            // client_infos: None,
        }
    }
}