redseat-api 0.1.0

Rust client for Redseat media servers
Documentation
//! Server-wide calls. Port of the start of `@redseat/api` `server.ts`.

use crate::client::RedseatClient;
use crate::error::Result;
use crate::library::{LibraryApi, library_from_json};
use crate::types::library::ServerLibrary;

/// Calls on a whole server.
#[derive(Debug, Clone)]
pub struct ServerApi {
    client: RedseatClient,
}

impl ServerApi {
    pub fn new(client: RedseatClient) -> Self {
        ServerApi { client }
    }

    pub fn client(&self) -> &RedseatClient {
        &self.client
    }

    /// The libraries the user can read.
    ///
    /// The server leaves out `settings` and `root` for libraries the user does not
    /// administer: those read as default settings and no root.
    pub async fn get_libraries(&self) -> Result<Vec<ServerLibrary>> {
        let values: Vec<serde_json::Value> = self.client.get_json("/libraries").await?;
        values.into_iter().map(library_from_json).collect()
    }

    /// A [`LibraryApi`] for `library`.
    pub fn library(&self, library: ServerLibrary) -> LibraryApi {
        LibraryApi::new(self.client.clone(), library)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::ClientOptions;
    use crate::cloud::ServerToken;
    use crate::test_support::FakeTransport;
    use crate::types::library::LibraryType;

    #[tokio::test]
    async fn reads_libraries_with_and_without_settings() {
        let fake = FakeTransport::new(|request| {
            assert_eq!(request.path, "/libraries");
            Ok(FakeTransport::json(serde_json::json!([
                {"id": "a", "name": "Photos", "source": "PathProvider", "type": "photos",
                 "crypt": true, "settings": {}, "roles": ["admin"], "passwordProtected": false},
                {"id": "b", "name": "Movies", "source": "PathProvider", "type": "movies",
                 "roles": ["read"], "passwordProtected": false, "hidden": true}
            ])))
        });
        let client = crate::RedseatClient::new(ClientOptions::new(fake));
        client.set_token(ServerToken {
            token: "t".into(),
            expires: i64::MAX,
        });
        let libraries = ServerApi::new(client).get_libraries().await.unwrap();
        assert_eq!(libraries.len(), 2);
        assert_eq!(libraries[0].crypt, Some(true));
        assert_eq!(libraries[1].kind, LibraryType::Movies);
        assert!(libraries[1].hidden);
    }
}