deribit_http/session/
http_session.rs

1//! HTTP session management
2
3use crate::config::HttpConfig;
4use crate::model::types::AuthToken;
5use std::sync::Arc;
6use tokio::sync::Mutex;
7
8/// HTTP session manager
9#[derive(Debug, Clone)]
10pub struct HttpSession {
11    config: Arc<HttpConfig>,
12    auth_token: Arc<Mutex<Option<AuthToken>>>,
13}
14
15impl HttpSession {
16    /// Create a new HTTP session
17    pub fn new(config: HttpConfig) -> Self {
18        Self {
19            config: Arc::new(config),
20            auth_token: Arc::new(Mutex::new(None)),
21        }
22    }
23
24    /// Get the configuration
25    pub fn config(&self) -> &HttpConfig {
26        &self.config
27    }
28
29    /// Set authentication token
30    pub async fn set_auth_token(&self, token: AuthToken) {
31        *self.auth_token.lock().await = Some(token);
32    }
33
34    /// Get authentication token
35    pub async fn auth_token(&self) -> Option<AuthToken> {
36        self.auth_token.lock().await.clone()
37    }
38
39    /// Check if session is authenticated
40    pub async fn is_authenticated(&self) -> bool {
41        self.auth_token.lock().await.is_some()
42    }
43
44    /// Clear authentication token
45    pub async fn clear_auth_token(&self) {
46        *self.auth_token.lock().await = None;
47    }
48
49    /// Check if token is expired
50    pub async fn is_token_expired(&self) -> bool {
51        // TODO: Implement token expiration check
52        // This would require storing the token creation time
53        // and comparing with expires_in value
54        false
55    }
56
57    /// Get authorization header value
58    pub async fn authorization_header(&self) -> Option<String> {
59        if let Some(token) = self.auth_token().await {
60            Some(format!("{} {}", token.token_type, token.access_token))
61        } else {
62            None
63        }
64    }
65}