files_sdk/storage/
locks.rs

1//! File locking operations
2
3use crate::{FilesClient, Result};
4use serde::{Deserialize, Serialize};
5use serde_json::json;
6
7/// Represents a file lock
8#[derive(Debug, Serialize, Deserialize, Clone)]
9pub struct LockEntity {
10    pub path: Option<String>,
11    pub timeout: Option<i64>,
12    pub depth: Option<String>,
13    pub owner: Option<String>,
14    pub scope: Option<String>,
15    pub token: Option<String>,
16    #[serde(rename = "type")]
17    pub lock_type: Option<String>,
18    pub user_id: Option<i64>,
19    pub username: Option<String>,
20}
21
22#[derive(Debug, Clone)]
23pub struct LockHandler {
24    client: FilesClient,
25}
26
27impl LockHandler {
28    pub fn new(client: FilesClient) -> Self {
29        Self { client }
30    }
31
32    pub async fn list_for_path(
33        &self,
34        path: &str,
35        include_children: bool,
36    ) -> Result<Vec<LockEntity>> {
37        let endpoint = format!("/locks{}?include_children={}", path, include_children);
38        let response = self.client.get_raw(&endpoint).await?;
39        Ok(serde_json::from_value(response)?)
40    }
41
42    pub async fn create(&self, path: &str, timeout: Option<i64>) -> Result<LockEntity> {
43        let mut body = json!({"path": path});
44        if let Some(t) = timeout {
45            body["timeout"] = json!(t);
46        }
47
48        let endpoint = format!("/locks{}", path);
49        let response = self.client.post_raw(&endpoint, body).await?;
50        Ok(serde_json::from_value(response)?)
51    }
52
53    pub async fn delete(&self, path: &str, token: &str) -> Result<()> {
54        let endpoint = format!("/locks{}?token={}", path, token);
55        self.client.delete_raw(&endpoint).await?;
56        Ok(())
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_handler_creation() {
66        let client = FilesClient::builder().api_key("test-key").build().unwrap();
67        let _handler = LockHandler::new(client);
68    }
69}