twilight_http/request/application/interaction/
delete_response.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{Request, TryIntoRequest},
5    response::{marker::EmptyBody, Response, ResponseFuture},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::id::{marker::ApplicationMarker, Id};
10
11/// Delete a followup message to an interaction, by its token and message ID.
12///
13/// This endpoint is not bound to the application's global rate limit.
14///
15/// # Examples
16///
17/// ```no_run
18/// # #[tokio::main]
19/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
20/// use std::env;
21/// use twilight_http::{request::AuditLogReason, Client};
22/// use twilight_model::id::Id;
23///
24/// let client = Client::new(env::var("DISCORD_TOKEN")?);
25/// let application_id = Id::new(1);
26///
27/// client
28///     .interaction(application_id)
29///     .delete_response("token here")
30///     .await?;
31/// # Ok(()) }
32/// ```
33#[must_use = "requests must be configured and executed"]
34pub struct DeleteResponse<'a> {
35    application_id: Id<ApplicationMarker>,
36    http: &'a Client,
37    token: &'a str,
38}
39
40impl<'a> DeleteResponse<'a> {
41    pub(crate) const fn new(
42        http: &'a Client,
43        application_id: Id<ApplicationMarker>,
44        token: &'a str,
45    ) -> Self {
46        Self {
47            application_id,
48            http,
49            token,
50        }
51    }
52}
53
54impl IntoFuture for DeleteResponse<'_> {
55    type Output = Result<Response<EmptyBody>, Error>;
56
57    type IntoFuture = ResponseFuture<EmptyBody>;
58
59    fn into_future(self) -> Self::IntoFuture {
60        let http = self.http;
61
62        match self.try_into_request() {
63            Ok(request) => http.request(request),
64            Err(source) => ResponseFuture::error(source),
65        }
66    }
67}
68
69impl TryIntoRequest for DeleteResponse<'_> {
70    fn try_into_request(self) -> Result<Request, Error> {
71        Request::builder(&Route::DeleteInteractionOriginal {
72            application_id: self.application_id.get(),
73            interaction_token: self.token,
74        })
75        .use_authorization_token(false)
76        .build()
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use crate::{client::Client, request::TryIntoRequest};
83    use std::error::Error;
84    use twilight_http_ratelimiting::Path;
85    use twilight_model::id::Id;
86
87    #[test]
88    fn delete_followup_message() -> Result<(), Box<dyn Error>> {
89        let application_id = Id::new(1);
90        let token = "foo".to_owned();
91
92        let client = Client::new(String::new());
93        let req = client
94            .interaction(application_id)
95            .delete_response(&token)
96            .try_into_request()?;
97
98        assert!(!req.use_authorization_token());
99        assert_eq!(
100            &Path::WebhooksIdTokenMessagesId(application_id.get(), token),
101            req.ratelimit_path()
102        );
103
104        Ok(())
105    }
106}