diff --git a/src/services/book_library/mod.rs b/src/services/book_library/mod.rs index aac6aa7..ca46d1e 100644 --- a/src/services/book_library/mod.rs +++ b/src/services/book_library/mod.rs @@ -1,9 +1,12 @@ pub mod types; +use once_cell::sync::Lazy; use serde::de::DeserializeOwned; use crate::config; +pub static CLIENT: Lazy = Lazy::new(reqwest::Client::new); + async fn _make_request( url: &str, params: Vec<(&str, String)>, @@ -11,11 +14,9 @@ async fn _make_request( where T: DeserializeOwned, { - let client = reqwest::Client::new(); - let formatted_url = format!("{}{}", &config::CONFIG.book_library_url, url); - let response = client + let response = CLIENT .get(formatted_url) .query(¶ms) .header("Authorization", &config::CONFIG.book_library_api_key) diff --git a/src/services/downloader/mod.rs b/src/services/downloader/mod.rs index 1a59ae1..3345d33 100644 --- a/src/services/downloader/mod.rs +++ b/src/services/downloader/mod.rs @@ -2,6 +2,7 @@ pub mod types; pub mod utils; pub mod zip; +use once_cell::sync::Lazy; use reqwest::Response; use tokio::task::JoinSet; @@ -15,6 +16,8 @@ use super::book_library::types::BookWithRemote; use super::covert::convert_file; use super::{book_library::get_remote_book, filename_getter::get_filename_by_book}; +pub static CLIENT: Lazy = Lazy::new(reqwest::Client::new); + pub async fn download<'a>( book_id: &'a u32, book_file_type: &'a str, @@ -37,7 +40,7 @@ pub async fn download<'a>( .build() .unwrap() } - None => reqwest::Client::new(), + None => CLIENT.clone(), }; let response = client.get(url).send().await; diff --git a/src/services/downloader/utils.rs b/src/services/downloader/utils.rs index 21d5964..1446266 100644 --- a/src/services/downloader/utils.rs +++ b/src/services/downloader/utils.rs @@ -25,7 +25,7 @@ pub async fn response_to_tempfile(res: &mut Response) -> Option<(SpooledTempFile data_size += data.len(); - match tmp_file.write(data.chunk()) { + match tmp_file.write_all(data.chunk()) { Ok(_) => (), Err(_) => return None, }