spectacles_rest/views/
webhook.rs

1use futures::Future;
2use reqwest::Method;
3use reqwest::r#async::multipart::{Form, Part};
4
5use spectacles_model::message::{ExecuteWebhookOptions, Message, ModifyWebhookOptions, Webhook};
6
7use crate::{Endpoint, Error, RestClient};
8
9/// A view for managing Discord webhooks.
10pub struct WebhookView {
11    id: u64,
12    client: RestClient,
13}
14
15impl WebhookView {
16    pub(crate) fn new(id: u64, client: RestClient) -> Self {
17        Self {
18            id,
19            client,
20        }
21    }
22
23    /// Returns a webhook object for the provided user ID.
24    pub fn get(&self) -> impl Future<Item=Webhook, Error=Error> {
25        self.client.request(Endpoint::new(Method::GET, format!("/webhooks/{}", self.id)))
26    }
27
28    /// Similar to [`method.get.html`], but accepts a webhook token. The returned webhook does not have a User object.
29    pub fn get_with_token(&self, token: &str) -> impl Future<Item=Webhook, Error=Error> {
30        self.client.request(Endpoint::new(
31            Method::GET,
32            format!("/webhooks/{}/{}", self.id, token),
33        ))
34    }
35
36    /// Modifies the webhook at the provided webhook ID.
37    /// This endpoint requires the `MANAGE_WEBHOOKS` permission.
38    pub fn modify(&self, opts: ModifyWebhookOptions) -> impl Future<Item=Webhook, Error=Error> {
39        self.client.request(Endpoint::new(
40            Method::PATCH,
41            format!("/webhooks/{}", self.id),
42        ).json(opts))
43    }
44
45    /// Similar to [`method.modify.html`], but accepts a webhook token. The returned webhook does not have a User object.
46    pub fn modify_with_token(&self, token: &str, opts: ModifyWebhookOptions) -> impl Future<Item=Webhook, Error=Error> {
47        self.client.request(Endpoint::new(
48            Method::PATCH,
49            format!("/webhooks/{}/{}", self.id, token),
50        ).json(opts))
51    }
52
53    /// Permanently deletes this webhook.
54    pub fn delete(&self) -> impl Future<Item=(), Error=Error> {
55        self.client.request_empty(Endpoint::new(
56            Method::DELETE,
57            format!("/webhooks/{}", self.id),
58        ))
59    }
60
61    /// Similar to [`method.delete.html`], but accepts a webhook token.
62    pub fn delete_with_token(&self, token: &str) -> impl Future<Item=(), Error=Error> {
63        self.client.request_empty(Endpoint::new(
64            Method::DELETE,
65            format!("/webhooks/{}/{}", self.id, token),
66        ))
67    }
68
69    /// Executes the provided webhook, with the provided options.
70    pub fn execute(&self, token: &str, opts: ExecuteWebhookOptions, wait: bool) -> impl Future<Item=Option<Message>, Error=Error> {
71        let endpt = Endpoint::new(Method::POST, format!("/webhooks/{}/{}", self.id, token));
72        let json = serde_json::to_string(&opts).expect("Failed to serialize webhook message");
73        if let Some((name, file)) = opts.file {
74            self.client.request(endpt.multipart(
75                Form::new()
76                    .part("file", Part::bytes(file).file_name(name))
77                    .part("payload_json", Part::text(json))
78            ).query(json!({ "wait": wait })))
79        } else {
80            self.client.request(endpt.json(opts).query(json!({
81                "wait": wait
82            })))
83        }
84    }
85}