twilight_http/request/application/interaction/
create_response_with_response.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{Request, TryIntoRequest, attachment::AttachmentManager},
5    response::{Response, ResponseFuture},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::{
10    application::interaction::callback::InteractionCallbackResponse,
11    http::interaction::InteractionResponse,
12    id::{Id, marker::InteractionMarker},
13};
14
15/// Respond to an interaction, by its ID and token.
16///
17/// This endpoint is not bound to the application's global rate limit.
18#[must_use = "requests must be configured and executed"]
19pub struct CreateResponseWithResponse<'a> {
20    interaction_id: Id<InteractionMarker>,
21    interaction_token: &'a str,
22    response: &'a InteractionResponse,
23    http: &'a Client,
24}
25
26impl<'a> CreateResponseWithResponse<'a> {
27    pub(crate) const fn new(
28        http: &'a Client,
29        interaction_id: Id<InteractionMarker>,
30        interaction_token: &'a str,
31        response: &'a InteractionResponse,
32    ) -> Self {
33        Self {
34            interaction_id,
35            interaction_token,
36            response,
37            http,
38        }
39    }
40}
41
42impl IntoFuture for CreateResponseWithResponse<'_> {
43    type Output = Result<Response<InteractionCallbackResponse>, Error>;
44
45    type IntoFuture = ResponseFuture<InteractionCallbackResponse>;
46
47    fn into_future(self) -> Self::IntoFuture {
48        let http = self.http;
49
50        match self.try_into_request() {
51            Ok(request) => http.request(request),
52            Err(source) => ResponseFuture::error(source),
53        }
54    }
55}
56
57impl TryIntoRequest for CreateResponseWithResponse<'_> {
58    fn try_into_request(self) -> Result<Request, Error> {
59        let mut request = Request::builder(&Route::InteractionCallback {
60            interaction_id: self.interaction_id.get(),
61            interaction_token: self.interaction_token,
62            with_response: true,
63        });
64
65        // Interaction executions don't need the authorization token, only the
66        // interaction token.
67        request = request.use_authorization_token(false);
68
69        // Determine whether we need to use a multipart/form-data body or a JSON
70        // body.
71        if let Some(attachments) = self
72            .response
73            .data
74            .as_ref()
75            .and_then(|data| data.attachments.as_ref())
76        {
77            let fields = crate::json::to_vec(&self.response).map_err(Error::json)?;
78
79            let form = AttachmentManager::new()
80                .set_files(attachments.iter().collect())
81                .build_form(&fields);
82
83            request = request.form(form);
84        } else {
85            request = request.json(&self.response);
86        }
87
88        request.build()
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use crate::{client::Client, request::TryIntoRequest};
95    use std::error::Error;
96    use twilight_model::{
97        http::interaction::{InteractionResponse, InteractionResponseType},
98        id::Id,
99    };
100
101    #[test]
102    fn interaction_callback() -> Result<(), Box<dyn Error>> {
103        let application_id = Id::new(1);
104        let interaction_id = Id::new(2);
105        let token = "foo".to_owned().into_boxed_str();
106
107        let client = Client::new(String::new());
108
109        let response = InteractionResponse {
110            kind: InteractionResponseType::DeferredUpdateMessage,
111            data: None,
112        };
113
114        let req = client
115            .interaction(application_id)
116            .create_response(interaction_id, &token, &response)
117            .with_response()
118            .try_into_request()?;
119
120        assert!(!req.use_authorization_token());
121
122        Ok(())
123    }
124}