wechat-api-rs 0.1.0

A Rust SDK for WeChat Official Account and Mini Program APIs
Documentation
//! Authentication related functionality

use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};

/// Access token holder
#[derive(Debug, Clone)]
pub struct AccessToken {
    pub token: String,
    pub expires_at: u64,
}

impl AccessToken {
    pub fn new(token: String, expires_in: u64) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        Self {
            token,
            expires_at: now + expires_in,
        }
    }

    pub fn is_expired(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        now >= self.expires_at
    }
}

/// Access token request parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessTokenRequest {
    pub grant_type: String,
    pub appid: String,
    pub secret: String,
}

impl AccessTokenRequest {
    pub fn new(appid: String, secret: String) -> Self {
        Self {
            grant_type: "client_credential".to_string(),
            appid,
            secret,
        }
    }
}