1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
use std::fmt::Display;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use crate::controllers::webhooks::{WebhookCreateBuilder, WebhookUpdateBuilder};
/// ```rust
/// #[cfg(test)]
/// mod tests {
/// use crate::{
/// webhooks::{Event, Resource, Webhook, WebhookStatus},
/// ApiClient, BatchObject, Entity,
/// };
///
/// #[tokio::test]
/// async fn test_create_update_bathc_update_delete_webhook() {
/// let client = ApiClient::from_env().unwrap();
/// let create = Webhook::create()
/// .name("test webhook")
/// .status(WebhookStatus::Disabled)
/// .resource(Resource::Customer)
/// .event(Event::Deleted)
/// .delivery_url("http://api.safira.club")
/// .build();
/// let created: Webhook = client.create(Entity::Webhook, create).await.unwrap();
/// assert_eq!(created.name, "test webhook");
/// let update = Webhook::update()
/// .name("test update webhook")
/// .build()
/// .unwrap();
/// let updated: Webhook = client
/// .update(Entity::Webhook, created.id, update)
/// .await
/// .unwrap();
/// assert_eq!(updated.name, "test update webhook");
/// let b_upd = Webhook::update()
/// .id(created.id)
/// .name("batch test webhook")
/// .build()
/// .unwrap();
/// let batch = BatchObject::builder().add_update(b_upd).build();
/// let batch_updated: BatchObject<Webhook> =
/// client.batch_update(Entity::Webhook, batch).await.unwrap();
/// assert!(batch_updated.update.is_some());
/// let _deleted: Webhook = client.delete(Entity::Webhook, created.id).await.unwrap();
/// }
/// #[tokio::test]
/// async fn test_list_all_webhooks_retrieve_webhook() {
/// let client = ApiClient::from_env().unwrap();
/// let result = client.list_all::<Webhook>(Entity::Webhook).await.unwrap();
/// assert!(!result.is_empty());
/// if let Some(first) = result.first() {
/// let wh: Webhook = client.retrieve(Entity::Webhook, first.id).await.unwrap();
/// assert_eq!(first.id, wh.id);
/// }
/// }
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Webhook {
/// Unique identifier for the resource.
pub id: i32,
/// A friendly name for the webhook.
pub name: String,
/// Webhook status. Options: active, paused and disabled. Default is active.
pub status: WebhookStatus,
/// Webhook topic.
pub topic: String,
/// Webhook resource.
pub resource: Resource,
/// Webhook event.
pub event: Event,
/// WooCommerce action names associated with the webhook.
pub hooks: Vec<String>,
/// The URL where the webhook payload is delivered.
pub delivery_url: String,
/// 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
pub secret: Option<String>,
/// The date the webhook was created, in the site's timezone.
pub date_created: NaiveDateTime,
/// The date the webhook was created, as GMT.
pub date_created_gmt: NaiveDateTime,
/// The date the webhook was last modified, in the site's timezone.
pub date_modified: Option<NaiveDateTime>,
/// The date the webhook was last modified, as GMT.
pub date_modified_gmt: Option<NaiveDateTime>,
}
#[derive(Default)]
pub struct NoResource;
#[derive(Default)]
pub struct NoEvent;
#[derive(Default)]
pub struct NoUrl;
impl Webhook {
pub fn create() -> WebhookCreateBuilder<NoResource, NoEvent, NoUrl> {
WebhookCreateBuilder::default()
}
pub fn update() -> WebhookUpdateBuilder {
WebhookUpdateBuilder::default()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WebhookStatus {
Active,
Paused,
Disabled,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Resource {
Coupon,
Customer,
Order,
Product,
}
impl Display for Resource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Resource::Coupon => write!(f, "coupon"),
Resource::Customer => write!(f, "customer"),
Resource::Order => write!(f, "order"),
Resource::Product => write!(f, "product"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Event {
Created,
Updated,
Deleted,
Restored,
}
impl Display for Event {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Event::Created => write!(f, "created"),
Event::Updated => write!(f, "updated"),
Event::Deleted => write!(f, "deleted"),
Event::Restored => write!(f, "restored"),
}
}
}