Skip to main content

twilight_http/request/channel/webhook/
get_webhook.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::Webhook,
12    id::{Id, marker::WebhookMarker},
13};
14
15struct GetWebhookFields<'a> {
16    token: Option<&'a str>,
17}
18
19/// Get a webhook by ID.
20#[must_use = "requests must be configured and executed"]
21pub struct GetWebhook<'a> {
22    fields: GetWebhookFields<'a>,
23    http: &'a Client,
24    id: Id<WebhookMarker>,
25}
26
27impl<'a> GetWebhook<'a> {
28    pub(crate) const fn new(http: &'a Client, id: Id<WebhookMarker>) -> Self {
29        Self {
30            fields: GetWebhookFields { token: None },
31            http,
32            id,
33        }
34    }
35
36    /// Specify the token for auth, if not already authenticated with a Bot
37    /// token.
38    pub const fn token(mut self, token: &'a str) -> Self {
39        self.fields.token = Some(token);
40
41        self
42    }
43}
44
45#[cfg(not(target_os = "wasi"))]
46impl IntoFuture for GetWebhook<'_> {
47    type Output = Result<Response<Webhook>, Error>;
48
49    type IntoFuture = ResponseFuture<Webhook>;
50
51    fn into_future(self) -> Self::IntoFuture {
52        let http = self.http;
53
54        match self.try_into_request() {
55            Ok(request) => http.request(request),
56            Err(source) => ResponseFuture::error(source),
57        }
58    }
59}
60
61impl TryIntoRequest for GetWebhook<'_> {
62    fn try_into_request(self) -> Result<Request, Error> {
63        let mut request = Request::builder(&Route::GetWebhook {
64            token: self.fields.token,
65            webhook_id: self.id.get(),
66        });
67
68        let use_webhook_token = self.fields.token.is_some();
69
70        // If a webhook token has been configured, then we don't need to use
71        // the client's authorization token.
72        if use_webhook_token {
73            request = request.use_authorization_token(false);
74        }
75
76        request.build()
77    }
78}