rust_woocommerce/models/
webhooks.rs

1use std::fmt::Display;
2
3use crate::controllers::Entity;
4use chrono::NaiveDateTime;
5use serde::{Deserialize, Serialize};
6
7use crate::controllers::webhooks::{WebhookCreateBuilder, WebhookUpdateBuilder};
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Webhook {
10    /// Unique identifier for the resource.
11    pub id: i32,
12    /// A friendly name for the webhook.
13    pub name: String,
14    /// Webhook status. Options: active, paused and disabled. Default is active.
15    pub status: WebhookStatus,
16    /// Webhook topic.
17    pub topic: String,
18    /// Webhook resource.
19    pub resource: Resource,
20    /// Webhook event.
21    pub event: Event,
22    /// WooCommerce action names associated with the webhook.
23    pub hooks: Vec<String>,
24    /// The URL where the webhook payload is delivered.
25    pub delivery_url: String,
26    /// Secret key used to generate a hash of the delivered webhook and provided in the request headers. This will default is a MD5 hash from the current user's ID
27    pub secret: Option<String>,
28    /// The date the webhook was created, in the site's timezone.
29    pub date_created: NaiveDateTime,
30    /// The date the webhook was created, as GMT.
31    pub date_created_gmt: NaiveDateTime,
32    /// The date the webhook was last modified, in the site's timezone.
33    pub date_modified: Option<NaiveDateTime>,
34    /// The date the webhook was last modified, as GMT.
35    pub date_modified_gmt: Option<NaiveDateTime>,
36}
37impl Entity for Webhook {
38    fn endpoint() -> String {
39        String::from("webhooks/")
40    }
41
42    fn child_endpoint(parent_id: i32) -> String {
43        let _ = parent_id;
44        String::new()
45    }
46}
47#[derive(Default)]
48pub struct NoResource;
49#[derive(Default)]
50pub struct NoEvent;
51#[derive(Default)]
52pub struct NoUrl;
53impl Webhook {
54    pub fn create() -> WebhookCreateBuilder<NoResource, NoEvent, NoUrl> {
55        WebhookCreateBuilder::default()
56    }
57    pub fn update() -> WebhookUpdateBuilder {
58        WebhookUpdateBuilder::default()
59    }
60}
61#[derive(Debug, Clone, Serialize, Deserialize)]
62#[serde(rename_all = "lowercase")]
63pub enum WebhookStatus {
64    Active,
65    Paused,
66    Disabled,
67}
68#[derive(Debug, Clone, Serialize, Deserialize)]
69#[serde(rename_all = "lowercase")]
70pub enum Resource {
71    Coupon,
72    Customer,
73    Order,
74    Product,
75}
76impl Display for Resource {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        match self {
79            Resource::Coupon => write!(f, "coupon"),
80            Resource::Customer => write!(f, "customer"),
81            Resource::Order => write!(f, "order"),
82            Resource::Product => write!(f, "product"),
83        }
84    }
85}
86#[derive(Debug, Clone, Serialize, Deserialize)]
87#[serde(rename_all = "lowercase")]
88pub enum Event {
89    Created,
90    Updated,
91    Deleted,
92    Restored,
93}
94impl Display for Event {
95    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96        match self {
97            Event::Created => write!(f, "created"),
98            Event::Updated => write!(f, "updated"),
99            Event::Deleted => write!(f, "deleted"),
100            Event::Restored => write!(f, "restored"),
101        }
102    }
103}