Fix for reuse connections

This commit is contained in:
2024-05-13 12:54:42 +02:00
parent eae46c8225
commit a07f3221b1
3 changed files with 8 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
use std::fmt; use std::fmt;
use base64::{engine::general_purpose, Engine}; use base64::{engine::general_purpose, Engine};
use once_cell::sync::Lazy;
use reqwest::StatusCode; use reqwest::StatusCode;
use smartstring::alias::String as SmartString; use smartstring::alias::String as SmartString;
use tempfile::SpooledTempFile; use tempfile::SpooledTempFile;
@@ -10,6 +11,8 @@ use crate::config;
use super::utils::response_to_tempfile; use super::utils::response_to_tempfile;
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct DownloadError { struct DownloadError {
status_code: StatusCode, status_code: StatusCode,
@@ -27,7 +30,7 @@ pub async fn download(
book_id: u64, book_id: u64,
file_type: SmartString, file_type: SmartString,
) -> Result<(SpooledTempFile, String), Box<dyn std::error::Error + Send + Sync>> { ) -> Result<(SpooledTempFile, String), Box<dyn std::error::Error + Send + Sync>> {
let mut response = reqwest::Client::new() let mut response = CLIENT
.get(format!( .get(format!(
"{}/api/v1/download/{book_id}/{file_type}/", "{}/api/v1/download/{book_id}/{file_type}/",
&config::CONFIG.cache_url &config::CONFIG.cache_url

View File

@@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use serde::{de::DeserializeOwned, Deserialize}; use serde::{de::DeserializeOwned, Deserialize};
use smallvec::SmallVec; use smallvec::SmallVec;
use smartstring::alias::String as SmartString; use smartstring::alias::String as SmartString;
@@ -5,6 +6,7 @@ use tracing::log;
use crate::config; use crate::config;
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
const PAGE_SIZE: &str = "50"; const PAGE_SIZE: &str = "50";
fn get_allowed_langs_params( fn get_allowed_langs_params(
@@ -23,7 +25,7 @@ async fn _make_request<T>(
where where
T: DeserializeOwned, T: DeserializeOwned,
{ {
let response = reqwest::Client::new() let response = CLIENT
.get(format!("{}{}", &config::CONFIG.library_url, url)) .get(format!("{}{}", &config::CONFIG.library_url, url))
.query(&params) .query(&params)
.header("Authorization", &config::CONFIG.library_api_key) .header("Authorization", &config::CONFIG.library_api_key)

View File

@@ -46,12 +46,7 @@ pub async fn response_to_tempfile(
data_size += data.len(); data_size += data.len();
match tmp_file.write(data.chunk()) { tmp_file.write_all(data.chunk())?;
Ok(_) => (),
Err(err) => {
return Err(Box::new(err));
}
}
} }
tmp_file.seek(SeekFrom::Start(0)).unwrap(); tmp_file.seek(SeekFrom::Start(0)).unwrap();