redseat-api 0.4.2

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;
use crate::transport::Request;
use crate::types::library::ServerLibraryForRead;

/// 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, as the user sees them: the server leaves out
    /// `settings` and `root` for libraries the user does not administer.
    pub async fn get_libraries(&self) -> Result<Vec<ServerLibraryForRead>> {
        self.client.get_json("/libraries").await
    }

    /// Hides `library` from the user's library lists (`hidden`), or shows it again: a
    /// preference of the user, kept by the server. The library, and what its other
    /// users see, do not change; [`ServerLibraryForRead::hidden`] says it in later
    /// lists.
    pub async fn set_library_hidden(&self, library: &str, hidden: bool) -> Result<()> {
        let path = if hidden {
            "/users/me/hidelibrary"
        } else {
            "/users/me/unhidelibrary"
        };
        let request = Request::post(path).json(serde_json::json!({ "library": library }));
        self.client.request(request).await?;
        Ok(())
    }

    /// A [`LibraryApi`] for `library`.
    pub fn library(&self, library: ServerLibraryForRead) -> 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::transport::{Body, Method};
    use crate::types::library::LibraryType;

    #[tokio::test]
    async fn hides_and_shows_a_library_again() {
        let fake = FakeTransport::new(|_| Ok(FakeTransport::json(serde_json::json!({"ok": true}))));
        let client = crate::RedseatClient::new(ClientOptions::new(fake.clone()));
        client.set_token(ServerToken {
            token: "t".into(),
            expires: i64::MAX,
        });
        let server = ServerApi::new(client);
        server.set_library_hidden("lib1", true).await.unwrap();
        server.set_library_hidden("lib1", false).await.unwrap();

        let requests = fake.requests();
        let body = Some(Body::Json(serde_json::json!({"library": "lib1"})));
        assert_eq!(requests[0].method, Method::Post);
        assert_eq!(requests[0].path, "/users/me/hidelibrary");
        assert_eq!(requests[0].body, body);
        assert_eq!(requests[1].method, Method::Post);
        assert_eq!(requests[1].path, "/users/me/unhidelibrary");
        assert_eq!(requests[1].body, body);
    }

    #[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!(libraries[0].settings.is_some());
        assert!(libraries[1].settings.is_none());
        assert_eq!(libraries[1].kind, LibraryType::Movies);
        assert!(libraries[1].hidden);
    }
}