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