Skip to content

Commit

Permalink
Add request timeout settings for Botapi Client Config
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored Sep 24, 2024
1 parent 9cc031e commit 0af1fdc
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/push/telegram/botapi_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
Some(120_000)
}

#[derive(Builder, Clone, Debug, Deserialize, Serialize)]
/// Bot API Client Config
pub struct BotapiClientConfig {
Expand All @@ -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<u64>,
}

pub struct BotapiClient {
Expand All @@ -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,
}
}

Expand Down

0 comments on commit 0af1fdc

Please sign in to comment.