use crate::config::HttpConfig;
use crate::model::types::AuthToken;
use crate::sync_compat::Mutex;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct HttpSession {
config: Arc<HttpConfig>,
auth_token: Arc<Mutex<Option<AuthToken>>>,
}
impl HttpSession {
pub fn new(config: HttpConfig) -> Self {
Self {
config: Arc::new(config),
auth_token: Arc::new(Mutex::new(None)),
}
}
pub fn config(&self) -> &HttpConfig {
&self.config
}
pub async fn set_auth_token(&self, token: AuthToken) {
*self.auth_token.lock().await = Some(token);
}
pub async fn auth_token(&self) -> Option<AuthToken> {
self.auth_token.lock().await.clone()
}
pub async fn is_authenticated(&self) -> bool {
self.auth_token.lock().await.is_some()
}
pub async fn clear_auth_token(&self) {
*self.auth_token.lock().await = None;
}
pub async fn is_token_expired(&self) -> bool {
false
}
pub async fn authorization_header(&self) -> Option<String> {
if let Some(token) = self.auth_token().await {
Some(format!("{} {}", token.token_type, token.access_token))
} else {
None
}
}
}