ucare/
webhook.rs

1//! Holds all primitives and logic around the webhook resource.
2
3use std::fmt::Debug;
4
5use reqwest::Method;
6use serde::{Deserialize, Serialize};
7
8use crate::ucare::{encode_json, rest::Client, Result};
9
10/// Service is used to make calls to webhook API.
11pub struct Service<'a> {
12    client: &'a Client,
13}
14
15/// creates an instance of the webhook service
16pub fn new_svc(client: &Client) -> Service {
17    Service { client }
18}
19
20impl Service<'_> {
21    /// Returns a list of project webhooks
22    pub fn list(&self) -> Result<List> {
23        self.client
24            .call::<String, String, List>(Method::GET, format!("/webhooks/"), None, None)
25    }
26
27    /// Create and subscribe to webhook
28    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(&params)?;
33
34        self.client.call::<String, Vec<u8>, Info>(
35            Method::POST,
36            format!("/webhooks/"),
37            None,
38            Some(json),
39        )
40    }
41
42    /// Update webhook attributes.
43    pub fn update(&self, params: UpdateParams) -> Result<Info> {
44        let json = encode_json(&params)?;
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    /// Unsubscribe and delete webhook.
55    pub fn delete(&self, params: DeleteParams) -> Result<()> {
56        let json = encode_json(&params)?;
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
74/// List of webhooks returned
75pub type List = Vec<Info>;
76
77/// Webhook information
78#[derive(Deserialize, Debug)]
79pub struct Info {
80    /// Webhook ID
81    pub id: i32,
82    /// Webhook creation date-time
83    pub created: String,
84    /// Webhook update date-time
85    pub updated: String,
86    /// Webhook event
87    pub event: String,
88    /// Where webhook data will be POSTed
89    pub target_url: String,
90    /// Webhook project ID
91    pub project: i32,
92    /// Whether it is active
93    pub is_active: bool,
94}
95
96/// Params for creating webhook
97#[derive(Debug, Serialize)]
98pub struct CreateParams {
99    /// An event you subscribe to.
100    pub event: Event,
101    /// A URL that is triggered by an event, for example, a file upload. A target URL MUST be
102    /// unique for each project — event type combination.
103    pub target_url: String,
104    /// Marks a subscription as either active or not, defaults to true, otherwise false.
105    pub is_active: Option<bool>,
106}
107
108/// Events to subscribe for
109#[derive(Debug, Serialize)]
110pub enum Event {
111    /// Fires when file is uploaded
112    #[serde(rename = "file.uploaded")]
113    FileUploaded,
114}
115
116/// Params for updating webhook
117#[derive(Debug, Serialize)]
118pub struct UpdateParams {
119    /// Webhook ID
120    pub id: i32,
121    /// An event you subscribe to. Leave None if you don't want to change it
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub event: Option<Event>,
124    /// A URL that is triggered by an event, for example, a file upload. A target URL MUST be
125    /// unique for each project — event type combination. Leave it None if you don't want to change
126    /// it.
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub target_url: Option<String>,
129    /// Marks a subscription as either active or not, leave it None if you don't want to change it.
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub is_active: Option<bool>,
132}
133
134/// Params for deleting webhook
135#[derive(Debug, Serialize)]
136pub struct DeleteParams {
137    /// Webhook will be found and deleted by its target_url
138    pub target_url: String,
139}