files_sdk/messages/
message_comments.rs1use crate::{Result, client::FilesClient, types::PaginationInfo};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize, Clone)]
5pub struct MessageCommentEntity {
6 #[serde(flatten)]
7 pub data: serde_json::Map<String, serde_json::Value>,
8}
9
10pub struct MessageCommentHandler {
11 client: FilesClient,
12}
13
14impl MessageCommentHandler {
15 pub fn new(client: FilesClient) -> Self {
16 Self { client }
17 }
18
19 pub async fn list(
20 &self,
21 user_id: Option<i64>,
22 cursor: Option<String>,
23 per_page: Option<i64>,
24 ) -> Result<(Vec<MessageCommentEntity>, PaginationInfo)> {
25 let mut endpoint = "/message_comments".to_string();
26 let mut query_params = Vec::new();
27
28 if let Some(user_id) = user_id {
29 query_params.push(format!("user_id={}", user_id));
30 }
31
32 if let Some(cursor) = cursor {
33 query_params.push(format!("cursor={}", cursor));
34 }
35
36 if let Some(per_page) = per_page {
37 query_params.push(format!("per_page={}", per_page));
38 }
39
40 if !query_params.is_empty() {
41 endpoint.push('?');
42 endpoint.push_str(&query_params.join("&"));
43 }
44
45 let url = format!("{}{}", self.client.inner.base_url, endpoint);
46 let response = reqwest::Client::new()
47 .get(&url)
48 .header("X-FilesAPI-Key", &self.client.inner.api_key)
49 .send()
50 .await?;
51
52 let headers = response.headers().clone();
53 let pagination = PaginationInfo::from_headers(&headers);
54
55 let status = response.status();
56 if !status.is_success() {
57 return Err(crate::FilesError::ApiError {
58 code: status.as_u16(),
59 message: response.text().await.unwrap_or_default(),
60 });
61 }
62
63 let entities: Vec<MessageCommentEntity> = response.json().await?;
64 Ok((entities, pagination))
65 }
66
67 pub async fn get(&self, id: i64) -> Result<MessageCommentEntity> {
68 let endpoint = format!("/message_comments/{}", id);
69 let response = self.client.get_raw(&endpoint).await?;
70 Ok(serde_json::from_value(response)?)
71 }
72
73 pub async fn create(&self, params: serde_json::Value) -> Result<MessageCommentEntity> {
74 let response = self.client.post_raw("/message_comments", params).await?;
75 Ok(serde_json::from_value(response)?)
76 }
77
78 pub async fn update(&self, id: i64, params: serde_json::Value) -> Result<MessageCommentEntity> {
79 let endpoint = format!("/message_comments/{}", id);
80 let response = self.client.patch_raw(&endpoint, params).await?;
81 Ok(serde_json::from_value(response)?)
82 }
83
84 pub async fn delete(&self, id: i64) -> Result<()> {
85 let endpoint = format!("/message_comments/{}", id);
86 self.client.delete_raw(&endpoint).await?;
87 Ok(())
88 }
89}