Skip to content

Commit

Permalink
refactor(targets): use get_url util for subpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-online committed Jan 15, 2025
1 parent a9d9734 commit ab57f5a
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/service/targets/autopulse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
db::models::ScanEvent,
settings::{auth::Auth, target::TargetProcess},
utils::get_url::get_url,
};
use reqwest::header;
use serde::Deserialize;
Expand Down Expand Up @@ -35,7 +36,7 @@ impl Autopulse {

async fn scan(&self, ev: &ScanEvent) -> anyhow::Result<()> {
let client = self.get_client()?;
let mut url = url::Url::parse(&self.url)?.join("/triggers/manual")?;
let mut url = get_url(&self.url)?.join("triggers/manual")?;

url.query_pairs_mut().append_pair("path", &ev.file_path);

Expand Down
28 changes: 10 additions & 18 deletions src/service/targets/emby.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use crate::{db::models::ScanEvent, settings::target::TargetProcess, utils::get_url::get_url};
use anyhow::Context;
use reqwest::header;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -117,20 +117,12 @@ impl Emby {
.map_err(Into::into)
}

fn get_url(&self) -> anyhow::Result<url::Url> {
let url = if self.url.ends_with('/') {
self.url.clone()
} else {
format!("{}/", self.url)
};

url::Url::parse(&url).map_err(Into::into)
}

async fn libraries(&self) -> anyhow::Result<Vec<Library>> {
let client = self.get_client()?;

let url = self.get_url()?.join("Library/VirtualFolders")?.to_string();
let url = get_url(&self.url)?
.join("Library/VirtualFolders")?
.to_string();

let res = client.get(&url).send().await?;
let status = res.status();
Expand Down Expand Up @@ -166,7 +158,7 @@ impl Emby {

async fn _get_item(&self, library: &Library, path: &str) -> anyhow::Result<Option<Item>> {
let client = self.get_client()?;
let mut url = self.get_url()?.join("Items")?;
let mut url = get_url(&self.url)?.join("Items")?;

url.query_pairs_mut().append_pair("Recursive", "true");
url.query_pairs_mut().append_pair("Fields", "Path");
Expand Down Expand Up @@ -220,7 +212,7 @@ impl Emby {
let limit = 1000;

let client = self.get_client()?;
let mut url = self.get_url()?.join("Items")?;
let mut url = get_url(&self.url)?.join("Items")?;

url.query_pairs_mut().append_pair("Recursive", "true");
url.query_pairs_mut().append_pair("Fields", "Path");
Expand Down Expand Up @@ -319,7 +311,9 @@ impl Emby {
// not as effective as refreshing the item, but good enough
async fn scan(&self, ev: &[&ScanEvent]) -> anyhow::Result<()> {
let client = self.get_client()?;
let url = self.get_url()?.join("Library/Media/Updated")?.to_string();
let url = get_url(&self.url)?
.join("Library/Media/Updated")?
.to_string();

let updates = ev
.iter()
Expand Down Expand Up @@ -354,9 +348,7 @@ impl Emby {

async fn refresh_item(&self, item: &Item) -> anyhow::Result<()> {
let client = self.get_client()?;
let mut url = self
.get_url()?
.join(&format!("Items/{}/Refresh", item.id))?;
let mut url = get_url(&self.url)?.join(&format!("Items/{}/Refresh", item.id))?;

url.query_pairs_mut().append_pair(
"metadataRefreshMode",
Expand Down
14 changes: 7 additions & 7 deletions src/service/targets/fileflows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use crate::{db::models::ScanEvent, settings::target::TargetProcess, utils::get_url::get_url};
use anyhow::Context;
use reqwest::header;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -97,7 +97,7 @@ impl FileFlows {
async fn get_libraries(&self) -> anyhow::Result<Vec<FileFlowsLibrary>> {
let client = self.get_client()?;

let url = url::Url::parse(&self.url)?.join("/api/library")?;
let url = get_url(&self.url)?.join("api/library")?;

let res = client.get(url.to_string()).send().await?;
let status = res.status();
Expand All @@ -123,7 +123,7 @@ impl FileFlows {
) -> anyhow::Result<Option<FileFlowsLibraryFile>> {
let client = self.get_client()?;

let url = url::Url::parse(&self.url)?.join("/api/library-file/search")?;
let url = get_url(&self.url)?.join("api/library-file/search")?;

let req = FileFlowsSearchRequest {
path: ev.file_path.clone(),
Expand Down Expand Up @@ -151,7 +151,7 @@ impl FileFlows {
async fn reprocess_library_filse(&self, evs: Vec<&FileFlowsLibraryFile>) -> anyhow::Result<()> {
let client = self.get_client()?;

let url = url::Url::parse(&self.url)?.join("/api/library-file/reprocess")?;
let url = get_url(&self.url)?.join("api/library-file/reprocess")?;

let req = FileFlowsReprocessRequest {
uids: evs.iter().map(|ev| ev.uid.clone()).collect(),
Expand Down Expand Up @@ -181,7 +181,7 @@ impl FileFlows {
) -> anyhow::Result<()> {
let client = self.get_client()?;

let url = url::Url::parse(&self.url)?.join("/api/library-file/manually-add")?;
let url = get_url(&self.url)?.join("api/library-file/manually-add")?;

let req = FileFlowsManuallyAddRequest {
flow_uid: library.flow.as_ref().unwrap().uid.clone(),
Expand All @@ -208,7 +208,7 @@ impl FileFlows {
// async fn rescan_library(&self, libraries: &FileFlowsLibrary) -> anyhow::Result<()> {
// let client = self.get_client()?;

// let url = url::Url::parse(&self.url)?.join("/api/library/rescan")?;
// let url = get_url(&self.url)?.join("/api/library/rescan")?;

// let req = FileFlowsRescanLibraryRequest {
// uids: vec![libraries.uid.clone()],
Expand All @@ -228,7 +228,7 @@ impl FileFlows {
// async fn scan(&self, ev: &ScanEvent, library: &FileFlowsLibrary) -> anyhow::Result<()> {
// let client = self.get_client()?;

// let mut url = url::Url::parse(&self.url)?.join("/api/library-file/process-file")?;
// let mut url = get_url(&self.url)?.join("/api/library-file/process-file")?;

// url.query_pairs_mut().append_pair("filename", &ev.file_path);

Expand Down
22 changes: 10 additions & 12 deletions src/service/targets/plex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::Path;

use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use crate::{db::models::ScanEvent, settings::target::TargetProcess, utils::get_url::get_url};
use anyhow::Context;
use reqwest::header;
use serde::Deserialize;
Expand Down Expand Up @@ -96,9 +96,7 @@ impl Plex {

async fn libraries(&self) -> anyhow::Result<Vec<Library>> {
let client = self.get_client()?;
let url = url::Url::parse(&self.url)?
.join("/library/sections")?
.to_string();
let url = get_url(&self.url)?.join("library/sections")?.to_string();

let res = client.get(&url).send().await?;
let status = res.status();
Expand Down Expand Up @@ -138,8 +136,8 @@ impl Plex {
// TODO: Change to get_items
async fn get_item(&self, library: &Library, path: &str) -> anyhow::Result<Option<Metadata>> {
let client = self.get_client()?;
let url = url::Url::parse(&self.url)?
.join(&format!("/library/sections/{}/all", library.key))?
let url = get_url(&self.url)?
.join(&format!("library/sections/{}/all", library.key))?
.to_string();

let res = client.get(&url).send().await?;
Expand Down Expand Up @@ -175,7 +173,7 @@ impl Plex {
// async fn refresh_library(&self, library: &str) -> anyhow::Result<()> {
// let client = self.get_client()?;
// let mut url =
// url::Url::parse(&self.url)?.join(&format!("/library/sections/{}/refresh", library))?;
// get_url(&self.url)?.join(&format!("/library/sections/{}/refresh", library))?;

// url.query_pairs_mut().append_pair("force", "1");

Expand All @@ -192,7 +190,7 @@ impl Plex {
// async fn analyze_library(&self, library: &str) -> anyhow::Result<()> {
// let client = self.get_client()?;
// let url =
// url::Url::parse(&self.url)?.join(&format!("/library/sections/{}/analyze", library))?;
// get_url(&self.url)?.join(&format!("/library/sections/{}/analyze", library))?;

// let res = client.put(url.to_string()).send().await?;

Expand All @@ -206,7 +204,7 @@ impl Plex {

async fn refresh_item(&self, key: &str) -> anyhow::Result<()> {
let client = self.get_client()?;
let url = url::Url::parse(&self.url)?.join(&format!("{}/refresh", key))?;
let url = get_url(&self.url)?.join(&format!("{}/refresh", key))?;

let res = client.put(url.to_string()).send().await?;

Expand All @@ -220,7 +218,7 @@ impl Plex {

async fn analyze_item(&self, key: &str) -> anyhow::Result<()> {
let client = self.get_client()?;
let url = url::Url::parse(&self.url)?.join(&format!("{}/analyze", key))?;
let url = get_url(&self.url)?.join(&format!("{}/analyze", key))?;

let res = client.put(url.to_string()).send().await?;

Expand All @@ -234,8 +232,8 @@ impl Plex {

async fn scan(&self, ev: &ScanEvent, library: &Library) -> anyhow::Result<()> {
let client = self.get_client()?;
let mut url = url::Url::parse(&self.url)?
.join(&format!("/library/sections/{}/refresh", library.key))?;
let mut url =
get_url(&self.url)?.join(&format!("library/sections/{}/refresh", library.key))?;

let file_dir = std::path::Path::new(&ev.file_path)
.parent()
Expand Down
6 changes: 2 additions & 4 deletions src/service/targets/tdarr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use reqwest::header;
use serde::{Deserialize, Serialize};

use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use crate::{db::models::ScanEvent, settings::target::TargetProcess, utils::get_url::get_url};

#[derive(Deserialize, Clone)]
pub struct Tdarr {
Expand Down Expand Up @@ -59,9 +59,7 @@ impl Tdarr {
},
};

let url = url::Url::parse(&self.url)?
.join("/api/v2/scan-files")?
.to_string();
let url = get_url(&self.url)?.join("/api/v2/scan-files")?.to_string();

let res = client
.post(&url)
Expand Down
31 changes: 31 additions & 0 deletions src/tests/utils/get_url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[cfg(test)]
mod tests {
use crate::utils::get_url::get_url;

#[test]
fn test_join_no_subpath() -> anyhow::Result<()> {
let url = "http://example.com".to_string();

let parsed = get_url(&url)?;

assert_eq!(parsed.to_string(), "http://example.com/");
assert_eq!(parsed.join("test")?.to_string(), "http://example.com/test");

Ok(())
}

#[test]
fn test_join_with_subpath() -> anyhow::Result<()> {
let url = "http://example.com/test".to_string();

let parsed = get_url(&url)?;

assert_eq!(parsed.to_string(), "http://example.com/test/");
assert_eq!(
parsed.join("test")?.to_string(),
"http://example.com/test/test"
);

Ok(())
}
}
1 change: 1 addition & 0 deletions src/tests/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod check_auth;
pub mod checksum;
pub mod generate_uuid;
pub mod get_url;
pub mod join_path;
pub mod rewrite;
pub mod sify;
10 changes: 10 additions & 0 deletions src/utils/get_url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::borrow::Cow;

pub fn get_url(url: &str) -> anyhow::Result<url::Url> {
let url: Cow<str> = if url.ends_with('/') {
Cow::Borrowed(url)
} else {
format!("{}/", url).into()
};
url::Url::parse(&url).map_err(Into::into)
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub mod generate_uuid;
#[doc(hidden)]
pub mod get_timestamp;
#[doc(hidden)]
pub mod get_url;
#[doc(hidden)]
pub mod join_path;
#[doc(hidden)]
pub mod logs;
Expand Down

0 comments on commit ab57f5a

Please sign in to comment.