1use std::fmt::Debug;
4
5use reqwest::Method;
6use serde::{Deserialize, Serialize};
7
8use crate::ucare::{encode_json, rest::Client, Result};
9
10pub struct Service<'a> {
12 client: &'a Client,
13}
14
15pub fn new_svc(client: &Client) -> Service {
17 Service { client }
18}
19
20impl Service<'_> {
21 pub fn list(&self) -> Result<List> {
23 self.client
24 .call::<String, String, List>(Method::GET, format!("/webhooks/"), None, None)
25 }
26
27 pub fn create(&self, mut params: CreateParams) -> Result<Info> {
29 if params.is_active.is_none() {
30 params.is_active = Some(true);
31 }
32 let json = encode_json(¶ms)?;
33
34 self.client.call::<String, Vec<u8>, Info>(
35 Method::POST,
36 format!("/webhooks/"),
37 None,
38 Some(json),
39 )
40 }
41
42 pub fn update(&self, params: UpdateParams) -> Result<Info> {
44 let json = encode_json(¶ms)?;
45
46 self.client.call::<String, Vec<u8>, Info>(
47 Method::PUT,
48 format!("/webhooks/{}/", params.id),
49 None,
50 Some(json),
51 )
52 }
53
54 pub fn delete(&self, params: DeleteParams) -> Result<()> {
56 let json = encode_json(¶ms)?;
57
58 let res = self.client.call::<String, Vec<u8>, String>(
59 Method::DELETE,
60 format!("/webhooks/unsubscribe/"),
61 None,
62 Some(json),
63 );
64 if let Err(err) = res {
65 if !err.to_string().contains("EOF") {
66 return Err(err);
67 }
68 }
69
70 Ok(())
71 }
72}
73
74pub type List = Vec<Info>;
76
77#[derive(Deserialize, Debug)]
79pub struct Info {
80 pub id: i32,
82 pub created: String,
84 pub updated: String,
86 pub event: String,
88 pub target_url: String,
90 pub project: i32,
92 pub is_active: bool,
94}
95
96#[derive(Debug, Serialize)]
98pub struct CreateParams {
99 pub event: Event,
101 pub target_url: String,
104 pub is_active: Option<bool>,
106}
107
108#[derive(Debug, Serialize)]
110pub enum Event {
111 #[serde(rename = "file.uploaded")]
113 FileUploaded,
114}
115
116#[derive(Debug, Serialize)]
118pub struct UpdateParams {
119 pub id: i32,
121 #[serde(skip_serializing_if = "Option::is_none")]
123 pub event: Option<Event>,
124 #[serde(skip_serializing_if = "Option::is_none")]
128 pub target_url: Option<String>,
129 #[serde(skip_serializing_if = "Option::is_none")]
131 pub is_active: Option<bool>,
132}
133
134#[derive(Debug, Serialize)]
136pub struct DeleteParams {
137 pub target_url: String,
139}