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

use sfr_types as st;

pub use st::FilesRemoteAddResponse;

use crate::{Client, Request};
use serde::Serialize;

/// The request object for <https://api.slack.com/methods/files.remote.add>.
#[derive(Serialize, Debug)]
pub struct FilesRemoteAdd(st::FilesRemoteAddRequest);

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

impl FilesRemoteAdd {
    /// Requests `files.remote.add` to Slack.
    async fn request_inner(&self, client: &Client) -> Result<FilesRemoteAddResponse, 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.remote.add";

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

        let response = client
            .client()
            .post(URL)
            .bearer_auth(client.token())
            .form(&self)
            .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()
    }
}

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

impl Request for FilesRemoteAdd {
    type Response = FilesRemoteAddResponse;

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