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