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