twilight_http/request/channel/webhook/
delete_webhook.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{self, AuditLogReason, Request, TryIntoRequest},
5    response::{marker::EmptyBody, Response, ResponseFuture},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::id::{marker::WebhookMarker, Id};
10use twilight_validate::request::{audit_reason as validate_audit_reason, ValidationError};
11
12struct DeleteWebhookParams<'a> {
13    token: Option<&'a str>,
14}
15
16/// Delete a webhook by its ID.
17#[must_use = "requests must be configured and executed"]
18pub struct DeleteWebhook<'a> {
19    fields: DeleteWebhookParams<'a>,
20    http: &'a Client,
21    id: Id<WebhookMarker>,
22    reason: Result<Option<&'a str>, ValidationError>,
23}
24
25impl<'a> DeleteWebhook<'a> {
26    pub(crate) const fn new(http: &'a Client, id: Id<WebhookMarker>) -> Self {
27        Self {
28            fields: DeleteWebhookParams { token: None },
29            http,
30            id,
31            reason: Ok(None),
32        }
33    }
34
35    /// Specify the token for auth, if not already authenticated with a Bot token.
36    pub const fn token(mut self, token: &'a str) -> Self {
37        self.fields.token = Some(token);
38
39        self
40    }
41}
42
43impl<'a> AuditLogReason<'a> for DeleteWebhook<'a> {
44    fn reason(mut self, reason: &'a str) -> Self {
45        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
46
47        self
48    }
49}
50
51impl IntoFuture for DeleteWebhook<'_> {
52    type Output = Result<Response<EmptyBody>, Error>;
53
54    type IntoFuture = ResponseFuture<EmptyBody>;
55
56    fn into_future(self) -> Self::IntoFuture {
57        let http = self.http;
58
59        match self.try_into_request() {
60            Ok(request) => http.request(request),
61            Err(source) => ResponseFuture::error(source),
62        }
63    }
64}
65
66impl TryIntoRequest for DeleteWebhook<'_> {
67    fn try_into_request(self) -> Result<Request, Error> {
68        let mut request = Request::builder(&Route::DeleteWebhook {
69            webhook_id: self.id.get(),
70            token: self.fields.token,
71        });
72
73        if let Some(reason) = self.reason.map_err(Error::validation)? {
74            request = request.headers(request::audit_header(reason)?);
75        }
76
77        request.build()
78    }
79}