Skip to main content

dingtalk_stream/client/
mod.rs

1//! DingTalk Stream Client
2//!
3//! The main client for connecting to DingTalk and handling messages
4
5use serde::{Deserialize, Serialize};
6use std::ops::Deref;
7use std::time::Duration;
8
9mod stream_;
10pub use stream_::*;
11use crate::frames::down_message::MessageTopic;
12
13/// Client configuration
14#[derive(Debug, Clone)]
15pub struct ClientConfig {
16    /// Whether to enable auto-reconnect
17    pub auto_reconnect: bool,
18    /// User agent string
19    pub ua: String,
20    /// Reconnect interval
21    pub reconnect_interval: Duration,
22    /// Keep alive interval
23    pub keep_alive_interval: Duration,
24}
25
26impl Default for ClientConfig {
27    fn default() -> Self {
28        Self {
29            auto_reconnect: true,
30            ua: format!("dingtalk-sdk-rust/{}", crate::VERSION),
31            reconnect_interval: Duration::from_secs(10),
32            keep_alive_interval: Duration::from_secs(60),
33        }
34    }
35}
36
37/// Subscription topic
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct Subscription {
40    /// Message type: EVENT or CALLBACK
41    pub topic: MessageTopic,
42    /// Topic path
43    #[serde(rename = "type")]
44    pub sub_type: String,
45}
46
47/// Connection response from gateway
48#[derive(Debug, Deserialize)]
49pub struct ConnectionResponse {
50    pub endpoint: String,
51    pub ticket: String,
52}
53
54/// Access token response
55#[derive(Debug, Deserialize)]
56pub struct AccessTokenResponse {
57    #[serde(rename = "accessToken")]
58    pub access_token: String,
59    #[serde(rename = "expireIn")]
60    pub expire_in: i64,
61}
62
63/// Access token cache
64#[derive(Clone)]
65struct AccessTokenCache {
66    token: AccessToken,
67    expire_time: i64,
68}
69#[derive(Clone)]
70pub struct AccessToken(String);
71
72impl Deref for AccessToken {
73    type Target = str;
74
75    fn deref(&self) -> &Self::Target {
76        &self.0
77    }
78}