From 0af1fdc128ca9f84eb3f3c003b0170949ecfdfb8 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 24 Sep 2024 02:20:11 +0000 Subject: [PATCH] Add request timeout settings for Botapi Client Config --- src/push/telegram/botapi_client.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/push/telegram/botapi_client.rs b/src/push/telegram/botapi_client.rs index 98d8783..18e0f64 100644 --- a/src/push/telegram/botapi_client.rs +++ b/src/push/telegram/botapi_client.rs @@ -6,11 +6,16 @@ use crate::webclient::WebClient; use derive_builder::Builder; use serde::{Deserialize, Serialize}; use std::collections::HashMap; +use std::time::Duration; fn default_base() -> String { String::from("https://api.telegram.org") } +fn default_timeout() -> Option { + Some(120_000) +} + #[derive(Builder, Clone, Debug, Deserialize, Serialize)] /// Bot API Client Config pub struct BotapiClientConfig { @@ -20,6 +25,10 @@ pub struct BotapiClientConfig { pub base: String, /// Auth token pub token: String, + #[serde(default = "default_timeout")] + #[builder(default = "Some(120_000)")] + /// Requests timeout in milliseconds. Default: 120s. [None] will use global settings + pub timeout: Option, } pub struct BotapiClient { @@ -41,9 +50,16 @@ impl From<&str> for BotapiClientError { impl BotapiClient { pub fn new(cfg: &BotapiClientConfig) -> Self { + let client = WebClient::default(); + match &cfg.timeout { + Some(t) => { + client.set_timeout(Some(Duration::from_millis(t.clone()))); + } + None => {} + } Self { cfg: cfg.clone(), - client: WebClient::default(), + client, } }