#[non_exhaustive]pub struct WebhookContext {
pub webhook_name: String,
pub request_id: String,
pub idempotency_key: Option<String>,
/* private fields */
}Expand description
Context available to webhook handlers.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.webhook_name: String§request_id: String§idempotency_key: Option<String>Implementations§
Source§impl WebhookContext
impl WebhookContext
Sourcepub fn new(
webhook_name: String,
request_id: String,
headers: HashMap<String, String>,
db_pool: PgPool,
http_client: CircuitBreakerClient,
) -> Self
pub fn new( webhook_name: String, request_id: String, headers: HashMap<String, String>, db_pool: PgPool, http_client: CircuitBreakerClient, ) -> Self
Create a new webhook context.
Sourcepub fn with_transaction(
webhook_name: String,
request_id: String,
headers: HashMap<String, String>,
db_pool: PgPool,
tx: Transaction<'static, Postgres>,
http_client: CircuitBreakerClient,
) -> (Self, Arc<Mutex<Option<Transaction<'static, Postgres>>>>)
pub fn with_transaction( webhook_name: String, request_id: String, headers: HashMap<String, String>, db_pool: PgPool, tx: Transaction<'static, Postgres>, http_client: CircuitBreakerClient, ) -> (Self, Arc<Mutex<Option<Transaction<'static, Postgres>>>>)
Build a webhook context that dispatches jobs and workflows on tx.
The runtime opens the transaction, owns the handle, and commits or rolls back based on the handler’s result. Anything the handler dispatches lands on the same connection, so failed webhooks don’t leave orphaned jobs behind.
Sourcepub fn is_transactional(&self) -> bool
pub fn is_transactional(&self) -> bool
Whether dispatches will participate in an enclosing transaction.
Sourcepub fn with_kv(self, kv: Arc<dyn KvHandle>) -> Self
pub fn with_kv(self, kv: Arc<dyn KvHandle>) -> Self
Attach a KV store handle. Called by the runtime before handing the context to the handler.
Sourcepub fn with_idempotency_key(self, key: Option<String>) -> Self
pub fn with_idempotency_key(self, key: Option<String>) -> Self
Set idempotency key.
Sourcepub fn with_job_dispatch(self, dispatcher: Arc<dyn JobDispatch>) -> Self
pub fn with_job_dispatch(self, dispatcher: Arc<dyn JobDispatch>) -> Self
Set job dispatcher.
Sourcepub fn with_workflow_dispatch(
self,
dispatcher: Arc<dyn WorkflowDispatch>,
) -> Self
pub fn with_workflow_dispatch( self, dispatcher: Arc<dyn WorkflowDispatch>, ) -> Self
Set workflow dispatcher.
Sourcepub fn with_env_provider(self, provider: Arc<dyn EnvProvider>) -> Self
pub fn with_env_provider(self, provider: Arc<dyn EnvProvider>) -> Self
Set environment provider.
Sourcepub fn db_conn(&self) -> DbConn<'_>
pub fn db_conn(&self) -> DbConn<'_>
Get a DbConn for use in shared helper functions.
In transactional mode, returns a transaction-backed handle. Outside a transaction, returns a pool-backed handle.
Sourcepub async fn conn(&self) -> Result<ForgeConn<'_>>
pub async fn conn(&self) -> Result<ForgeConn<'_>>
Acquire a connection compatible with sqlx compile-time checked macros.
In transactional mode, returns a guard over the active transaction so the handler’s queries participate in the same transaction as its dispatches. Outside a transaction, acquires a fresh pool connection.
Sourcepub fn http(&self) -> HttpClient
pub fn http(&self) -> HttpClient
Get the HTTP client for external requests.
Sourcepub fn raw_http(&self) -> &Client
pub fn raw_http(&self) -> &Client
Get the raw reqwest client, bypassing circuit breaker execution.
pub fn set_http_timeout(&mut self, timeout: Option<Duration>)
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
Get a request header value.
Header names are case-insensitive.
Sourcepub async fn dispatch_job<T: Serialize>(
&self,
job_type: &str,
args: T,
) -> Result<Uuid>
pub async fn dispatch_job<T: Serialize>( &self, job_type: &str, args: T, ) -> Result<Uuid>
Dispatch a background job for async processing.
This is the recommended way to handle webhook events:
- Validate the webhook signature
- Dispatch a job to process the event
- Return 202 Accepted immediately
§Arguments
job_type- The registered name of the job typeargs- The arguments for the job (will be serialized to JSON)
§Returns
The UUID of the dispatched job, or an error if dispatch is not available.
Sourcepub async fn dispatch<J: ForgeJob>(&self, args: J::Args) -> Result<Uuid>
pub async fn dispatch<J: ForgeJob>(&self, args: J::Args) -> Result<Uuid>
Type-safe dispatch: resolves the job name from the type’s ForgeJob
impl and serializes the args at the call site.
Sourcepub async fn start_workflow<T: Serialize>(
&self,
workflow_name: &str,
input: T,
) -> Result<Uuid>
pub async fn start_workflow<T: Serialize>( &self, workflow_name: &str, input: T, ) -> Result<Uuid>
Start a workflow.
Trait Implementations§
Source§impl EnvAccess for WebhookContext
impl EnvAccess for WebhookContext
fn env_provider(&self) -> &dyn EnvProvider
fn env(&self, key: &str) -> Option<String>
fn env_or(&self, key: &str, default: &str) -> String
fn env_require(&self, key: &str) -> Result<String>
fn env_parse<T: FromStr>(&self, key: &str) -> Result<T>
Source§fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
fn env_contains(&self, key: &str) -> bool
Auto Trait Implementations§
impl Freeze for WebhookContext
impl !RefUnwindSafe for WebhookContext
impl Send for WebhookContext
impl Sync for WebhookContext
impl Unpin for WebhookContext
impl UnsafeUnpin for WebhookContext
impl !UnwindSafe for WebhookContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more