files_sdk/users/sessions.rs
1//! Session management operations
2//!
3//! This module provides session management functionality including:
4//! - Create sessions (login with username/password)
5//! - Delete sessions (logout)
6//!
7//! Note: Most SDK users will use API key authentication instead of sessions.
8//! Sessions are primarily for user-facing applications that need login flows.
9
10use crate::{FilesClient, Result};
11use serde::{Deserialize, Serialize};
12use serde_json::json;
13
14/// Session entity from Files.com API
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct SessionEntity {
17 /// Session ID
18 #[serde(skip_serializing_if = "Option::is_none")]
19 pub id: Option<String>,
20
21 /// Language preference for this session
22 #[serde(skip_serializing_if = "Option::is_none")]
23 pub language: Option<String>,
24
25 /// Is this a read-only session
26 #[serde(skip_serializing_if = "Option::is_none")]
27 pub read_only: Option<bool>,
28
29 /// Allow insecure SFTP ciphers
30 #[serde(skip_serializing_if = "Option::is_none")]
31 pub sftp_insecure_ciphers: Option<bool>,
32}
33
34/// Handler for session operations
35#[derive(Debug, Clone)]
36pub struct SessionHandler {
37 client: FilesClient,
38}
39
40impl SessionHandler {
41 /// Creates a new SessionHandler
42 pub fn new(client: FilesClient) -> Self {
43 Self { client }
44 }
45
46 /// Create a new session (login)
47 ///
48 /// # Arguments
49 ///
50 /// * `username` - Username to sign in as
51 /// * `password` - Password for sign in
52 /// * `otp` - One-time password for 2FA (optional)
53 ///
54 /// # Examples
55 ///
56 /// ```rust,no_run
57 /// # use files_sdk::{FilesClient, SessionHandler};
58 /// # #[tokio::main]
59 /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
60 /// # let client = FilesClient::builder().api_key("key").build()?;
61 /// let handler = SessionHandler::new(client);
62 /// let session = handler.create("username", "password", None).await?;
63 /// println!("Session ID: {:?}", session.id);
64 /// # Ok(())
65 /// # }
66 /// ```
67 pub async fn create(
68 &self,
69 username: &str,
70 password: &str,
71 otp: Option<&str>,
72 ) -> Result<SessionEntity> {
73 let mut body = json!({
74 "username": username,
75 "password": password,
76 });
77
78 if let Some(o) = otp {
79 body["otp"] = json!(o);
80 }
81
82 let response = self.client.post_raw("/sessions", body).await?;
83 Ok(serde_json::from_value(response)?)
84 }
85
86 /// Delete a session (logout)
87 ///
88 /// Deletes the current session, effectively logging out the user.
89 ///
90 /// # Examples
91 ///
92 /// ```rust,no_run
93 /// # use files_sdk::{FilesClient, SessionHandler};
94 /// # #[tokio::main]
95 /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
96 /// # let client = FilesClient::builder().api_key("key").build()?;
97 /// let handler = SessionHandler::new(client);
98 /// handler.delete().await?;
99 /// println!("Logged out successfully");
100 /// # Ok(())
101 /// # }
102 /// ```
103 pub async fn delete(&self) -> Result<()> {
104 self.client.delete_raw("/sessions").await?;
105 Ok(())
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn test_handler_creation() {
115 let client = FilesClient::builder().api_key("test-key").build().unwrap();
116 let _handler = SessionHandler::new(client);
117 }
118}