spectacles_rest/views/
webhook.rs1use 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
9pub 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 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 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 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 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 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 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 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}