use std::time::Duration;
use crate::client::RedseatClient;
use crate::error::Result;
use crate::library::LibraryApi;
use crate::transport::Request;
use crate::types::library::ServerLibraryForRead;
use crate::types::watched::Watched;
#[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
}
pub async fn get_libraries(&self) -> Result<Vec<ServerLibraryForRead>> {
self.client.get_json("/libraries").await
}
pub async fn ping(&self, timeout: Option<Duration>) -> Result<()> {
let mut request = Request::get("/ping");
request.timeout = timeout;
self.client.request(request).await?;
Ok(())
}
pub async fn get_history(&self) -> Result<Vec<Watched>> {
self.client.get_json("/users/me/history").await
}
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(())
}
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 pings_and_reads_the_history() {
let fake = FakeTransport::new(|request| {
Ok(FakeTransport::json(match request.path.as_str() {
"/ping" => serde_json::json!({"result": {"success": true}}),
"/users/me/history" => serde_json::json!([
{"type": "movie", "id": "movie:imdb/tt0078748", "userRef": "u1",
"date": 1700, "modified": 1800}
]),
other => panic!("unexpected {other}"),
}))
});
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.ping(None).await.unwrap();
server.ping(Some(Duration::from_secs(2))).await.unwrap();
let history = server.get_history().await.unwrap();
assert_eq!(
history[0].kind,
rs_plugin_common_interfaces::MediaType::Movie
);
assert_eq!(history[0].id, "movie:imdb/tt0078748");
assert_eq!(history[0].date, 1700);
let requests = fake.requests();
assert_eq!(requests[0].method, Method::Get);
assert_eq!(requests[0].path, "/ping");
assert_eq!(requests[0].timeout, None);
assert_eq!(requests[1].timeout, Some(Duration::from_secs(2)));
assert_eq!(requests[2].method, Method::Get);
assert_eq!(requests[2].path, "/users/me/history");
assert!(requests[2].query.is_empty());
assert_eq!(requests[2].body, None);
}
#[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);
}
}