sfr_slack_api/api/
files_info.rs

1//! The Slack API definition of `files.info`.
2//!
3//! <https://api.slack.com/methods/files.info>
4
5use sfr_types as st;
6
7pub use st::FilesInfoResponse;
8
9use crate::{Client, Request};
10use reqwest::multipart::Form;
11
12/// The request object for <https://api.slack.com/methods/files.info>.
13#[derive(Debug)]
14pub struct FilesInfo(st::FilesInfoRequest);
15
16/// The code indicating the request handled in this file.
17const API_CODE: &str = "files.info";
18
19impl FilesInfo {
20    /// Requests `files.info` to Slack.
21    async fn request_inner(self, client: &Client) -> Result<FilesInfoResponse, st::Error> {
22        #[allow(clippy::missing_docs_in_private_items)] // https://github.com/rust-lang/rust-clippy/issues/13298
23        const URL: &str = "https://slack.com/api/files.info";
24
25        let form = self
26            .form()
27            .await
28            .map_err(|e| e.stack("failed to create `form`"))?;
29        tracing::debug!("form = {form:?}");
30
31        let response = client
32            .client()
33            .post(URL)
34            .bearer_auth(client.token())
35            .multipart(form)
36            .send()
37            .await
38            .map_err(|e| st::Error::failed_to_request_by_http(API_CODE, e))?;
39        tracing::debug!("response = {response:?}");
40
41        let body: serde_json::Value = response
42            .json()
43            .await
44            .map_err(|e| st::Error::failed_to_read_json(API_CODE, e))?;
45        tracing::debug!("body = {body:?}");
46
47        body.try_into()
48    }
49
50    /// Creates [`Form`].
51    async fn form(self) -> Result<Form, st::Error> {
52        let mut form = Form::new();
53
54        form = form.text("file", self.0.file);
55
56        if let Some(count) = self.0.count {
57            form = form.text("count", count.to_string());
58        }
59
60        if let Some(cursor) = self.0.cursor {
61            form = form.text("cursor", cursor);
62        }
63
64        if let Some(limit) = self.0.limit {
65            form = form.text("limit", limit.to_string());
66        }
67
68        if let Some(page) = self.0.page {
69            form = form.text("page", page.to_string());
70        }
71
72        Ok(form)
73    }
74}
75
76impl From<st::FilesInfoRequest> for FilesInfo {
77    fn from(inner: st::FilesInfoRequest) -> Self {
78        Self(inner)
79    }
80}
81
82impl Request for FilesInfo {
83    type Response = FilesInfoResponse;
84
85    async fn request(self, client: &Client) -> Result<Self::Response, st::Error> {
86        self.request_inner(client).await
87    }
88}