twilight_http/request/application/interaction/
get_followup.rs

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