Fix for reuse connections

This commit is contained in:
2024-05-13 13:16:42 +02:00
parent a390a5aa8e
commit 548b3d66d7
5 changed files with 38 additions and 17 deletions

View File

@@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use smallvec::SmallVec;
use smartstring::alias::String as SmartString;
@@ -5,6 +6,10 @@ use serde::{Deserialize, Serialize};
use crate::config;
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TaskObjectType {
@@ -43,7 +48,7 @@ pub struct Task {
pub async fn create_task(
data: CreateTaskData,
) -> Result<Task, Box<dyn std::error::Error + Send + Sync>> {
Ok(reqwest::Client::new()
Ok(CLIENT
.post(format!("{}/api/", &config::CONFIG.batch_downloader_url))
.body(serde_json::to_string(&data).unwrap())
.header("Authorization", &config::CONFIG.batch_downloader_api_key)
@@ -56,7 +61,7 @@ pub async fn create_task(
}
pub async fn get_task(task_id: String) -> Result<Task, Box<dyn std::error::Error + Send + Sync>> {
Ok(reqwest::Client::new()
Ok(CLIENT
.get(format!(
"{}/api/check_archive/{task_id}",
&config::CONFIG.batch_downloader_url

View File

@@ -1,4 +1,5 @@
use base64::{engine::general_purpose, Engine};
use once_cell::sync::Lazy;
use reqwest::StatusCode;
use crate::{bots::approved_bot::modules::download::callback_data::DownloadQueryData, bots_manager::BotCache, config};
@@ -8,6 +9,9 @@ use self::types::{CachedMessage, DownloadFile};
pub mod types;
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
pub async fn get_cached_message(
download_data: &DownloadQueryData,
bot_cache: BotCache,
@@ -19,8 +23,7 @@ pub async fn get_cached_message(
let is_need_copy = bot_cache == BotCache::Cache;
let client = reqwest::Client::new();
let response = client
let response = CLIENT
.get(format!(
"{}/api/v1/{id}/{format}/?copy={is_need_copy}",
&config::CONFIG.cache_server_url
@@ -46,7 +49,7 @@ pub async fn download_file(
file_type: format,
} = download_data;
let response = reqwest::Client::new()
let response = CLIENT
.get(format!(
"{}/api/v1/download/{id}/{format}/",
&config::CONFIG.cache_server_url
@@ -92,7 +95,7 @@ pub async fn download_file_by_link(
filename: String,
link: String,
) -> Result<Option<DownloadFile>, Box<dyn std::error::Error + Send + Sync>> {
let response = reqwest::Client::new()
let response = CLIENT
.get(link)
.send()
.await?

View File

@@ -1,6 +1,7 @@
pub mod formatters;
pub mod types;
use once_cell::sync::Lazy;
use smartstring::alias::String as SmartString;
use serde::de::DeserializeOwned;
@@ -11,6 +12,10 @@ use crate::config;
use self::types::Empty;
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
fn get_allowed_langs_params(
allowed_langs: SmallVec<[SmartString; 3]>,
) -> Vec<(&'static str, SmartString)> {
@@ -27,7 +32,7 @@ async fn _make_request<T>(
where
T: DeserializeOwned,
{
let response = reqwest::Client::new()
let response = CLIENT
.get(format!("{}{}", &config::CONFIG.book_server_url, url))
.query(&params)
.header("Authorization", &config::CONFIG.book_server_api_key)

View File

@@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use reqwest::StatusCode;
use serde::Deserialize;
use serde_json::json;
@@ -8,6 +9,10 @@ use tracing::log;
use crate::{bots_manager::USER_LANGS_CACHE, config};
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
#[derive(Deserialize, Debug, Clone)]
pub struct Lang {
// pub id: u32,
@@ -28,7 +33,7 @@ pub struct UserSettings {
pub async fn get_user_settings(
user_id: UserId,
) -> Result<Option<UserSettings>, Box<dyn std::error::Error + Send + Sync>> {
let response = reqwest::Client::new()
let response = CLIENT
.get(format!(
"{}/users/{}",
&config::CONFIG.user_settings_url,
@@ -88,7 +93,7 @@ pub async fn create_or_update_user_settings(
"allowed_langs": allowed_langs.into_vec()
});
let response = reqwest::Client::new()
let response = CLIENT
.post(format!("{}/users/", &config::CONFIG.user_settings_url))
.body(body.to_string())
.header("Authorization", &config::CONFIG.user_settings_api_key)
@@ -101,7 +106,7 @@ pub async fn create_or_update_user_settings(
}
pub async fn get_langs() -> Result<Vec<Lang>, Box<dyn std::error::Error + Send + Sync>> {
let response = reqwest::Client::new()
let response = CLIENT
.get(format!("{}/languages/", &config::CONFIG.user_settings_url))
.header("Authorization", &config::CONFIG.user_settings_api_key)
.send()
@@ -114,7 +119,7 @@ pub async fn get_langs() -> Result<Vec<Lang>, Box<dyn std::error::Error + Send +
pub async fn update_user_activity(
user_id: UserId,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
reqwest::Client::new()
CLIENT
.post(format!(
"{}/users/{user_id}/update_activity",
&config::CONFIG.user_settings_url
@@ -131,7 +136,7 @@ pub async fn is_need_donate_notifications(
chat_id: ChatId,
is_private: bool,
) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
let response = reqwest::Client::new()
let response = CLIENT
.get(format!(
"{}/donate_notifications/{chat_id}/is_need_send?is_private={is_private}",
&config::CONFIG.user_settings_url
@@ -147,7 +152,7 @@ pub async fn is_need_donate_notifications(
pub async fn mark_donate_notification_sent(
chat_id: ChatId,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
reqwest::Client::new()
CLIENT
.post(format!(
"{}/donate_notifications/{chat_id}",
&config::CONFIG.user_settings_url

View File

@@ -1,7 +1,12 @@
use once_cell::sync::Lazy;
use serde::Deserialize;
use crate::config;
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
#[derive(Deserialize, Debug, PartialEq, Clone, Copy)]
pub enum BotCache {
#[serde(rename = "original")]
@@ -20,8 +25,7 @@ pub struct BotData {
}
pub async fn get_bots() -> Result<Vec<BotData>, reqwest::Error> {
let client = reqwest::Client::new();
let response = client
let response = CLIENT
.get(&config::CONFIG.manager_url)
.header("Authorization", &config::CONFIG.manager_api_key)
.send()
@@ -35,8 +39,7 @@ pub async fn get_bots() -> Result<Vec<BotData>, reqwest::Error> {
pub async fn delete_bot(id: u32) -> Result<(), reqwest::Error> {
let client = reqwest::Client::new();
let response = client
let response = CLIENT
.delete(&format!("{}/{}/", config::CONFIG.manager_url, id))
.header("Authorization", &config::CONFIG.manager_api_key)
.send()