mattermost_client/openapi/apis/
files_api.rs

1/*
2 * Mattermost API Reference
3 *
4 * There is also a work-in-progress [Postman API reference](https://documenter.getpostman.com/view/4508214/RW8FERUn).
5 *
6 * The version of the OpenAPI document: 4.0.0
7 * Contact: feedback@mattermost.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11use reqwest;
12
13use super::{configuration, Error};
14use super::ResponseContent;
15
16/// struct for passing parameters to the method [`get_file`]
17#[derive(Clone, Debug, Default)]
18pub struct GetFileParams {
19    /// The ID of the file to get
20    pub file_id: String,
21}
22
23/// struct for passing parameters to the method [`get_file_info`]
24#[derive(Clone, Debug, Default)]
25pub struct GetFileInfoParams {
26    /// The ID of the file info to get
27    pub file_id: String,
28}
29
30/// struct for passing parameters to the method [`get_file_link`]
31#[derive(Clone, Debug, Default)]
32pub struct GetFileLinkParams {
33    /// The ID of the file to get a link for
34    pub file_id: String,
35}
36
37/// struct for passing parameters to the method [`get_file_preview`]
38#[derive(Clone, Debug, Default)]
39pub struct GetFilePreviewParams {
40    /// The ID of the file to get
41    pub file_id: String,
42}
43
44/// struct for passing parameters to the method [`get_file_public`]
45#[derive(Clone, Debug, Default)]
46pub struct GetFilePublicParams {
47    /// The ID of the file to get
48    pub file_id: String,
49    /// File hash
50    pub h: String,
51}
52
53/// struct for passing parameters to the method [`get_file_thumbnail`]
54#[derive(Clone, Debug, Default)]
55pub struct GetFileThumbnailParams {
56    /// The ID of the file to get
57    pub file_id: String,
58}
59
60/// struct for passing parameters to the method [`search_files`]
61#[derive(Clone, Debug, Default)]
62pub struct SearchFilesParams {
63    /// Team GUID
64    pub team_id: String,
65    /// The search terms as inputed by the user. To search for files from a user include `from:someusername`, using a user's username. To search in a specific channel include `in:somechannel`, using the channel name (not the display name). To search for specific extensions included `ext:extension`.
66    pub terms: String,
67    /// Set to true if an Or search should be performed vs an And search.
68    pub is_or_search: bool,
69    /// Offset from UTC of user timezone for date searches.
70    pub time_zone_offset: Option<i32>,
71    /// Set to true if deleted channels should be included in the search. (archived channels)
72    pub include_deleted_channels: Option<bool>,
73    /// The page to select. (Only works with Elasticsearch)
74    pub page: Option<i32>,
75    /// The number of posts per page. (Only works with Elasticsearch)
76    pub per_page: Option<i32>,
77}
78
79/// struct for passing parameters to the method [`upload_file`]
80#[derive(Clone, Debug, Default)]
81pub struct UploadFileParams {
82    /// The ID of the channel that this file will be uploaded to
83    pub channel_id: Option<String>,
84    /// The name of the file to be uploaded
85    pub filename: Option<String>,
86    /// A file to be uploaded
87    pub files: Option<std::path::PathBuf>,
88    /// The ID of the channel that this file will be uploaded to
89    pub channel_id2: Option<String>,
90    /// A unique identifier for the file that will be returned in the response
91    pub client_ids: Option<String>,
92}
93
94/// struct for typed errors of method [`get_file`]
95#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
96#[serde(untagged)]
97pub enum GetFileError {
98    Status400(crate::openapi::models::AppError),
99    Status401(crate::openapi::models::AppError),
100    Status403(crate::openapi::models::AppError),
101    Status404(crate::openapi::models::AppError),
102    Status501(crate::openapi::models::AppError),
103    UnknownValue(serde_json::Value),
104}
105
106/// struct for typed errors of method [`get_file_info`]
107#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
108#[serde(untagged)]
109pub enum GetFileInfoError {
110    Status400(crate::openapi::models::AppError),
111    Status401(crate::openapi::models::AppError),
112    Status403(crate::openapi::models::AppError),
113    Status404(crate::openapi::models::AppError),
114    Status501(crate::openapi::models::AppError),
115    UnknownValue(serde_json::Value),
116}
117
118/// struct for typed errors of method [`get_file_link`]
119#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
120#[serde(untagged)]
121pub enum GetFileLinkError {
122    Status400(crate::openapi::models::AppError),
123    Status401(crate::openapi::models::AppError),
124    Status403(crate::openapi::models::AppError),
125    Status404(crate::openapi::models::AppError),
126    Status501(crate::openapi::models::AppError),
127    UnknownValue(serde_json::Value),
128}
129
130/// struct for typed errors of method [`get_file_preview`]
131#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
132#[serde(untagged)]
133pub enum GetFilePreviewError {
134    Status400(crate::openapi::models::AppError),
135    Status401(crate::openapi::models::AppError),
136    Status403(crate::openapi::models::AppError),
137    Status404(crate::openapi::models::AppError),
138    Status501(crate::openapi::models::AppError),
139    UnknownValue(serde_json::Value),
140}
141
142/// struct for typed errors of method [`get_file_public`]
143#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
144#[serde(untagged)]
145pub enum GetFilePublicError {
146    Status400(crate::openapi::models::AppError),
147    Status401(crate::openapi::models::AppError),
148    Status403(crate::openapi::models::AppError),
149    Status404(crate::openapi::models::AppError),
150    Status501(crate::openapi::models::AppError),
151    UnknownValue(serde_json::Value),
152}
153
154/// struct for typed errors of method [`get_file_thumbnail`]
155#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
156#[serde(untagged)]
157pub enum GetFileThumbnailError {
158    Status400(crate::openapi::models::AppError),
159    Status401(crate::openapi::models::AppError),
160    Status403(crate::openapi::models::AppError),
161    Status404(crate::openapi::models::AppError),
162    Status501(crate::openapi::models::AppError),
163    UnknownValue(serde_json::Value),
164}
165
166/// struct for typed errors of method [`search_files`]
167#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
168#[serde(untagged)]
169pub enum SearchFilesError {
170    Status400(crate::openapi::models::AppError),
171    Status401(crate::openapi::models::AppError),
172    Status403(crate::openapi::models::AppError),
173    UnknownValue(serde_json::Value),
174}
175
176/// struct for typed errors of method [`upload_file`]
177#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
178#[serde(untagged)]
179pub enum UploadFileError {
180    Status400(crate::openapi::models::AppError),
181    Status401(crate::openapi::models::AppError),
182    Status403(crate::openapi::models::AppError),
183    Status413(crate::openapi::models::AppError),
184    Status501(crate::openapi::models::AppError),
185    UnknownValue(serde_json::Value),
186}
187
188/// Gets a file that has been uploaded previously. ##### Permissions Must have `read_channel` permission or be uploader of the file.
189pub async fn get_file(
190    configuration: &configuration::Configuration,
191    params: GetFileParams,
192) -> Result<(), Error<GetFileError>> {
193    let local_var_configuration = configuration;
194
195    // unbox the parameters
196    let file_id = params.file_id;
197
198    let local_var_client = &local_var_configuration.client;
199
200    let local_var_uri_str = format!(
201        "{}/api/v4/files/{file_id}",
202        local_var_configuration.base_path,
203        file_id = super::urlencode(file_id)
204    );
205    let mut local_var_req_builder =
206        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
207
208    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
209        local_var_req_builder =
210            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
211    }
212    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
213        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
214    };
215
216    let local_var_req = local_var_req_builder.build()?;
217    let local_var_resp = local_var_client.execute(local_var_req).await?;
218
219    let local_var_status = local_var_resp.status();
220    let local_var_content = local_var_resp.text().await?;
221
222    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
223        Ok(())
224    } else {
225        let local_var_entity: Option<GetFileError> = serde_json::from_str(&local_var_content).ok();
226        let local_var_error = ResponseContent {
227            status: local_var_status,
228            content: local_var_content,
229            entity: local_var_entity,
230        };
231        Err(Error::ResponseError(local_var_error))
232    }
233}
234
235/// Gets a file's info. ##### Permissions Must have `read_channel` permission or be uploader of the file.
236pub async fn get_file_info(
237    configuration: &configuration::Configuration,
238    params: GetFileInfoParams,
239) -> Result<crate::openapi::models::FileInfo, Error<GetFileInfoError>> {
240    let local_var_configuration = configuration;
241
242    // unbox the parameters
243    let file_id = params.file_id;
244
245    let local_var_client = &local_var_configuration.client;
246
247    let local_var_uri_str = format!(
248        "{}/api/v4/files/{file_id}/info",
249        local_var_configuration.base_path,
250        file_id = super::urlencode(file_id)
251    );
252    let mut local_var_req_builder =
253        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
254
255    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
256        local_var_req_builder =
257            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
258    }
259    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
260        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
261    };
262
263    let local_var_req = local_var_req_builder.build()?;
264    let local_var_resp = local_var_client.execute(local_var_req).await?;
265
266    let local_var_status = local_var_resp.status();
267    let local_var_content = local_var_resp.text().await?;
268
269    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
270        serde_json::from_str(&local_var_content).map_err(Error::from)
271    } else {
272        let local_var_entity: Option<GetFileInfoError> =
273            serde_json::from_str(&local_var_content).ok();
274        let local_var_error = ResponseContent {
275            status: local_var_status,
276            content: local_var_content,
277            entity: local_var_entity,
278        };
279        Err(Error::ResponseError(local_var_error))
280    }
281}
282
283/// Gets a public link for a file that can be accessed without logging into Mattermost. ##### Permissions Must have `read_channel` permission or be uploader of the file.
284pub async fn get_file_link(
285    configuration: &configuration::Configuration,
286    params: GetFileLinkParams,
287) -> Result<crate::openapi::models::GetFileLink200Response, Error<GetFileLinkError>> {
288    let local_var_configuration = configuration;
289
290    // unbox the parameters
291    let file_id = params.file_id;
292
293    let local_var_client = &local_var_configuration.client;
294
295    let local_var_uri_str = format!(
296        "{}/api/v4/files/{file_id}/link",
297        local_var_configuration.base_path,
298        file_id = super::urlencode(file_id)
299    );
300    let mut local_var_req_builder =
301        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
302
303    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
304        local_var_req_builder =
305            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
306    }
307    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
308        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
309    };
310
311    let local_var_req = local_var_req_builder.build()?;
312    let local_var_resp = local_var_client.execute(local_var_req).await?;
313
314    let local_var_status = local_var_resp.status();
315    let local_var_content = local_var_resp.text().await?;
316
317    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
318        serde_json::from_str(&local_var_content).map_err(Error::from)
319    } else {
320        let local_var_entity: Option<GetFileLinkError> =
321            serde_json::from_str(&local_var_content).ok();
322        let local_var_error = ResponseContent {
323            status: local_var_status,
324            content: local_var_content,
325            entity: local_var_entity,
326        };
327        Err(Error::ResponseError(local_var_error))
328    }
329}
330
331/// Gets a file's preview. ##### Permissions Must have `read_channel` permission or be uploader of the file.
332pub async fn get_file_preview(
333    configuration: &configuration::Configuration,
334    params: GetFilePreviewParams,
335) -> Result<(), Error<GetFilePreviewError>> {
336    let local_var_configuration = configuration;
337
338    // unbox the parameters
339    let file_id = params.file_id;
340
341    let local_var_client = &local_var_configuration.client;
342
343    let local_var_uri_str = format!(
344        "{}/api/v4/files/{file_id}/preview",
345        local_var_configuration.base_path,
346        file_id = super::urlencode(file_id)
347    );
348    let mut local_var_req_builder =
349        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
350
351    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
352        local_var_req_builder =
353            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
354    }
355    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
356        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
357    };
358
359    let local_var_req = local_var_req_builder.build()?;
360    let local_var_resp = local_var_client.execute(local_var_req).await?;
361
362    let local_var_status = local_var_resp.status();
363    let local_var_content = local_var_resp.text().await?;
364
365    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
366        Ok(())
367    } else {
368        let local_var_entity: Option<GetFilePreviewError> =
369            serde_json::from_str(&local_var_content).ok();
370        let local_var_error = ResponseContent {
371            status: local_var_status,
372            content: local_var_content,
373            entity: local_var_entity,
374        };
375        Err(Error::ResponseError(local_var_error))
376    }
377}
378
379/// ##### Permissions No permissions required.
380pub async fn get_file_public(
381    configuration: &configuration::Configuration,
382    params: GetFilePublicParams,
383) -> Result<(), Error<GetFilePublicError>> {
384    let local_var_configuration = configuration;
385
386    // unbox the parameters
387    let file_id = params.file_id;
388    let h = params.h;
389
390    let local_var_client = &local_var_configuration.client;
391
392    let local_var_uri_str = format!(
393        "{}/api/v4/files/{file_id}/public",
394        local_var_configuration.base_path,
395        file_id = super::urlencode(file_id)
396    );
397    let mut local_var_req_builder =
398        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
399
400    local_var_req_builder = local_var_req_builder.query(&[("h", &h.to_string())]);
401    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
402        local_var_req_builder =
403            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
404    }
405    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
406        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
407    };
408
409    let local_var_req = local_var_req_builder.build()?;
410    let local_var_resp = local_var_client.execute(local_var_req).await?;
411
412    let local_var_status = local_var_resp.status();
413    let local_var_content = local_var_resp.text().await?;
414
415    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
416        Ok(())
417    } else {
418        let local_var_entity: Option<GetFilePublicError> =
419            serde_json::from_str(&local_var_content).ok();
420        let local_var_error = ResponseContent {
421            status: local_var_status,
422            content: local_var_content,
423            entity: local_var_entity,
424        };
425        Err(Error::ResponseError(local_var_error))
426    }
427}
428
429/// Gets a file's thumbnail. ##### Permissions Must have `read_channel` permission or be uploader of the file.
430pub async fn get_file_thumbnail(
431    configuration: &configuration::Configuration,
432    params: GetFileThumbnailParams,
433) -> Result<(), Error<GetFileThumbnailError>> {
434    let local_var_configuration = configuration;
435
436    // unbox the parameters
437    let file_id = params.file_id;
438
439    let local_var_client = &local_var_configuration.client;
440
441    let local_var_uri_str = format!(
442        "{}/api/v4/files/{file_id}/thumbnail",
443        local_var_configuration.base_path,
444        file_id = super::urlencode(file_id)
445    );
446    let mut local_var_req_builder =
447        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
448
449    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
450        local_var_req_builder =
451            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
452    }
453    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
454        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
455    };
456
457    let local_var_req = local_var_req_builder.build()?;
458    let local_var_resp = local_var_client.execute(local_var_req).await?;
459
460    let local_var_status = local_var_resp.status();
461    let local_var_content = local_var_resp.text().await?;
462
463    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
464        Ok(())
465    } else {
466        let local_var_entity: Option<GetFileThumbnailError> =
467            serde_json::from_str(&local_var_content).ok();
468        let local_var_error = ResponseContent {
469            status: local_var_status,
470            content: local_var_content,
471            entity: local_var_entity,
472        };
473        Err(Error::ResponseError(local_var_error))
474    }
475}
476
477/// Search for files in a team based on file name, extention and file content (if file content extraction is enabled and supported for the files). __Minimum server version__: 5.34 ##### Permissions Must be authenticated and have the `view_team` permission.
478pub async fn search_files(
479    configuration: &configuration::Configuration,
480    params: SearchFilesParams,
481) -> Result<crate::openapi::models::FileInfoList, Error<SearchFilesError>> {
482    let local_var_configuration = configuration;
483
484    // unbox the parameters
485    let team_id = params.team_id;
486    let terms = params.terms;
487    let is_or_search = params.is_or_search;
488    let time_zone_offset = params.time_zone_offset;
489    let include_deleted_channels = params.include_deleted_channels;
490    let page = params.page;
491    let per_page = params.per_page;
492
493    let local_var_client = &local_var_configuration.client;
494
495    let local_var_uri_str = format!(
496        "{}/api/v4/teams/{team_id}/files/search",
497        local_var_configuration.base_path,
498        team_id = super::urlencode(team_id)
499    );
500    let mut local_var_req_builder =
501        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
502
503    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
504        local_var_req_builder =
505            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
506    }
507    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
508        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
509    };
510    let mut local_var_form = reqwest::multipart::Form::new();
511    local_var_form = local_var_form.text("terms", terms.to_string());
512    local_var_form = local_var_form.text("is_or_search", is_or_search.to_string());
513    if let Some(local_var_param_value) = time_zone_offset {
514        local_var_form = local_var_form.text("time_zone_offset", local_var_param_value.to_string());
515    }
516    if let Some(local_var_param_value) = include_deleted_channels {
517        local_var_form = local_var_form.text(
518            "include_deleted_channels",
519            local_var_param_value.to_string(),
520        );
521    }
522    if let Some(local_var_param_value) = page {
523        local_var_form = local_var_form.text("page", local_var_param_value.to_string());
524    }
525    if let Some(local_var_param_value) = per_page {
526        local_var_form = local_var_form.text("per_page", local_var_param_value.to_string());
527    }
528    local_var_req_builder = local_var_req_builder.multipart(local_var_form);
529
530    let local_var_req = local_var_req_builder.build()?;
531    let local_var_resp = local_var_client.execute(local_var_req).await?;
532
533    let local_var_status = local_var_resp.status();
534    let local_var_content = local_var_resp.text().await?;
535
536    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
537        serde_json::from_str(&local_var_content).map_err(Error::from)
538    } else {
539        let local_var_entity: Option<SearchFilesError> =
540            serde_json::from_str(&local_var_content).ok();
541        let local_var_error = ResponseContent {
542            status: local_var_status,
543            content: local_var_content,
544            entity: local_var_entity,
545        };
546        Err(Error::ResponseError(local_var_error))
547    }
548}
549
550/// Uploads a file that can later be attached to a post.  This request can either be a multipart/form-data request with a channel_id, files and optional client_ids defined in the FormData, or it can be a request with the channel_id and filename defined as query parameters with the contents of a single file in the body of the request.  Only multipart/form-data requests are supported by server versions up to and including 4.7. Server versions 4.8 and higher support both types of requests.  ##### Permissions Must have `upload_file` permission.
551pub async fn upload_file(
552    configuration: &configuration::Configuration,
553    params: UploadFileParams,
554) -> Result<crate::openapi::models::UploadFile201Response, Error<UploadFileError>> {
555    let local_var_configuration = configuration;
556
557    // unbox the parameters
558    let channel_id = params.channel_id;
559    let filename = params.filename;
560    let _files = params.files;
561    let channel_id2 = params.channel_id2;
562    let client_ids = params.client_ids;
563
564    let local_var_client = &local_var_configuration.client;
565
566    let local_var_uri_str = format!("{}/api/v4/files", local_var_configuration.base_path);
567    let mut local_var_req_builder =
568        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
569
570    if let Some(ref local_var_str) = channel_id {
571        local_var_req_builder =
572            local_var_req_builder.query(&[("channel_id", &local_var_str.to_string())]);
573    }
574    if let Some(ref local_var_str) = filename {
575        local_var_req_builder =
576            local_var_req_builder.query(&[("filename", &local_var_str.to_string())]);
577    }
578    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
579        local_var_req_builder =
580            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
581    }
582    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
583        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
584    };
585    let mut local_var_form = reqwest::multipart::Form::new();
586    // TODO: support file upload for 'files' parameter
587    if let Some(local_var_param_value) = channel_id2 {
588        local_var_form = local_var_form.text("channel_id", local_var_param_value.to_string());
589    }
590    if let Some(local_var_param_value) = client_ids {
591        local_var_form = local_var_form.text("client_ids", local_var_param_value.to_string());
592    }
593    local_var_req_builder = local_var_req_builder.multipart(local_var_form);
594
595    let local_var_req = local_var_req_builder.build()?;
596    let local_var_resp = local_var_client.execute(local_var_req).await?;
597
598    let local_var_status = local_var_resp.status();
599    let local_var_content = local_var_resp.text().await?;
600
601    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
602        serde_json::from_str(&local_var_content).map_err(Error::from)
603    } else {
604        let local_var_entity: Option<UploadFileError> =
605            serde_json::from_str(&local_var_content).ok();
606        let local_var_error = ResponseContent {
607            status: local_var_status,
608            content: local_var_content,
609            entity: local_var_entity,
610        };
611        Err(Error::ResponseError(local_var_error))
612    }
613}