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