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