feat: Added initial Sonarr CLI support and the initial network handler setup for the TUI
This commit is contained in:
+76
-6
@@ -8,35 +8,42 @@ use regex::Regex;
|
||||
use reqwest::{Client, RequestBuilder};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use sonarr_network::SonarrEvent;
|
||||
use strum_macros::Display;
|
||||
use tokio::select;
|
||||
use tokio::sync::{Mutex, MutexGuard};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::app::App;
|
||||
use crate::app::{App, ServarrConfig};
|
||||
use crate::models::Serdeable;
|
||||
use crate::network::radarr_network::RadarrEvent;
|
||||
#[cfg(test)]
|
||||
use mockall::automock;
|
||||
|
||||
pub mod radarr_network;
|
||||
pub mod sonarr_network;
|
||||
mod utils;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "network_tests.rs"]
|
||||
mod network_tests;
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub enum NetworkEvent {
|
||||
Radarr(RadarrEvent),
|
||||
}
|
||||
|
||||
#[cfg_attr(test, automock)]
|
||||
#[async_trait]
|
||||
pub trait NetworkTrait {
|
||||
async fn handle_network_event(&mut self, network_event: NetworkEvent) -> Result<Serdeable>;
|
||||
}
|
||||
|
||||
pub trait NetworkResource {
|
||||
fn resource(&self) -> &'static str;
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub enum NetworkEvent {
|
||||
Radarr(RadarrEvent),
|
||||
Sonarr(SonarrEvent),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Network<'a, 'b> {
|
||||
client: Client,
|
||||
@@ -52,6 +59,10 @@ impl<'a, 'b> NetworkTrait for Network<'a, 'b> {
|
||||
.handle_radarr_event(radarr_event)
|
||||
.await
|
||||
.map(Serdeable::from),
|
||||
NetworkEvent::Sonarr(sonarr_event) => self
|
||||
.handle_sonarr_event(sonarr_event)
|
||||
.await
|
||||
.map(Serdeable::from),
|
||||
};
|
||||
|
||||
let mut app = self.app.lock().await;
|
||||
@@ -180,6 +191,65 @@ impl<'a, 'b> Network<'a, 'b> {
|
||||
.header("X-Api-Key", api_token),
|
||||
}
|
||||
}
|
||||
|
||||
async fn request_props_from<T, N>(
|
||||
&self,
|
||||
network_event: N,
|
||||
method: RequestMethod,
|
||||
body: Option<T>,
|
||||
path: Option<String>,
|
||||
query_params: Option<String>,
|
||||
) -> RequestProps<T>
|
||||
where
|
||||
T: Serialize + Debug,
|
||||
N: Into<NetworkEvent> + NetworkResource,
|
||||
{
|
||||
let app = self.app.lock().await;
|
||||
let resource = network_event.resource();
|
||||
let (
|
||||
ServarrConfig {
|
||||
host,
|
||||
port,
|
||||
uri,
|
||||
api_token,
|
||||
ssl_cert_path,
|
||||
},
|
||||
default_port,
|
||||
) = match network_event.into() {
|
||||
NetworkEvent::Radarr(_) => (&app.config.radarr, 7878),
|
||||
NetworkEvent::Sonarr(_) => (&app.config.sonarr, 8989),
|
||||
};
|
||||
let mut uri = if let Some(servarr_uri) = uri {
|
||||
format!("{servarr_uri}/api/v3{resource}")
|
||||
} else {
|
||||
let protocol = if ssl_cert_path.is_some() {
|
||||
"https"
|
||||
} else {
|
||||
"http"
|
||||
};
|
||||
let host = host.as_ref().unwrap();
|
||||
format!(
|
||||
"{protocol}://{host}:{}/api/v3{resource}",
|
||||
port.unwrap_or(default_port)
|
||||
)
|
||||
};
|
||||
|
||||
if let Some(path) = path {
|
||||
uri = format!("{uri}{path}");
|
||||
}
|
||||
|
||||
if let Some(params) = query_params {
|
||||
uri = format!("{uri}?{params}");
|
||||
}
|
||||
|
||||
RequestProps {
|
||||
uri,
|
||||
method,
|
||||
body,
|
||||
api_token: api_token.to_owned(),
|
||||
ignore_status_code: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Display, PartialEq, Eq)]
|
||||
|
||||
Reference in New Issue
Block a user