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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// @generated by xtask/generate_services.py — do not edit by hand.
//! `medias` — typed methods for all 7 API v3 operations
//! in this module.
//!
//! Access via [`Ghl::v3`](crate::Ghl::v3)`().medias()`. These endpoints send `Version: v3`.
//!
//! Request and response types come from [`ghl_models::v3::medias`](https://docs.rs/ghl-models/latest/ghl_models/v3/medias/); every endpoint is also documented in the
//! [`medias` API reference](https://github.com/Shahroz/ghl-rs/blob/main/docs/api/medias.md).
//!
//! Enable with `features = ["medias"]`.
#![allow(clippy::too_many_arguments)]
use crate::client::Ghl;
use crate::error::Result;
use ghl_models::v3::medias as models;
/// Typed access to the `medias` API v3 surface (7 operations). Obtained via
/// [`Ghl::v3`](crate::Ghl::v3)`().medias()`.
#[derive(Debug, Clone)]
pub struct MediasService {
pub(crate) client: Ghl,
}
impl MediasService {
pub(crate) fn new(client: Ghl) -> Self {
Self { client }
}
}
/// Query parameters for [`MediasService::get_list_of_files_folders`].
#[derive(Debug, Clone, Default)]
pub struct GetListOfFilesFoldersParams {
/// Number of files to skip in listing
pub offset: Option<String>,
/// Number of files to show in the listing
pub limit: Option<String>,
/// Field to sorting the file listing by
/// Required by the API.
pub sort_by: String,
/// Direction in which file needs to be sorted
/// Required by the API.
pub sort_order: String,
/// Type
/// Required by the API.
pub type_: String,
/// Query text
pub query: Option<String>,
/// AltType
/// Allowed values: `location`.
/// Required by the API.
pub alt_type: String,
/// location Id
/// Required by the API.
pub alt_id: String,
/// parent id or folder id
pub parent_id: Option<String>,
/// Fetch all files or folders
pub fetch_all: Option<String>,
}
impl GetListOfFilesFoldersParams {
/// Start from the parameters the API requires.
pub fn new(
sort_by: impl Into<String>,
sort_order: impl Into<String>,
type_: impl Into<String>,
alt_type: impl Into<String>,
alt_id: impl Into<String>,
) -> Self {
Self {
sort_by: sort_by.into(),
sort_order: sort_order.into(),
type_: type_.into(),
alt_type: alt_type.into(),
alt_id: alt_id.into(),
..Default::default()
}
}
/// Number of files to skip in listing
pub fn offset(mut self, v: impl Into<String>) -> Self {
self.offset = Some(v.into());
self
}
/// Number of files to show in the listing
pub fn limit(mut self, v: impl Into<String>) -> Self {
self.limit = Some(v.into());
self
}
/// Query text
pub fn query(mut self, v: impl Into<String>) -> Self {
self.query = Some(v.into());
self
}
/// parent id or folder id
pub fn parent_id(mut self, v: impl Into<String>) -> Self {
self.parent_id = Some(v.into());
self
}
/// Fetch all files or folders
pub fn fetch_all(mut self, v: impl Into<String>) -> Self {
self.fetch_all = Some(v.into());
self
}
fn to_query(&self) -> Vec<(String, String)> {
let mut q: Vec<(String, String)> = vec![
("sortBy".into(), self.sort_by.clone()),
("sortOrder".into(), self.sort_order.clone()),
("type".into(), self.type_.clone()),
("altType".into(), self.alt_type.clone()),
("altId".into(), self.alt_id.clone()),
];
if let Some(v) = &self.offset {
q.push(("offset".into(), v.to_string()));
}
if let Some(v) = &self.limit {
q.push(("limit".into(), v.to_string()));
}
if let Some(v) = &self.query {
q.push(("query".into(), v.to_string()));
}
if let Some(v) = &self.parent_id {
q.push(("parentId".into(), v.to_string()));
}
if let Some(v) = &self.fetch_all {
q.push(("fetchAll".into(), v.to_string()));
}
q
}
}
/// Query parameters for [`MediasService::delete_file_or_folder`].
#[derive(Debug, Clone, Default)]
pub struct DeleteFileOrFolderParams {
/// AltType
/// Allowed values: `location`.
/// Required by the API.
pub alt_type: String,
/// location Id
/// Required by the API.
pub alt_id: String,
}
impl DeleteFileOrFolderParams {
/// Start from the parameters the API requires.
pub fn new(alt_type: impl Into<String>, alt_id: impl Into<String>) -> Self {
Self {
alt_type: alt_type.into(),
alt_id: alt_id.into(),
}
}
fn to_query(&self) -> Vec<(String, String)> {
let q: Vec<(String, String)> = vec![
("altType".into(), self.alt_type.clone()),
("altId".into(), self.alt_id.clone()),
];
q
}
}
impl MediasService {
/// Bulk Delete / Trash Files or Folders
///
/// Soft-deletes or trashes multiple files and folders in a single request
///
/// `PUT /medias/delete-files`
pub async fn bulk_delete_trash_files_or_folders(
&self,
body: &models::DeleteMediaObjectsBodyParams,
) -> Result<serde_json::Value> {
let query = Vec::new();
self.client
.send_versioned(
reqwest::Method::PUT,
"/medias/delete-files",
&query,
Some(body),
Some("v3"),
)
.await
}
/// Get List of Files/ Folders
///
/// Fetches list of files and folders from the media storage
///
/// `GET /medias/files`
///
/// Requires scope: `medias.readonly`.
pub async fn get_list_of_files_folders(
&self,
params: &GetListOfFilesFoldersParams,
) -> Result<models::GetFilesResponseDTO> {
let query = params.to_query();
self.client
.send_versioned(
reqwest::Method::GET,
"/medias/files",
&query,
None::<&()>,
Some("v3"),
)
.await
}
/// Create Folder
///
/// Creates a new folder in the media storage
///
/// `POST /medias/folder`
pub async fn create_folder(
&self,
body: &models::CreateFolderParams,
) -> Result<models::FolderDTO> {
let query = Vec::new();
self.client
.send_versioned(
reqwest::Method::POST,
"/medias/folder",
&query,
Some(body),
Some("v3"),
)
.await
}
/// Bulk Update Files/ Folders
///
/// Updates metadata or status of multiple files and folders
///
/// `PUT /medias/update-files`
pub async fn bulk_update_files_folders(
&self,
body: &models::UpdateMediaObjects,
) -> Result<serde_json::Value> {
let query = Vec::new();
self.client
.send_versioned(
reqwest::Method::PUT,
"/medias/update-files",
&query,
Some(body),
Some("v3"),
)
.await
}
/// Upload File into Media Storage
///
/// If hosted is set to true then fileUrl is required. Else file is required. If adding
/// a file, maximum allowed is 25 MB. For video files, the maximum allowed size is 500
/// MB.
///
/// `POST /medias/upload-file`
///
/// Requires scope: `medias.write`.
pub async fn upload_file_into_media_storage(
&self,
body: &serde_json::Value,
) -> Result<models::UploadFileResponseDTO> {
let query = Vec::new();
self.client
.send_versioned(
reqwest::Method::POST,
"/medias/upload-file",
&query,
Some(body),
Some("v3"),
)
.await
}
/// Delete File or Folder
///
/// Deletes specific file or folder from the media storage
///
/// `DELETE /medias/{id}`
///
/// Requires scope: `medias.write`.
pub async fn delete_file_or_folder(
&self,
id: &str,
params: &DeleteFileOrFolderParams,
) -> Result<serde_json::Value> {
let path = format!("/medias/{}", crate::services::encode(id));
let query = params.to_query();
self.client
.send_versioned(
reqwest::Method::DELETE,
&path,
&query,
None::<&()>,
Some("v3"),
)
.await
}
/// Update File/ Folder
///
/// Updates a single file or folder by ID
///
/// `POST /medias/{id}`
pub async fn update_file_folder(
&self,
id: &str,
body: &models::UpdateObject,
) -> Result<serde_json::Value> {
let path = format!("/medias/{}", crate::services::encode(id));
let query = Vec::new();
self.client
.send_versioned(reqwest::Method::POST, &path, &query, Some(body), Some("v3"))
.await
}
}