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
//! The Slack API definition of `files.getUploadURLExternal`.
//!
//! <https://api.slack.com/methods/files.getUploadURLExternal>

use sfr_types as st;

pub use st::FilesGetUploadUrlExternalResponse;

use crate::{Client, Request};
use reqwest::multipart::Form;

/// The request object for <https://api.slack.com/methods/files.getUploadURLExternal>.
#[derive(Debug)]
pub struct FilesGetUploadUrlExternal(st::FilesGetUploadUrlExternalRequest);

/// The code indicating the request handled in this file.
const API_CODE: &str = "files.getUploadURLExternal";

impl FilesGetUploadUrlExternal {
    /// Requests `files.getUploadURLExternal` to Slack.
    async fn request_inner(
        self,
        client: &Client,
    ) -> Result<FilesGetUploadUrlExternalResponse, st::Error> {
        #[allow(clippy::missing_docs_in_private_items)] // https://github.com/rust-lang/rust-clippy/issues/13298
        const URL: &str = "https://slack.com/api/files.getUploadURLExternal";

        let form = self.form().await;
        tracing::debug!("form = {form:?}");

        let response = client
            .client()
            .post(URL)
            .bearer_auth(client.token())
            .multipart(form)
            .send()
            .await
            .map_err(|e| st::Error::failed_to_request_by_http(API_CODE, e))?;
        tracing::debug!("response = {response:?}");

        let body: serde_json::Value = response
            .json()
            .await
            .map_err(|e| st::Error::failed_to_read_json(API_CODE, e))?;
        tracing::debug!("body = {body:?}");

        body.try_into()
    }

    /// Creates [`Form`].
    async fn form(self) -> Form {
        let mut form = Form::new();

        form = form.text("filename", self.0.filename);
        form = form.text("length", self.0.length.to_string());

        if let Some(alt_txt) = &self.0.alt_txt {
            form = form.text("alt_txt", alt_txt.clone());
        }

        if let Some(snippet_type) = &self.0.snippet_type {
            form = form.text("snippet_type", snippet_type.clone());
        }

        form
    }
}

impl From<st::FilesGetUploadUrlExternalRequest> for FilesGetUploadUrlExternal {
    fn from(inner: st::FilesGetUploadUrlExternalRequest) -> Self {
        Self(inner)
    }
}

impl Request for FilesGetUploadUrlExternal {
    type Response = FilesGetUploadUrlExternalResponse;

    async fn request(self, client: &Client) -> Result<Self::Response, st::Error> {
        self.request_inner(client).await
    }
}