open_lark/core/
app_ticket_manager.rs1use serde::{Deserialize, Serialize};
2
3use crate::core::{
4 cache::QuickCache,
5 config::Config,
6 constants::{APPLY_APP_TICKET_PATH, APP_TICKET_KEY_PREFIX},
7 SDKResult,
8};
9
10#[derive(Debug)]
11pub struct AppTicketManager {
12 pub cache: QuickCache<String>,
13}
14
15impl Default for AppTicketManager {
16 fn default() -> Self {
17 Self::new()
18 }
19}
20
21impl AppTicketManager {
22 pub fn new() -> Self {
23 Self {
24 cache: QuickCache::new(),
25 }
26 }
27
28 pub fn set(&mut self, app_id: &str, value: &str, expire_time: i32) {
29 let key = app_ticket_key(app_id);
30 self.cache.set(&key, value.to_string(), expire_time);
31 }
32
33 pub async fn get(&self, config: &Config) -> Option<String> {
34 let key = app_ticket_key(&config.app_id);
35 match self.cache.get(&key) {
36 None => None,
37 Some(ticket) => {
38 if ticket.is_empty() {
39 apply_app_ticket(config).await.ok();
40 }
41
42 Some(ticket)
43 }
44 }
45 }
46}
47
48fn app_ticket_key(app_id: &str) -> String {
49 format!("{APP_TICKET_KEY_PREFIX}-{app_id}")
50}
51
52pub async fn apply_app_ticket(config: &Config) -> SDKResult<()> {
53 let url = format!("{}{}", config.base_url, APPLY_APP_TICKET_PATH);
54
55 let body = ResendAppTicketReq {
56 app_id: config.app_id.clone(),
57 app_secret: config.app_secret.clone(),
58 };
59
60 let _response = config.http_client.post(&url).json(&body).send().await?;
61
62 Ok(())
63}
64
65#[derive(Serialize, Deserialize)]
66struct ResendAppTicketReq {
67 app_id: String,
68 app_secret: String,
69}
70
71