mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Fix for reuse connections
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
use once_cell::sync::Lazy;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use smartstring::alias::String as SmartString;
|
use smartstring::alias::String as SmartString;
|
||||||
|
|
||||||
@@ -5,6 +6,10 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
use crate::config;
|
use crate::config;
|
||||||
|
|
||||||
|
|
||||||
|
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum TaskObjectType {
|
pub enum TaskObjectType {
|
||||||
@@ -43,7 +48,7 @@ pub struct Task {
|
|||||||
pub async fn create_task(
|
pub async fn create_task(
|
||||||
data: CreateTaskData,
|
data: CreateTaskData,
|
||||||
) -> Result<Task, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<Task, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
Ok(reqwest::Client::new()
|
Ok(CLIENT
|
||||||
.post(format!("{}/api/", &config::CONFIG.batch_downloader_url))
|
.post(format!("{}/api/", &config::CONFIG.batch_downloader_url))
|
||||||
.body(serde_json::to_string(&data).unwrap())
|
.body(serde_json::to_string(&data).unwrap())
|
||||||
.header("Authorization", &config::CONFIG.batch_downloader_api_key)
|
.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>> {
|
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!(
|
.get(format!(
|
||||||
"{}/api/check_archive/{task_id}",
|
"{}/api/check_archive/{task_id}",
|
||||||
&config::CONFIG.batch_downloader_url
|
&config::CONFIG.batch_downloader_url
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use base64::{engine::general_purpose, Engine};
|
use base64::{engine::general_purpose, Engine};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
|
|
||||||
use crate::{bots::approved_bot::modules::download::callback_data::DownloadQueryData, bots_manager::BotCache, config};
|
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 mod types;
|
||||||
|
|
||||||
|
|
||||||
|
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
|
||||||
|
|
||||||
|
|
||||||
pub async fn get_cached_message(
|
pub async fn get_cached_message(
|
||||||
download_data: &DownloadQueryData,
|
download_data: &DownloadQueryData,
|
||||||
bot_cache: BotCache,
|
bot_cache: BotCache,
|
||||||
@@ -19,8 +23,7 @@ pub async fn get_cached_message(
|
|||||||
|
|
||||||
let is_need_copy = bot_cache == BotCache::Cache;
|
let is_need_copy = bot_cache == BotCache::Cache;
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let response = CLIENT
|
||||||
let response = client
|
|
||||||
.get(format!(
|
.get(format!(
|
||||||
"{}/api/v1/{id}/{format}/?copy={is_need_copy}",
|
"{}/api/v1/{id}/{format}/?copy={is_need_copy}",
|
||||||
&config::CONFIG.cache_server_url
|
&config::CONFIG.cache_server_url
|
||||||
@@ -46,7 +49,7 @@ pub async fn download_file(
|
|||||||
file_type: format,
|
file_type: format,
|
||||||
} = download_data;
|
} = download_data;
|
||||||
|
|
||||||
let response = reqwest::Client::new()
|
let response = CLIENT
|
||||||
.get(format!(
|
.get(format!(
|
||||||
"{}/api/v1/download/{id}/{format}/",
|
"{}/api/v1/download/{id}/{format}/",
|
||||||
&config::CONFIG.cache_server_url
|
&config::CONFIG.cache_server_url
|
||||||
@@ -92,7 +95,7 @@ pub async fn download_file_by_link(
|
|||||||
filename: String,
|
filename: String,
|
||||||
link: String,
|
link: String,
|
||||||
) -> Result<Option<DownloadFile>, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<Option<DownloadFile>, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let response = reqwest::Client::new()
|
let response = CLIENT
|
||||||
.get(link)
|
.get(link)
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
pub mod formatters;
|
pub mod formatters;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use smartstring::alias::String as SmartString;
|
use smartstring::alias::String as SmartString;
|
||||||
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
@@ -11,6 +12,10 @@ use crate::config;
|
|||||||
|
|
||||||
use self::types::Empty;
|
use self::types::Empty;
|
||||||
|
|
||||||
|
|
||||||
|
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
|
||||||
|
|
||||||
|
|
||||||
fn get_allowed_langs_params(
|
fn get_allowed_langs_params(
|
||||||
allowed_langs: SmallVec<[SmartString; 3]>,
|
allowed_langs: SmallVec<[SmartString; 3]>,
|
||||||
) -> Vec<(&'static str, SmartString)> {
|
) -> Vec<(&'static str, SmartString)> {
|
||||||
@@ -27,7 +32,7 @@ async fn _make_request<T>(
|
|||||||
where
|
where
|
||||||
T: DeserializeOwned,
|
T: DeserializeOwned,
|
||||||
{
|
{
|
||||||
let response = reqwest::Client::new()
|
let response = CLIENT
|
||||||
.get(format!("{}{}", &config::CONFIG.book_server_url, url))
|
.get(format!("{}{}", &config::CONFIG.book_server_url, url))
|
||||||
.query(¶ms)
|
.query(¶ms)
|
||||||
.header("Authorization", &config::CONFIG.book_server_api_key)
|
.header("Authorization", &config::CONFIG.book_server_api_key)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use once_cell::sync::Lazy;
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
@@ -8,6 +9,10 @@ use tracing::log;
|
|||||||
|
|
||||||
use crate::{bots_manager::USER_LANGS_CACHE, config};
|
use crate::{bots_manager::USER_LANGS_CACHE, config};
|
||||||
|
|
||||||
|
|
||||||
|
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Lang {
|
pub struct Lang {
|
||||||
// pub id: u32,
|
// pub id: u32,
|
||||||
@@ -28,7 +33,7 @@ pub struct UserSettings {
|
|||||||
pub async fn get_user_settings(
|
pub async fn get_user_settings(
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
) -> Result<Option<UserSettings>, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<Option<UserSettings>, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let response = reqwest::Client::new()
|
let response = CLIENT
|
||||||
.get(format!(
|
.get(format!(
|
||||||
"{}/users/{}",
|
"{}/users/{}",
|
||||||
&config::CONFIG.user_settings_url,
|
&config::CONFIG.user_settings_url,
|
||||||
@@ -88,7 +93,7 @@ pub async fn create_or_update_user_settings(
|
|||||||
"allowed_langs": allowed_langs.into_vec()
|
"allowed_langs": allowed_langs.into_vec()
|
||||||
});
|
});
|
||||||
|
|
||||||
let response = reqwest::Client::new()
|
let response = CLIENT
|
||||||
.post(format!("{}/users/", &config::CONFIG.user_settings_url))
|
.post(format!("{}/users/", &config::CONFIG.user_settings_url))
|
||||||
.body(body.to_string())
|
.body(body.to_string())
|
||||||
.header("Authorization", &config::CONFIG.user_settings_api_key)
|
.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>> {
|
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))
|
.get(format!("{}/languages/", &config::CONFIG.user_settings_url))
|
||||||
.header("Authorization", &config::CONFIG.user_settings_api_key)
|
.header("Authorization", &config::CONFIG.user_settings_api_key)
|
||||||
.send()
|
.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(
|
pub async fn update_user_activity(
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
reqwest::Client::new()
|
CLIENT
|
||||||
.post(format!(
|
.post(format!(
|
||||||
"{}/users/{user_id}/update_activity",
|
"{}/users/{user_id}/update_activity",
|
||||||
&config::CONFIG.user_settings_url
|
&config::CONFIG.user_settings_url
|
||||||
@@ -131,7 +136,7 @@ pub async fn is_need_donate_notifications(
|
|||||||
chat_id: ChatId,
|
chat_id: ChatId,
|
||||||
is_private: bool,
|
is_private: bool,
|
||||||
) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let response = reqwest::Client::new()
|
let response = CLIENT
|
||||||
.get(format!(
|
.get(format!(
|
||||||
"{}/donate_notifications/{chat_id}/is_need_send?is_private={is_private}",
|
"{}/donate_notifications/{chat_id}/is_need_send?is_private={is_private}",
|
||||||
&config::CONFIG.user_settings_url
|
&config::CONFIG.user_settings_url
|
||||||
@@ -147,7 +152,7 @@ pub async fn is_need_donate_notifications(
|
|||||||
pub async fn mark_donate_notification_sent(
|
pub async fn mark_donate_notification_sent(
|
||||||
chat_id: ChatId,
|
chat_id: ChatId,
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
reqwest::Client::new()
|
CLIENT
|
||||||
.post(format!(
|
.post(format!(
|
||||||
"{}/donate_notifications/{chat_id}",
|
"{}/donate_notifications/{chat_id}",
|
||||||
&config::CONFIG.user_settings_url
|
&config::CONFIG.user_settings_url
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
|
use once_cell::sync::Lazy;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::config;
|
use crate::config;
|
||||||
|
|
||||||
|
|
||||||
|
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, PartialEq, Clone, Copy)]
|
#[derive(Deserialize, Debug, PartialEq, Clone, Copy)]
|
||||||
pub enum BotCache {
|
pub enum BotCache {
|
||||||
#[serde(rename = "original")]
|
#[serde(rename = "original")]
|
||||||
@@ -20,8 +25,7 @@ pub struct BotData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_bots() -> Result<Vec<BotData>, reqwest::Error> {
|
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)
|
.get(&config::CONFIG.manager_url)
|
||||||
.header("Authorization", &config::CONFIG.manager_api_key)
|
.header("Authorization", &config::CONFIG.manager_api_key)
|
||||||
.send()
|
.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> {
|
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))
|
.delete(&format!("{}/{}/", config::CONFIG.manager_url, id))
|
||||||
.header("Authorization", &config::CONFIG.manager_api_key)
|
.header("Authorization", &config::CONFIG.manager_api_key)
|
||||||
.send()
|
.send()
|
||||||
|
|||||||
Reference in New Issue
Block a user