files_sdk/files/
file_comments.rs1use crate::utils::encode_path;
11use crate::{FilesClient, Result};
12use serde::{Deserialize, Serialize};
13use serde_json::json;
14
15#[derive(Debug, Serialize, Deserialize, Clone)]
17pub struct FileCommentEntity {
18 pub id: Option<i64>,
20
21 pub body: Option<String>,
23
24 pub reactions: Option<Vec<FileCommentReactionEntity>>,
26}
27
28#[derive(Debug, Serialize, Deserialize, Clone)]
30pub struct FileCommentReactionEntity {
31 pub id: Option<i64>,
33
34 pub emoji: Option<String>,
36}
37
38#[derive(Debug, Clone)]
40pub struct FileCommentHandler {
41 client: FilesClient,
42}
43
44impl FileCommentHandler {
45 pub fn new(client: FilesClient) -> Self {
47 Self { client }
48 }
49
50 pub async fn list(&self, path: &str) -> Result<Vec<FileCommentEntity>> {
73 let encoded_path = encode_path(path);
74 let endpoint = format!("/file_comments/files{}", encoded_path);
75 let response = self.client.get_raw(&endpoint).await?;
76 Ok(serde_json::from_value(response)?)
77 }
78
79 pub async fn create(&self, path: &str, body: &str) -> Result<FileCommentEntity> {
86 let body_json = json!({
87 "body": body,
88 "path": path,
89 });
90
91 let response = self.client.post_raw("/file_comments", body_json).await?;
92 Ok(serde_json::from_value(response)?)
93 }
94
95 pub async fn update(&self, id: i64, body: &str) -> Result<FileCommentEntity> {
102 let body_json = json!({
103 "body": body,
104 });
105
106 let endpoint = format!("/file_comments/{}", id);
107 let response = self.client.patch_raw(&endpoint, body_json).await?;
108 Ok(serde_json::from_value(response)?)
109 }
110
111 pub async fn delete(&self, id: i64) -> Result<()> {
117 let endpoint = format!("/file_comments/{}", id);
118 self.client.delete_raw(&endpoint).await?;
119 Ok(())
120 }
121
122 pub async fn add_reaction(
129 &self,
130 file_comment_id: i64,
131 emoji: &str,
132 ) -> Result<FileCommentReactionEntity> {
133 let body = json!({
134 "file_comment_id": file_comment_id,
135 "emoji": emoji,
136 });
137
138 let response = self
139 .client
140 .post_raw("/file_comment_reactions", body)
141 .await?;
142 Ok(serde_json::from_value(response)?)
143 }
144
145 pub async fn delete_reaction(&self, id: i64) -> Result<()> {
151 let endpoint = format!("/file_comment_reactions/{}", id);
152 self.client.delete_raw(&endpoint).await?;
153 Ok(())
154 }
155}
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn test_handler_creation() {
163 let client = FilesClient::builder().api_key("test-key").build().unwrap();
164 let _handler = FileCommentHandler::new(client);
165 }
166}