highlevel-api 0.2.0

Unofficial Rust SDK for the GoHighLevel (HighLevel) API
Documentation
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenData {
    pub access_token: String,
    pub refresh_token: Option<String>,
    pub token_type: String,
    pub expires_in: u64,
    pub scope: Option<String>,
    pub user_type: Option<String>,
    pub company_id: Option<String>,
    pub location_id: Option<String>,
    /// Unix timestamp (seconds) when this token was issued, for expiry tracking.
    /// Serialized so restored tokens keep correct expiry. `None` means expiry is
    /// unchecked until `mark_issued_now()` is called.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub issued_at: Option<u64>,
}

impl TokenData {
    pub fn is_expired(&self) -> bool {
        let Some(issued_at) = self.issued_at else {
            return false;
        };
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        // Treat as expired 60 seconds before actual expiry
        now >= issued_at + self.expires_in.saturating_sub(60)
    }

    pub fn mark_issued_now(&mut self) {
        self.issued_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .ok();
    }
}