feat(readarr): refresh all Readarr authors and expose the command on the CLI
Posts the RefreshAuthor command to the command endpoint with no author ID, which is how Readarr distinguishes a library wide refresh from a single author refresh. The response is returned verbatim and no app state is mutated.
This commit is contained in:
6 files changed
+101
No files matched your search
@@ -18,6 +18,8 @@ mod refresh_command_handler_tests;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Subcommand)]
|
||||
pub enum ReadarrRefreshCommand {
|
||||
#[command(about = "Refresh all author data for all authors in your Readarr library")]
|
||||
AllAuthors,
|
||||
#[command(about = "Refresh author data and scan disk for the author with the given ID")]
|
||||
Author {
|
||||
#[arg(
|
||||
@@ -58,6 +60,13 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, ReadarrRefreshCommand>
|
||||
|
||||
async fn handle(self) -> Result<String> {
|
||||
let result = match self.command {
|
||||
ReadarrRefreshCommand::AllAuthors => {
|
||||
let resp = self
|
||||
.network
|
||||
.handle_network_event(ReadarrEvent::UpdateAllAuthors.into())
|
||||
.await?;
|
||||
serde_json::to_string_pretty(&resp)?
|
||||
}
|
||||
ReadarrRefreshCommand::Author { author_id } => {
|
||||
let resp = self
|
||||
.network
|
||||
|
||||
@@ -24,6 +24,14 @@ mod tests {
|
||||
use clap::error::ErrorKind;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_refresh_all_authors_has_no_arg_requirements() {
|
||||
let result =
|
||||
Cli::command().try_get_matches_from(["managarr", "readarr", "refresh", "all-authors"]);
|
||||
|
||||
assert_ok!(&result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_refresh_author_requires_author_id() {
|
||||
let result =
|
||||
@@ -75,6 +83,28 @@ mod tests {
|
||||
network::{MockNetworkTrait, NetworkEvent, readarr_network::ReadarrEvent},
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_refresh_all_authors_command() {
|
||||
let mut mock_network = MockNetworkTrait::new();
|
||||
mock_network
|
||||
.expect_handle_network_event()
|
||||
.with(eq::<NetworkEvent>(ReadarrEvent::UpdateAllAuthors.into()))
|
||||
.times(1)
|
||||
.returning(|_| {
|
||||
Ok(Serdeable::Readarr(ReadarrSerdeable::Value(
|
||||
json!({"testResponse": "response"}),
|
||||
)))
|
||||
});
|
||||
let app_arc = Arc::new(Mutex::new(App::test_default()));
|
||||
let refresh_command = ReadarrRefreshCommand::AllAuthors;
|
||||
|
||||
let result = ReadarrRefreshCommandHandler::with(&app_arc, refresh_command, &mut mock_network)
|
||||
.handle()
|
||||
.await;
|
||||
|
||||
assert_ok!(&result);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_refresh_author_command() {
|
||||
let mut mock_network = MockNetworkTrait::new();
|
||||
|
||||
@@ -362,6 +362,23 @@ impl Network<'_, '_> {
|
||||
.await
|
||||
}
|
||||
|
||||
pub(in crate::network::readarr_network) async fn update_all_authors(&mut self) -> Result<Value> {
|
||||
info!("Updating all authors");
|
||||
let event = ReadarrEvent::UpdateAllAuthors;
|
||||
let body = ReadarrCommandBody {
|
||||
name: "RefreshAuthor".to_owned(),
|
||||
..ReadarrCommandBody::default()
|
||||
};
|
||||
|
||||
let request_props = self
|
||||
.request_props_from(event, RequestMethod::Post, Some(body), None, None)
|
||||
.await;
|
||||
|
||||
self
|
||||
.handle_request::<ReadarrCommandBody, Value>(request_props, |_, _| ())
|
||||
.await
|
||||
}
|
||||
|
||||
pub(in crate::network::readarr_network) async fn update_and_scan_author(
|
||||
&mut self,
|
||||
author_id: i64,
|
||||
|
||||
@@ -843,4 +843,45 @@ mod tests {
|
||||
mock.assert_async().await;
|
||||
assert_err!(result);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_update_all_authors_event() {
|
||||
let (mock, app, _server) = MockServarrApi::post()
|
||||
.with_request_body(json!({
|
||||
"name": "RefreshAuthor"
|
||||
}))
|
||||
.returns(json!({}))
|
||||
.build_for(ReadarrEvent::UpdateAllAuthors)
|
||||
.await;
|
||||
app.lock().await.server_tabs.set_index(3);
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::UpdateAllAuthors)
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
assert_ok!(result);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_update_all_authors_event_failure() {
|
||||
let (mock, app, _server) = MockServarrApi::post()
|
||||
.with_request_body(json!({
|
||||
"name": "RefreshAuthor"
|
||||
}))
|
||||
.returns(json!({"name": "RefreshAuthor", "status": "queued"}))
|
||||
.status(500)
|
||||
.build_for(ReadarrEvent::UpdateAllAuthors)
|
||||
.await;
|
||||
app.lock().await.server_tabs.set_index(3);
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::UpdateAllAuthors)
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
assert_err!(result);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ pub enum ReadarrEvent {
|
||||
StartTask(ReadarrTaskName),
|
||||
ToggleAuthorMonitoring(i64),
|
||||
TriggerAutomaticAuthorSearch(i64),
|
||||
UpdateAllAuthors,
|
||||
UpdateAndScanAuthor(i64),
|
||||
}
|
||||
|
||||
@@ -66,6 +67,7 @@ impl NetworkResource for ReadarrEvent {
|
||||
ReadarrEvent::GetQueuedEvents
|
||||
| ReadarrEvent::StartTask(_)
|
||||
| ReadarrEvent::TriggerAutomaticAuthorSearch(_)
|
||||
| ReadarrEvent::UpdateAllAuthors
|
||||
| ReadarrEvent::UpdateAndScanAuthor(_) => "/command",
|
||||
ReadarrEvent::GetHostConfig | ReadarrEvent::GetSecurityConfig => "/config/host",
|
||||
ReadarrEvent::GetDiskSpace => "/diskspace",
|
||||
@@ -178,6 +180,7 @@ impl Network<'_, '_> {
|
||||
.trigger_automatic_author_search(author_id)
|
||||
.await
|
||||
.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::UpdateAllAuthors => self.update_all_authors().await.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::UpdateAndScanAuthor(author_id) => self
|
||||
.update_and_scan_author(author_id)
|
||||
.await
|
||||
|
||||
@@ -25,6 +25,7 @@ mod tests {
|
||||
#[case(ReadarrEvent::GetQueuedEvents, "/command")]
|
||||
#[case(ReadarrEvent::StartTask(Default::default()), "/command")]
|
||||
#[case(ReadarrEvent::TriggerAutomaticAuthorSearch(1), "/command")]
|
||||
#[case(ReadarrEvent::UpdateAllAuthors, "/command")]
|
||||
#[case(ReadarrEvent::UpdateAndScanAuthor(1), "/command")]
|
||||
#[case(ReadarrEvent::GetHostConfig, "/config/host")]
|
||||
#[case(ReadarrEvent::GetSecurityConfig, "/config/host")]
|
||||
|
||||
Reference in new issue
Block a user