Skip to main content

WebhookHandler

Trait WebhookHandler 

Source
pub trait WebhookHandler: Send + Sync {
    // Required method
    fn handle<'a>(
        &'a self,
        context: WebhookContext,
        payload: Value,
    ) -> BoxFuture<'a, Result<(), WebhookError>>;
}
Expand description

Trait for handling incoming webhook requests.

Implement this trait to define custom webhook handling logic. Handlers are invoked by WebhookRegistry::process() after webhook signature verification succeeds.

§Thread Safety

Handlers must be Send + Sync to allow sharing across async tasks.

§Example

use shopify_sdk::webhooks::{WebhookHandler, WebhookContext, WebhookError, BoxFuture};
use serde_json::Value;

struct OrderCreatedHandler;

impl WebhookHandler for OrderCreatedHandler {
    fn handle<'a>(
        &'a self,
        context: WebhookContext,
        payload: Value,
    ) -> BoxFuture<'a, Result<(), WebhookError>> {
        Box::pin(async move {
            // Access webhook metadata
            if let Some(shop) = context.shop_domain() {
                println!("Received webhook from: {}", shop);
            }

            // Process the payload
            if let Some(order_id) = payload.get("id") {
                println!("Order created: {}", order_id);
            }

            Ok(())
        })
    }
}

Required Methods§

Source

fn handle<'a>( &'a self, context: WebhookContext, payload: Value, ) -> BoxFuture<'a, Result<(), WebhookError>>

Handles an incoming webhook request.

§Arguments
  • context - Verified webhook metadata (topic, shop domain, etc.)
  • payload - The parsed JSON payload from the webhook body
§Returns

A boxed future that resolves to Ok(()) on success, or a WebhookError if handling fails.

Implementors§