feat(cli): Support for downloading a Series release in Sonarr
This commit is contained in:
@@ -0,0 +1,90 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use clap::Subcommand;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
app::App,
|
||||||
|
cli::{CliCommandHandler, Command},
|
||||||
|
models::sonarr_models::SonarrReleaseDownloadBody,
|
||||||
|
network::{sonarr_network::SonarrEvent, NetworkTrait},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::SonarrCommand;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "download_command_handler_tests.rs"]
|
||||||
|
mod download_command_handler_tests;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Subcommand)]
|
||||||
|
pub enum SonarrDownloadCommand {
|
||||||
|
#[command(about = "Manually download the given series release for the specified series ID")]
|
||||||
|
Series {
|
||||||
|
#[arg(long, help = "The GUID of the release to download", required = true)]
|
||||||
|
guid: String,
|
||||||
|
#[arg(
|
||||||
|
long,
|
||||||
|
help = "The indexer ID to download the release from",
|
||||||
|
required = true
|
||||||
|
)]
|
||||||
|
indexer_id: i64,
|
||||||
|
#[arg(
|
||||||
|
long,
|
||||||
|
help = "The series ID that the release is associated with",
|
||||||
|
required = true
|
||||||
|
)]
|
||||||
|
series_id: i64,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SonarrDownloadCommand> for Command {
|
||||||
|
fn from(value: SonarrDownloadCommand) -> Self {
|
||||||
|
Command::Sonarr(SonarrCommand::Download(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) struct SonarrDownloadCommandHandler<'a, 'b> {
|
||||||
|
_app: &'a Arc<Mutex<App<'b>>>,
|
||||||
|
command: SonarrDownloadCommand,
|
||||||
|
network: &'a mut dyn NetworkTrait,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrDownloadCommand>
|
||||||
|
for SonarrDownloadCommandHandler<'a, 'b>
|
||||||
|
{
|
||||||
|
fn with(
|
||||||
|
_app: &'a Arc<Mutex<App<'b>>>,
|
||||||
|
command: SonarrDownloadCommand,
|
||||||
|
network: &'a mut dyn NetworkTrait,
|
||||||
|
) -> Self {
|
||||||
|
SonarrDownloadCommandHandler {
|
||||||
|
_app,
|
||||||
|
command,
|
||||||
|
network,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle(self) -> anyhow::Result<String> {
|
||||||
|
let result = match self.command {
|
||||||
|
SonarrDownloadCommand::Series {
|
||||||
|
guid,
|
||||||
|
indexer_id,
|
||||||
|
series_id,
|
||||||
|
} => {
|
||||||
|
let params = SonarrReleaseDownloadBody {
|
||||||
|
guid,
|
||||||
|
indexer_id,
|
||||||
|
series_id: Some(series_id),
|
||||||
|
..SonarrReleaseDownloadBody::default()
|
||||||
|
};
|
||||||
|
let resp = self
|
||||||
|
.network
|
||||||
|
.handle_network_event(SonarrEvent::DownloadRelease(params).into())
|
||||||
|
.await?;
|
||||||
|
serde_json::to_string_pretty(&resp)?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::{
|
||||||
|
cli::{
|
||||||
|
sonarr::{download_command_handler::SonarrDownloadCommand, SonarrCommand},
|
||||||
|
Command,
|
||||||
|
},
|
||||||
|
Cli,
|
||||||
|
};
|
||||||
|
use clap::CommandFactory;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sonarr_download_command_from() {
|
||||||
|
let command = SonarrDownloadCommand::Series {
|
||||||
|
guid: "Test".to_owned(),
|
||||||
|
indexer_id: 1,
|
||||||
|
series_id: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = Command::from(command.clone());
|
||||||
|
|
||||||
|
assert_eq!(result, Command::Sonarr(SonarrCommand::Download(command)));
|
||||||
|
}
|
||||||
|
|
||||||
|
mod cli {
|
||||||
|
use super::*;
|
||||||
|
use clap::error::ErrorKind;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn test_download_series_requires_series_id() {
|
||||||
|
let result = Cli::command().try_get_matches_from([
|
||||||
|
"managarr",
|
||||||
|
"sonarr",
|
||||||
|
"download",
|
||||||
|
"series",
|
||||||
|
"--indexer-id",
|
||||||
|
"1",
|
||||||
|
"--guid",
|
||||||
|
"1",
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap_err().kind(),
|
||||||
|
ErrorKind::MissingRequiredArgument
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn test_download_series_requires_guid() {
|
||||||
|
let result = Cli::command().try_get_matches_from([
|
||||||
|
"managarr",
|
||||||
|
"sonarr",
|
||||||
|
"download",
|
||||||
|
"series",
|
||||||
|
"--indexer-id",
|
||||||
|
"1",
|
||||||
|
"--series-id",
|
||||||
|
"1",
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap_err().kind(),
|
||||||
|
ErrorKind::MissingRequiredArgument
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn test_download_series_requires_indexer_id() {
|
||||||
|
let result = Cli::command().try_get_matches_from([
|
||||||
|
"managarr",
|
||||||
|
"sonarr",
|
||||||
|
"download",
|
||||||
|
"series",
|
||||||
|
"--guid",
|
||||||
|
"1",
|
||||||
|
"--series-id",
|
||||||
|
"1",
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap_err().kind(),
|
||||||
|
ErrorKind::MissingRequiredArgument
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_download_series_requirements_satisfied() {
|
||||||
|
let result = Cli::command().try_get_matches_from([
|
||||||
|
"managarr",
|
||||||
|
"sonarr",
|
||||||
|
"download",
|
||||||
|
"series",
|
||||||
|
"--guid",
|
||||||
|
"1",
|
||||||
|
"--series-id",
|
||||||
|
"1",
|
||||||
|
"--indexer-id",
|
||||||
|
"1",
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod handler {
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use mockall::predicate::eq;
|
||||||
|
use serde_json::json;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
app::App,
|
||||||
|
cli::{
|
||||||
|
sonarr::download_command_handler::{SonarrDownloadCommand, SonarrDownloadCommandHandler},
|
||||||
|
CliCommandHandler,
|
||||||
|
},
|
||||||
|
models::{
|
||||||
|
sonarr_models::{SonarrReleaseDownloadBody, SonarrSerdeable},
|
||||||
|
Serdeable,
|
||||||
|
},
|
||||||
|
network::{sonarr_network::SonarrEvent, MockNetworkTrait, NetworkEvent},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_download_release_command() {
|
||||||
|
let expected_release_download_body = SonarrReleaseDownloadBody {
|
||||||
|
guid: "guid".to_owned(),
|
||||||
|
indexer_id: 1,
|
||||||
|
series_id: Some(1),
|
||||||
|
..SonarrReleaseDownloadBody::default()
|
||||||
|
};
|
||||||
|
let mut mock_network = MockNetworkTrait::new();
|
||||||
|
mock_network
|
||||||
|
.expect_handle_network_event()
|
||||||
|
.with(eq::<NetworkEvent>(
|
||||||
|
SonarrEvent::DownloadRelease(expected_release_download_body).into(),
|
||||||
|
))
|
||||||
|
.times(1)
|
||||||
|
.returning(|_| {
|
||||||
|
Ok(Serdeable::Sonarr(SonarrSerdeable::Value(
|
||||||
|
json!({"testResponse": "response"}),
|
||||||
|
)))
|
||||||
|
});
|
||||||
|
let app_arc = Arc::new(Mutex::new(App::default()));
|
||||||
|
let download_release_command = SonarrDownloadCommand::Series {
|
||||||
|
guid: "guid".to_owned(),
|
||||||
|
indexer_id: 1,
|
||||||
|
series_id: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let result =
|
||||||
|
SonarrDownloadCommandHandler::with(&app_arc, download_release_command, &mut mock_network)
|
||||||
|
.handle()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ use add_command_handler::{SonarrAddCommand, SonarrAddCommandHandler};
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Subcommand;
|
use clap::Subcommand;
|
||||||
use delete_command_handler::{SonarrDeleteCommand, SonarrDeleteCommandHandler};
|
use delete_command_handler::{SonarrDeleteCommand, SonarrDeleteCommandHandler};
|
||||||
|
use download_command_handler::{SonarrDownloadCommand, SonarrDownloadCommandHandler};
|
||||||
use get_command_handler::{SonarrGetCommand, SonarrGetCommandHandler};
|
use get_command_handler::{SonarrGetCommand, SonarrGetCommandHandler};
|
||||||
use list_command_handler::{SonarrListCommand, SonarrListCommandHandler};
|
use list_command_handler::{SonarrListCommand, SonarrListCommandHandler};
|
||||||
use refresh_command_handler::{SonarrRefreshCommand, SonarrRefreshCommandHandler};
|
use refresh_command_handler::{SonarrRefreshCommand, SonarrRefreshCommandHandler};
|
||||||
@@ -19,6 +20,7 @@ use super::{CliCommandHandler, Command};
|
|||||||
|
|
||||||
mod add_command_handler;
|
mod add_command_handler;
|
||||||
mod delete_command_handler;
|
mod delete_command_handler;
|
||||||
|
mod download_command_handler;
|
||||||
mod get_command_handler;
|
mod get_command_handler;
|
||||||
mod list_command_handler;
|
mod list_command_handler;
|
||||||
mod refresh_command_handler;
|
mod refresh_command_handler;
|
||||||
@@ -44,6 +46,11 @@ pub enum SonarrCommand {
|
|||||||
about = "Commands to fetch details of the resources in your Sonarr instance"
|
about = "Commands to fetch details of the resources in your Sonarr instance"
|
||||||
)]
|
)]
|
||||||
Get(SonarrGetCommand),
|
Get(SonarrGetCommand),
|
||||||
|
#[command(
|
||||||
|
subcommand,
|
||||||
|
about = "Commands to download releases in your Sonarr instance"
|
||||||
|
)]
|
||||||
|
Download(SonarrDownloadCommand),
|
||||||
#[command(
|
#[command(
|
||||||
subcommand,
|
subcommand,
|
||||||
about = "Commands to list attributes from your Sonarr instance"
|
about = "Commands to list attributes from your Sonarr instance"
|
||||||
@@ -176,6 +183,11 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrCommand> for SonarrCliHandler<'a, '
|
|||||||
.handle()
|
.handle()
|
||||||
.await?
|
.await?
|
||||||
}
|
}
|
||||||
|
SonarrCommand::Download(download_command) => {
|
||||||
|
SonarrDownloadCommandHandler::with(self.app, download_command, self.network)
|
||||||
|
.handle()
|
||||||
|
.await?
|
||||||
|
}
|
||||||
SonarrCommand::Get(get_command) => {
|
SonarrCommand::Get(get_command) => {
|
||||||
SonarrGetCommandHandler::with(self.app, get_command, self.network)
|
SonarrGetCommandHandler::with(self.app, get_command, self.network)
|
||||||
.handle()
|
.handle()
|
||||||
|
|||||||
@@ -311,14 +311,16 @@ mod tests {
|
|||||||
cli::{
|
cli::{
|
||||||
sonarr::{
|
sonarr::{
|
||||||
add_command_handler::SonarrAddCommand, delete_command_handler::SonarrDeleteCommand,
|
add_command_handler::SonarrAddCommand, delete_command_handler::SonarrDeleteCommand,
|
||||||
get_command_handler::SonarrGetCommand, list_command_handler::SonarrListCommand,
|
download_command_handler::SonarrDownloadCommand, get_command_handler::SonarrGetCommand,
|
||||||
refresh_command_handler::SonarrRefreshCommand, SonarrCliHandler, SonarrCommand,
|
list_command_handler::SonarrListCommand, refresh_command_handler::SonarrRefreshCommand,
|
||||||
|
SonarrCliHandler, SonarrCommand,
|
||||||
},
|
},
|
||||||
CliCommandHandler,
|
CliCommandHandler,
|
||||||
},
|
},
|
||||||
models::{
|
models::{
|
||||||
sonarr_models::{
|
sonarr_models::{
|
||||||
BlocklistItem, BlocklistResponse, Series, SonarrSerdeable, SonarrTaskName,
|
BlocklistItem, BlocklistResponse, Series, SonarrReleaseDownloadBody, SonarrSerdeable,
|
||||||
|
SonarrTaskName,
|
||||||
},
|
},
|
||||||
Serdeable,
|
Serdeable,
|
||||||
},
|
},
|
||||||
@@ -500,6 +502,42 @@ mod tests {
|
|||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_sonarr_cli_handler_delegates_download_commands_to_the_download_command_handler() {
|
||||||
|
let expected_params = SonarrReleaseDownloadBody {
|
||||||
|
guid: "1234".to_owned(),
|
||||||
|
indexer_id: 1,
|
||||||
|
series_id: Some(1),
|
||||||
|
..SonarrReleaseDownloadBody::default()
|
||||||
|
};
|
||||||
|
let mut mock_network = MockNetworkTrait::new();
|
||||||
|
mock_network
|
||||||
|
.expect_handle_network_event()
|
||||||
|
.with(eq::<NetworkEvent>(
|
||||||
|
SonarrEvent::DownloadRelease(expected_params).into(),
|
||||||
|
))
|
||||||
|
.times(1)
|
||||||
|
.returning(|_| {
|
||||||
|
Ok(Serdeable::Sonarr(SonarrSerdeable::Value(
|
||||||
|
json!({"testResponse": "response"}),
|
||||||
|
)))
|
||||||
|
});
|
||||||
|
let app_arc = Arc::new(Mutex::new(App::default()));
|
||||||
|
let download_series_release_command =
|
||||||
|
SonarrCommand::Download(SonarrDownloadCommand::Series {
|
||||||
|
guid: "1234".to_owned(),
|
||||||
|
indexer_id: 1,
|
||||||
|
series_id: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
let result =
|
||||||
|
SonarrCliHandler::with(&app_arc, download_series_release_command, &mut mock_network)
|
||||||
|
.handle()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_sonarr_cli_handler_delegates_get_commands_to_the_get_command_handler() {
|
async fn test_sonarr_cli_handler_delegates_get_commands_to_the_get_command_handler() {
|
||||||
let mut mock_network = MockNetworkTrait::new();
|
let mut mock_network = MockNetworkTrait::new();
|
||||||
|
|||||||
Reference in New Issue
Block a user