feat: Added initial Sonarr CLI support and the initial network handler setup for the TUI

This commit is contained in:
2024-11-10 21:23:55 -07:00
parent b6f5b9d08c
commit 60d61b9e31
28 changed files with 2419 additions and 761 deletions
+106
View File
@@ -0,0 +1,106 @@
use anyhow::Result;
use log::info;
use crate::{
models::{
servarr_data::sonarr::sonarr_data::ActiveSonarrBlock,
sonarr_models::{Series, SonarrSerdeable, SystemStatus},
Route,
},
network::RequestMethod,
};
use super::{Network, NetworkEvent, NetworkResource};
#[cfg(test)]
#[path = "sonarr_network_tests.rs"]
mod sonarr_network_tests;
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum SonarrEvent {
GetStatus,
HealthCheck,
ListSeries,
}
impl NetworkResource for SonarrEvent {
fn resource(&self) -> &'static str {
match &self {
SonarrEvent::GetStatus => "/system/status",
SonarrEvent::HealthCheck => "/health",
SonarrEvent::ListSeries => "/series",
}
}
}
impl From<SonarrEvent> for NetworkEvent {
fn from(sonarr_event: SonarrEvent) -> Self {
NetworkEvent::Sonarr(sonarr_event)
}
}
impl<'a, 'b> Network<'a, 'b> {
pub async fn handle_sonarr_event(
&mut self,
sonarr_event: SonarrEvent,
) -> Result<SonarrSerdeable> {
match sonarr_event {
SonarrEvent::GetStatus => self.get_sonarr_status().await.map(SonarrSerdeable::from),
SonarrEvent::HealthCheck => self
.get_sonarr_healthcheck()
.await
.map(SonarrSerdeable::from),
SonarrEvent::ListSeries => self.list_series().await.map(SonarrSerdeable::from),
}
}
async fn get_sonarr_healthcheck(&mut self) -> Result<()> {
info!("Performing Sonarr health check");
let event = SonarrEvent::HealthCheck;
let request_props = self
.request_props_from(event, RequestMethod::Get, None::<()>, None, None)
.await;
self
.handle_request::<(), ()>(request_props, |_, _| ())
.await
}
async fn list_series(&mut self) -> Result<Vec<Series>> {
info!("Fetching Sonarr library");
let event = SonarrEvent::ListSeries;
let request_props = self
.request_props_from(event, RequestMethod::Get, None::<()>, None, None)
.await;
self
.handle_request::<(), Vec<Series>>(request_props, |mut series_vec, mut app| {
if !matches!(
app.get_current_route(),
Route::Sonarr(ActiveSonarrBlock::SeriesSortPrompt, _)
) {
series_vec.sort_by(|a, b| a.id.cmp(&b.id));
app.data.sonarr_data.series.set_items(series_vec);
app.data.sonarr_data.series.apply_sorting_toggle(false);
}
})
.await
}
async fn get_sonarr_status(&mut self) -> Result<SystemStatus> {
info!("Fetching Sonarr system status");
let event = SonarrEvent::GetStatus;
let request_props = self
.request_props_from(event, RequestMethod::Get, None::<()>, None, None)
.await;
self
.handle_request::<(), SystemStatus>(request_props, |system_status, mut app| {
app.data.sonarr_data.version = system_status.version;
app.data.sonarr_data.start_time = system_status.start_time;
})
.await
}
}