Skip to main content

twilight_http/request/channel/webhook/
execute_webhook_and_wait.rs

1use super::ExecuteWebhook;
2#[cfg(not(target_os = "wasi"))]
3use crate::response::{Response, ResponseFuture};
4use crate::{
5    client::Client,
6    error::Error,
7    request::{Request, TryIntoRequest},
8};
9use std::future::IntoFuture;
10use twilight_model::channel::Message;
11
12/// Execute a webhook, sending a message to its channel, and then wait for the
13/// message to be created.
14///
15/// # Examples
16///
17/// ```no_run
18/// use twilight_http::Client;
19/// use twilight_model::id::Id;
20///
21/// # #[tokio::main]
22/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
23/// let client = Client::new("my token".to_owned());
24/// let id = Id::new(432);
25///
26/// let message = client
27///     .execute_webhook(id, "webhook token")
28///     .content("Pinkie...")
29///     .wait()
30///     .await?
31///     .model()
32///     .await?;
33///
34/// println!("message id: {}", message.id);
35/// # Ok(()) }
36/// ```
37#[must_use = "requests must be configured and executed"]
38pub struct ExecuteWebhookAndWait<'a> {
39    http: &'a Client,
40    inner: ExecuteWebhook<'a>,
41}
42
43impl<'a> ExecuteWebhookAndWait<'a> {
44    pub(crate) const fn new(http: &'a Client, inner: ExecuteWebhook<'a>) -> Self {
45        Self { http, inner }
46    }
47}
48
49#[cfg(not(target_os = "wasi"))]
50impl IntoFuture for ExecuteWebhookAndWait<'_> {
51    type Output = Result<Response<Message>, Error>;
52
53    type IntoFuture = ResponseFuture<Message>;
54
55    fn into_future(self) -> Self::IntoFuture {
56        let http = self.http;
57
58        match self.try_into_request() {
59            Ok(request) => http.request(request),
60            Err(source) => ResponseFuture::error(source),
61        }
62    }
63}
64
65impl TryIntoRequest for ExecuteWebhookAndWait<'_> {
66    fn try_into_request(self) -> Result<Request, Error> {
67        self.inner.try_into_request()
68    }
69}