1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Request (File Request) operations
//!
//! Requests are files that should be uploaded by a specific user or group.
//! They can be manually created/managed or automatically managed by automations.
use crate::{FilesClient, PaginationInfo, Result};
use serde::{Deserialize, Serialize};
use serde_json::json;
/// A Request entity (File Request)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestEntity {
/// Request ID
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<i64>,
/// Folder path
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
/// Source filename (if applicable)
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
/// Destination filename
#[serde(skip_serializing_if = "Option::is_none")]
pub destination: Option<String>,
/// ID of automation that created this request
#[serde(skip_serializing_if = "Option::is_none")]
pub automation_id: Option<i64>,
/// User making the request (if applicable)
#[serde(skip_serializing_if = "Option::is_none")]
pub user_display_name: Option<String>,
}
/// Handler for request operations
pub struct RequestHandler {
client: FilesClient,
}
impl RequestHandler {
/// Create a new request handler
pub fn new(client: FilesClient) -> Self {
Self { client }
}
/// List requests
///
/// # Arguments
/// * `cursor` - Pagination cursor
/// * `per_page` - Results per page (max 10,000)
/// * `path` - Filter by path
/// * `mine` - Only show requests for current user
///
/// # Returns
/// Tuple of (requests, pagination_info)
///
/// # Example
/// ```no_run
/// use files_sdk::{FilesClient, RequestHandler};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FilesClient::builder().api_key("key").build()?;
/// let handler = RequestHandler::new(client);
/// let (requests, pagination) = handler.list(None, None, None, None).await?;
/// # Ok(())
/// # }
/// ```
pub async fn list(
&self,
cursor: Option<&str>,
per_page: Option<i64>,
path: Option<&str>,
mine: Option<bool>,
) -> Result<(Vec<RequestEntity>, PaginationInfo)> {
let mut params = vec![];
if let Some(c) = cursor {
params.push(("cursor", c.to_string()));
}
if let Some(pp) = per_page {
params.push(("per_page", pp.to_string()));
}
if let Some(p) = path {
params.push(("path", p.to_string()));
}
if let Some(m) = mine {
params.push(("mine", m.to_string()));
}
let query = if params.is_empty() {
String::new()
} else {
format!(
"?{}",
params
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect::<Vec<_>>()
.join("&")
)
};
let response = self.client.get_raw(&format!("/requests{}", query)).await?;
let requests: Vec<RequestEntity> = serde_json::from_value(response)?;
let pagination = PaginationInfo {
cursor_next: None,
cursor_prev: None,
};
Ok((requests, pagination))
}
/// List requests for a specific folder path
///
/// # Arguments
/// * `path` - Folder path
/// * `cursor` - Pagination cursor
/// * `per_page` - Results per page
///
/// # Returns
/// Tuple of (requests, pagination_info)
pub async fn list_for_folder(
&self,
path: &str,
cursor: Option<&str>,
per_page: Option<i64>,
) -> Result<(Vec<RequestEntity>, PaginationInfo)> {
let mut params = vec![];
if let Some(c) = cursor {
params.push(("cursor", c.to_string()));
}
if let Some(pp) = per_page {
params.push(("per_page", pp.to_string()));
}
let query = if params.is_empty() {
String::new()
} else {
format!(
"?{}",
params
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect::<Vec<_>>()
.join("&")
)
};
let response = self
.client
.get_raw(&format!("/requests/folders/{}{}", path, query))
.await?;
let requests: Vec<RequestEntity> = serde_json::from_value(response)?;
let pagination = PaginationInfo {
cursor_next: None,
cursor_prev: None,
};
Ok((requests, pagination))
}
/// Create a new request
///
/// # Arguments
/// * `path` - Folder path where file should be uploaded (required)
/// * `destination` - Destination filename without extension (required)
/// * `user_ids` - Comma-separated list of user IDs to request from
/// * `group_ids` - Comma-separated list of group IDs to request from
///
/// # Returns
/// The created request
///
/// # Example
/// ```no_run
/// use files_sdk::{FilesClient, RequestHandler};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FilesClient::builder().api_key("key").build()?;
/// let handler = RequestHandler::new(client);
/// let request = handler.create(
/// "/uploads",
/// "monthly_report",
/// Some("123,456"),
/// None
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn create(
&self,
path: &str,
destination: &str,
user_ids: Option<&str>,
group_ids: Option<&str>,
) -> Result<RequestEntity> {
let mut body = json!({
"path": path,
"destination": destination,
});
if let Some(uids) = user_ids {
body["user_ids"] = json!(uids);
}
if let Some(gids) = group_ids {
body["group_ids"] = json!(gids);
}
let response = self.client.post_raw("/requests", body).await?;
Ok(serde_json::from_value(response)?)
}
/// Delete a request
///
/// # Arguments
/// * `id` - Request ID
///
/// # Example
/// ```no_run
/// use files_sdk::{FilesClient, RequestHandler};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FilesClient::builder().api_key("key").build()?;
/// let handler = RequestHandler::new(client);
/// handler.delete(123).await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete(&self, id: i64) -> Result<()> {
self.client.delete_raw(&format!("/requests/{}", id)).await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_handler_creation() {
let client = FilesClient::builder().api_key("test-key").build().unwrap();
let _handler = RequestHandler::new(client);
}
}