twilight_http/request/channel/webhook/
execute_webhook_and_wait.rs

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