Successful implementation of movie adding and deleting, and refactored network logic to be more reusable later
This commit is contained in:
+93
-2
@@ -1,7 +1,12 @@
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Arc;
|
||||
|
||||
use reqwest::Client;
|
||||
use tokio::sync::Mutex;
|
||||
use anyhow::anyhow;
|
||||
use log::{debug, error};
|
||||
use reqwest::{Client, RequestBuilder};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use tokio::sync::{Mutex, MutexGuard};
|
||||
|
||||
use crate::app::App;
|
||||
use crate::network::radarr_network::RadarrEvent;
|
||||
@@ -33,4 +38,90 @@ impl<'a> Network<'a> {
|
||||
let mut app = self.app.lock().await;
|
||||
app.is_loading = false;
|
||||
}
|
||||
|
||||
pub async fn handle_request<T, R>(
|
||||
&self,
|
||||
request_props: RequestProps<T>,
|
||||
mut app_update_fn: impl FnMut(R, MutexGuard<App>),
|
||||
) where
|
||||
T: Serialize + Default + Debug,
|
||||
R: DeserializeOwned,
|
||||
{
|
||||
let method = request_props.method.clone();
|
||||
match self.call_api(request_props).await.send().await {
|
||||
Ok(response) => {
|
||||
if response.status().is_success() {
|
||||
match method {
|
||||
RequestMethod::Get => match utils::parse_response::<R>(response).await {
|
||||
Ok(value) => {
|
||||
let app = self.app.lock().await;
|
||||
app_update_fn(value, app);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to parse response! {:?}", e);
|
||||
self.app.lock().await.handle_error(anyhow!(e));
|
||||
}
|
||||
},
|
||||
RequestMethod::Delete | RequestMethod::Post => (),
|
||||
}
|
||||
} else {
|
||||
error!(
|
||||
"Request failed. Received {} response code",
|
||||
response.status()
|
||||
);
|
||||
self.app.lock().await.handle_error(anyhow!(
|
||||
"Request failed. Received {} response code",
|
||||
response.status()
|
||||
));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to send request. {:?}", e);
|
||||
self.app.lock().await.handle_error(anyhow!(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn call_api<T: Serialize + Default + Debug>(
|
||||
&self,
|
||||
request_props: RequestProps<T>,
|
||||
) -> RequestBuilder {
|
||||
let RequestProps {
|
||||
uri,
|
||||
method,
|
||||
body,
|
||||
api_token,
|
||||
} = request_props;
|
||||
debug!("Creating RequestBuilder for resource: {:?}", uri);
|
||||
let app = self.app.lock().await;
|
||||
debug!(
|
||||
"Sending {:?} request to {} with body {:?}",
|
||||
method, uri, body
|
||||
);
|
||||
|
||||
match method {
|
||||
RequestMethod::Get => app.client.get(uri).header("X-Api-Key", api_token),
|
||||
RequestMethod::Post => app
|
||||
.client
|
||||
.post(uri)
|
||||
.json(&body.unwrap_or_default())
|
||||
.header("X-Api-Key", api_token),
|
||||
RequestMethod::Delete => app.client.delete(uri).header("X-Api-Key", api_token),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum RequestMethod {
|
||||
Get,
|
||||
Post,
|
||||
Delete,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RequestProps<T: Serialize + Debug> {
|
||||
pub uri: String,
|
||||
pub method: RequestMethod,
|
||||
pub body: Option<T>,
|
||||
pub api_token: String,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user