Skip to main content

twilight_http/request/channel/webhook/
delete_webhook.rs

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