feat(readarr): toggle Readarr author monitoring and expose the command on the CLI
This commit is contained in:
6 files changed
+221
-2
No files matched your search
@@ -1,5 +1,5 @@
|
||||
use anyhow::Result;
|
||||
use log::{debug, info};
|
||||
use log::{debug, info, warn};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::models::Route;
|
||||
@@ -277,4 +277,67 @@ impl Network<'_, '_> {
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub(in crate::network::readarr_network) async fn toggle_author_monitoring(
|
||||
&mut self,
|
||||
author_id: i64,
|
||||
) -> Result<()> {
|
||||
let event = ReadarrEvent::ToggleAuthorMonitoring(author_id);
|
||||
|
||||
let detail_event = ReadarrEvent::GetAuthorDetails(author_id);
|
||||
info!("Toggling author monitoring for author with ID: {author_id}");
|
||||
info!("Fetching author details for author with ID: {author_id}");
|
||||
|
||||
let request_props = self
|
||||
.request_props_from(
|
||||
detail_event,
|
||||
RequestMethod::Get,
|
||||
None::<()>,
|
||||
Some(format!("/{author_id}")),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut response = String::new();
|
||||
|
||||
self
|
||||
.handle_request::<(), Value>(request_props, |detailed_author_body, _| {
|
||||
response = detailed_author_body.to_string()
|
||||
})
|
||||
.await?;
|
||||
|
||||
info!("Constructing toggle author monitoring body");
|
||||
|
||||
match serde_json::from_str::<Value>(&response) {
|
||||
Ok(mut detailed_author_body) => {
|
||||
let monitored = detailed_author_body
|
||||
.get("monitored")
|
||||
.unwrap()
|
||||
.as_bool()
|
||||
.unwrap();
|
||||
|
||||
*detailed_author_body.get_mut("monitored").unwrap() = json!(!monitored);
|
||||
|
||||
debug!("Toggle author monitoring body: {detailed_author_body:?}");
|
||||
|
||||
let request_props = self
|
||||
.request_props_from(
|
||||
event,
|
||||
RequestMethod::Put,
|
||||
Some(detailed_author_body),
|
||||
Some(format!("/{author_id}")),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
self
|
||||
.handle_request::<Value, ()>(request_props, |_, _| ())
|
||||
.await
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("Request for detailed author body was interrupted");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -680,4 +680,81 @@ mod tests {
|
||||
mock.assert_async().await;
|
||||
assert_err!(result);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(true, false)]
|
||||
#[case(false, true)]
|
||||
#[tokio::test]
|
||||
async fn test_handle_toggle_author_monitoring_event(
|
||||
#[case] initial_monitored: bool,
|
||||
#[case] expected_monitored: bool,
|
||||
) {
|
||||
let mut author_json: Value = serde_json::from_str(AUTHOR_JSON).unwrap();
|
||||
*author_json.get_mut("monitored").unwrap() = json!(initial_monitored);
|
||||
let mut expected_body = author_json.clone();
|
||||
*expected_body.get_mut("monitored").unwrap() = json!(expected_monitored);
|
||||
let (async_details_server, app, mut server) = MockServarrApi::get()
|
||||
.returns(author_json)
|
||||
.path("/1")
|
||||
.build_for(ReadarrEvent::GetAuthorDetails(1))
|
||||
.await;
|
||||
let async_toggle_server = server
|
||||
.mock(
|
||||
"PUT",
|
||||
format!(
|
||||
"/api/v1{}/1",
|
||||
ReadarrEvent::ToggleAuthorMonitoring(1).resource()
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.with_status(202)
|
||||
.match_header("X-Api-Key", "test1234")
|
||||
.match_body(Matcher::Json(expected_body))
|
||||
.create_async()
|
||||
.await;
|
||||
app.lock().await.server_tabs.set_index(3);
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::ToggleAuthorMonitoring(1))
|
||||
.await;
|
||||
|
||||
async_details_server.assert_async().await;
|
||||
async_toggle_server.assert_async().await;
|
||||
assert_ok!(result);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_toggle_author_monitoring_event_failure() {
|
||||
let (async_details_server, app, mut server) = MockServarrApi::get()
|
||||
.returns(serde_json::from_str(AUTHOR_JSON).unwrap())
|
||||
.status(404)
|
||||
.path("/1")
|
||||
.build_for(ReadarrEvent::GetAuthorDetails(1))
|
||||
.await;
|
||||
let async_toggle_server = server
|
||||
.mock(
|
||||
"PUT",
|
||||
format!(
|
||||
"/api/v1{}/1",
|
||||
ReadarrEvent::ToggleAuthorMonitoring(1).resource()
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.with_status(202)
|
||||
.match_header("X-Api-Key", "test1234")
|
||||
.expect(0)
|
||||
.create_async()
|
||||
.await;
|
||||
app.lock().await.server_tabs.set_index(3);
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::ToggleAuthorMonitoring(1))
|
||||
.await;
|
||||
|
||||
async_details_server.assert_async().await;
|
||||
async_toggle_server.assert_async().await;
|
||||
assert_err!(result);
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ pub enum ReadarrEvent {
|
||||
ListAuthors,
|
||||
SearchNewAuthor(String),
|
||||
StartTask(ReadarrTaskName),
|
||||
ToggleAuthorMonitoring(i64),
|
||||
}
|
||||
|
||||
impl NetworkResource for ReadarrEvent {
|
||||
@@ -57,7 +58,8 @@ impl NetworkResource for ReadarrEvent {
|
||||
| ReadarrEvent::DeleteAuthor(_)
|
||||
| ReadarrEvent::EditAuthor(_)
|
||||
| ReadarrEvent::GetAuthorDetails(_)
|
||||
| ReadarrEvent::ListAuthors => "/author",
|
||||
| ReadarrEvent::ListAuthors
|
||||
| ReadarrEvent::ToggleAuthorMonitoring(_) => "/author",
|
||||
ReadarrEvent::SearchNewAuthor(_) => "/author/lookup",
|
||||
ReadarrEvent::GetQueuedEvents | ReadarrEvent::StartTask(_) => "/command",
|
||||
ReadarrEvent::GetHostConfig | ReadarrEvent::GetSecurityConfig => "/config/host",
|
||||
@@ -163,6 +165,10 @@ impl Network<'_, '_> {
|
||||
.start_readarr_task(task_name)
|
||||
.await
|
||||
.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::ToggleAuthorMonitoring(author_id) => self
|
||||
.toggle_author_monitoring(author_id)
|
||||
.await
|
||||
.map(ReadarrSerdeable::from),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ mod tests {
|
||||
#[case(ReadarrEvent::EditAuthor(EditAuthorParams::default()), "/author")]
|
||||
#[case(ReadarrEvent::GetAuthorDetails(1), "/author")]
|
||||
#[case(ReadarrEvent::ListAuthors, "/author")]
|
||||
#[case(ReadarrEvent::ToggleAuthorMonitoring(1), "/author")]
|
||||
#[case(ReadarrEvent::SearchNewAuthor(String::new()), "/author/lookup")]
|
||||
#[case(ReadarrEvent::GetQueuedEvents, "/command")]
|
||||
#[case(ReadarrEvent::StartTask(Default::default()), "/command")]
|
||||
|
||||
Reference in new issue
Block a user