Skip to main content

twilight_http/request/application/interaction/
get_followup.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::{
11    channel::Message,
12    id::{
13        Id,
14        marker::{ApplicationMarker, MessageMarker},
15    },
16};
17
18/// Get a followup message of an interaction, by its token and the message ID.
19///
20/// This endpoint is not bound to the application's global rate limit.
21///
22/// # Examples
23///
24/// ```no_run
25/// # #[tokio::main]
26/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
27/// use std::env;
28/// use twilight_http::{Client, request::AuditLogReason};
29/// use twilight_model::id::Id;
30///
31/// let client = Client::new(env::var("DISCORD_TOKEN")?);
32/// let application_id = Id::new(1);
33///
34/// let response = client
35///     .interaction(application_id)
36///     .followup("token here", Id::new(2))
37///     .await?;
38/// # Ok(()) }
39/// ```
40#[must_use = "requests must be configured and executed"]
41pub struct GetFollowup<'a> {
42    application_id: Id<ApplicationMarker>,
43    http: &'a Client,
44    message_id: Id<MessageMarker>,
45    interaction_token: &'a str,
46}
47
48impl<'a> GetFollowup<'a> {
49    pub(crate) const fn new(
50        http: &'a Client,
51        application_id: Id<ApplicationMarker>,
52        interaction_token: &'a str,
53        message_id: Id<MessageMarker>,
54    ) -> Self {
55        Self {
56            application_id,
57            http,
58            message_id,
59            interaction_token,
60        }
61    }
62}
63
64#[cfg(not(target_os = "wasi"))]
65impl IntoFuture for GetFollowup<'_> {
66    type Output = Result<Response<Message>, Error>;
67
68    type IntoFuture = ResponseFuture<Message>;
69
70    fn into_future(self) -> Self::IntoFuture {
71        let http = self.http;
72
73        match self.try_into_request() {
74            Ok(request) => http.request(request),
75            Err(source) => ResponseFuture::error(source),
76        }
77    }
78}
79
80impl TryIntoRequest for GetFollowup<'_> {
81    fn try_into_request(self) -> Result<Request, Error> {
82        Request::builder(&Route::GetFollowupMessage {
83            application_id: self.application_id.get(),
84            interaction_token: self.interaction_token,
85            thread_id: None,
86            message_id: self.message_id.get(),
87        })
88        .use_authorization_token(false)
89        .build()
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::GetFollowup;
96    use crate::{
97        client::Client,
98        request::{Request, TryIntoRequest},
99        routing::Route,
100    };
101    use static_assertions::assert_impl_all;
102    use std::error::Error;
103    use twilight_model::id::{
104        Id,
105        marker::{ApplicationMarker, MessageMarker},
106    };
107
108    assert_impl_all!(GetFollowup<'_>: Send, Sync);
109
110    #[test]
111    fn request() -> Result<(), Box<dyn Error>> {
112        const APPLICATION_ID: Id<ApplicationMarker> = Id::new(1);
113        const MESSAGE_ID: Id<MessageMarker> = Id::new(2);
114        const TOKEN: &str = "token";
115
116        let client = Client::new("token".to_owned());
117
118        let actual = client
119            .interaction(APPLICATION_ID)
120            .followup(TOKEN, MESSAGE_ID)
121            .try_into_request()?;
122        let expected = Request::builder(&Route::GetFollowupMessage {
123            application_id: APPLICATION_ID.get(),
124            interaction_token: TOKEN,
125            thread_id: None,
126            message_id: MESSAGE_ID.get(),
127        })
128        .use_authorization_token(false)
129        .build()?;
130
131        assert!(expected.body().is_none());
132        assert_eq!(expected.path(), actual.path());
133        assert_eq!(
134            expected.use_authorization_token(),
135            actual.use_authorization_token()
136        );
137
138        Ok(())
139    }
140}