deribit_http/session/
http_session.rs1use crate::config::HttpConfig;
4use crate::model::types::AuthToken;
5use std::sync::Arc;
6use tokio::sync::Mutex;
7
8#[derive(Debug, Clone)]
10pub struct HttpSession {
11 config: Arc<HttpConfig>,
12 auth_token: Arc<Mutex<Option<AuthToken>>>,
13}
14
15impl HttpSession {
16 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 pub fn config(&self) -> &HttpConfig {
26 &self.config
27 }
28
29 pub async fn set_auth_token(&self, token: AuthToken) {
31 *self.auth_token.lock().await = Some(token);
32 }
33
34 pub async fn auth_token(&self) -> Option<AuthToken> {
36 self.auth_token.lock().await.clone()
37 }
38
39 pub async fn is_authenticated(&self) -> bool {
41 self.auth_token.lock().await.is_some()
42 }
43
44 pub async fn clear_auth_token(&self) {
46 *self.auth_token.lock().await = None;
47 }
48
49 pub async fn is_token_expired(&self) -> bool {
51 false
55 }
56
57 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}