Documentation
¶
Overview ¶
Package webhooks verifies and reads Rendobar webhook deliveries and per-job callbacks.
body, _ := io.ReadAll(r.Body) // the exact bytes the signature covers
event, err := webhooks.Parse(body, r.Header, secret)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
payload, err := event.Payload()
switch data := payload.(type) {
case *webhooks.JobCompletedData:
fmt.Println(data.JobID, data.Output.File.URL)
default:
// an event this code does not handle, or one newer than this SDK
}
Index ¶
- Constants
- Variables
- func Verify(body []byte, header http.Header, secret string) bool
- func VerifyWith(body []byte, header http.Header, secret string, o Options) bool
- type BalanceDepletedData
- type BalanceLowData
- type Event
- type JobCancelledData
- type JobCancelledDataTiming
- type JobCompletedData
- type JobCompletedDataTiming
- type JobCreatedData
- type JobCreatedDataTiming
- type JobDeliveriesSettledData
- type JobDeliveryFailedData
- type JobDeliverySucceededData
- type JobFailedData
- type JobFailedDataTiming
- type JobStartedData
- type JobStartedDataTiming
- type Options
- type Payload
- type SettledDelivery
- type TestEventData
- type UnknownPayload
- type WebhookJobCost
- type WebhookJobStep
Constants ¶
const ( JobCreated rendobar.WebhookEvent = "job.created" // A job was accepted JobStarted rendobar.WebhookEvent = "job.started" // A job started running JobCompleted rendobar.WebhookEvent = "job.completed" // A job completed. `data.output` is the output, as GET /jobs/{id} returns it JobFailed rendobar.WebhookEvent = "job.failed" // A job failed. `data.error` says why JobCancelled rendobar.WebhookEvent = "job.cancelled" // A job was cancelled JobDeliverySucceeded rendobar.WebhookEvent = "job.delivery_succeeded" // One destination of a job received its output JobDeliveryFailed rendobar.WebhookEvent = "job.delivery_failed" // One destination of a job will not receive its output JobDeliveriesSettled rendobar.WebhookEvent = "job.deliveries_settled" // Every destination of a job has resolved BalanceLow rendobar.WebhookEvent = "balance.low" // The balance fell below the low-balance threshold BalanceDepleted rendobar.WebhookEvent = "balance.depleted" // The balance ran out Test rendobar.WebhookEvent = "test" // A test delivery sent from the dashboard or POST /webhooks/{id}/test )
The events this SDK knows. The API may add events: an unknown one still parses, with its own Type, so keep a default branch.
const DefaultTolerance = 5 * time.Minute
DefaultTolerance is how far a delivery's timestamp may be from now, either way, before it is refused.
Variables ¶
var ( // ErrNoSecret: the secret is empty or whitespace. An empty key proves nothing. ErrNoSecret = errors.New("webhooks: no signing secret") // ErrSignature: the signature is missing, malformed or does not match. ErrSignature = errors.New("webhooks: signature does not match") // ErrTimestamp: the timestamp is missing, malformed or outside the tolerance. ErrTimestamp = errors.New("webhooks: timestamp missing or outside the tolerance") // ErrPayload: the body is signed but is not a Rendobar event envelope. ErrPayload = errors.New("webhooks: body is not a Rendobar event") )
Why a delivery is refused. Parse returns one of these, wrapped, so check with errors.Is.
Functions ¶
func Verify ¶
Verify reports whether body is signed with secret, or with the previous secret during a rotation, within the tolerance. Use it when you only need the check. Parse also reads the event.
The signature is "sha256=" followed by the hex HMAC-SHA256 of "<X-Rendobar-Timestamp>.<body>". A signature without the sha256= prefix, or that is not hex, fails. So does an empty or whitespace-only secret.
Types ¶
type BalanceDepletedData ¶
type BalanceDepletedData struct {
Balance int64 `json:"balance"`
BalanceFormatted string `json:"balanceFormatted"`
TriggeredByJobID string `json:"triggeredByJobId"`
TriggeredByJobType rendobar.JobType `json:"triggeredByJobType"`
}
BalanceDepletedData is read from the API. Fields it does not send are zero.
type BalanceLowData ¶
type BalanceLowData struct {
Balance int64 `json:"balance"`
BalanceFormatted string `json:"balanceFormatted"`
Threshold int64 `json:"threshold"`
ThresholdFormatted string `json:"thresholdFormatted"`
TriggeredByJobID string `json:"triggeredByJobId"`
TriggeredByJobType rendobar.JobType `json:"triggeredByJobType"`
}
BalanceLowData is read from the API. Fields it does not send are zero.
type Event ¶
type Event struct {
// Version is the envelope version, "1" today. A newer version still
// parses as far as the fields below go.
Version string
Type rendobar.WebhookEvent
// ID is the delivery id. A retried delivery keeps it, so use it to ignore
// a repeat.
ID string
OrgID string // the workspace the event belongs to
SentAt time.Time // when the API sent the event
Data json.RawMessage
}
Event is one delivery: the envelope every event shares. Data is the event's payload as JSON. Payload reads it as the type for Type.
func Parse ¶
Parse verifies a delivery, as Verify does, and reads its envelope. A refusal wraps ErrNoSecret, ErrTimestamp, ErrSignature or ErrPayload.
type JobCancelledData ¶
type JobCancelledData struct {
JobID string `json:"jobId"`
JobType rendobar.JobType `json:"jobType"`
Timing *JobCancelledDataTiming `json:"timing"`
}
JobCancelledData is read from the API. Fields it does not send are zero.
type JobCancelledDataTiming ¶
type JobCancelledDataTiming struct {
CreatedAt rendobar.UnixMillis `json:"createdAt"`
CancelledAt rendobar.UnixMillis `json:"cancelledAt"`
}
JobCancelledDataTiming is read from the API. Fields it does not send are zero.
type JobCompletedData ¶
type JobCompletedData struct {
JobID string `json:"jobId"`
JobType rendobar.JobType `json:"jobType"`
Output *rendobar.JobOutput `json:"output"`
Cost *WebhookJobCost `json:"cost"`
Timing *JobCompletedDataTiming `json:"timing"`
Steps []WebhookJobStep `json:"steps"`
OutputCategory rendobar.OutputCategory `json:"outputCategory"`
MediaType string `json:"mediaType"`
}
JobCompletedData is read from the API. Fields it does not send are zero.
type JobCompletedDataTiming ¶
type JobCompletedDataTiming struct {
CreatedAt rendobar.UnixMillis `json:"createdAt"`
DispatchedAt rendobar.UnixMillis `json:"dispatchedAt"`
StartedAt rendobar.UnixMillis `json:"startedAt"`
CompletedAt rendobar.UnixMillis `json:"completedAt"`
}
JobCompletedDataTiming is read from the API. Fields it does not send are zero.
type JobCreatedData ¶
type JobCreatedData struct {
JobID string `json:"jobId"`
JobType rendobar.JobType `json:"jobType"`
Timing *JobCreatedDataTiming `json:"timing"`
}
JobCreatedData is read from the API. Fields it does not send are zero.
type JobCreatedDataTiming ¶
type JobCreatedDataTiming struct {
CreatedAt rendobar.UnixMillis `json:"createdAt"`
}
JobCreatedDataTiming is read from the API. Fields it does not send are zero.
type JobDeliveriesSettledData ¶
type JobDeliveriesSettledData struct {
JobID string `json:"jobId"`
Deliveries []SettledDelivery `json:"deliveries"`
}
JobDeliveriesSettledData is read from the API. Fields it does not send are zero.
type JobDeliveryFailedData ¶
type JobDeliveryFailedData struct {
JobID string `json:"jobId"`
StorageID string `json:"storageId"`
Reason rendobar.DeliveryReason `json:"reason"`
}
JobDeliveryFailedData is read from the API. Fields it does not send are zero.
type JobDeliverySucceededData ¶
type JobDeliverySucceededData struct {
JobID string `json:"jobId"`
StorageID string `json:"storageId"`
Path string `json:"path"`
URL string `json:"url"`
}
JobDeliverySucceededData is read from the API. Fields it does not send are zero.
type JobFailedData ¶
type JobFailedData struct {
JobID string `json:"jobId"`
JobType rendobar.JobType `json:"jobType"`
Error *rendobar.JobError `json:"error"`
Timing *JobFailedDataTiming `json:"timing"`
Steps []WebhookJobStep `json:"steps"`
}
JobFailedData is read from the API. Fields it does not send are zero.
type JobFailedDataTiming ¶
type JobFailedDataTiming struct {
CreatedAt rendobar.UnixMillis `json:"createdAt"`
DispatchedAt rendobar.UnixMillis `json:"dispatchedAt"`
StartedAt rendobar.UnixMillis `json:"startedAt"`
FailedAt rendobar.UnixMillis `json:"failedAt"`
}
JobFailedDataTiming is read from the API. Fields it does not send are zero.
type JobStartedData ¶
type JobStartedData struct {
JobID string `json:"jobId"`
JobType rendobar.JobType `json:"jobType"`
Timing *JobStartedDataTiming `json:"timing"`
}
JobStartedData is read from the API. Fields it does not send are zero.
type JobStartedDataTiming ¶
type JobStartedDataTiming struct {
CreatedAt rendobar.UnixMillis `json:"createdAt"`
DispatchedAt rendobar.UnixMillis `json:"dispatchedAt"`
StartedAt rendobar.UnixMillis `json:"startedAt"`
}
JobStartedDataTiming is read from the API. Fields it does not send are zero.
type Options ¶
Options tune Verify and Parse. The zero value uses DefaultTolerance and the current time.
type Payload ¶
type Payload interface {
// contains filtered or unexported methods
}
Payload is the data of one event: a *<Event>Data type such as *JobCompletedData, or *UnknownPayload for an event newer than this SDK. Read it with a type switch that keeps a default branch.
type SettledDelivery ¶
type SettledDelivery struct {
// The storage connection id
StorageID string `json:"storageId"`
Status rendobar.DeliveryStatus `json:"status"`
// Object key in the bucket, rendered from the template. A multi-file output is a folder, and its path ends in /
Path string `json:"path"`
// Where to fetch it: the public base URL when set, else our files route. Absent for a folder
URL string `json:"url"`
Reason rendobar.DeliveryReason `json:"reason"`
// The name was taken, so the job's tail was appended to free it
Renamed bool `json:"renamed"`
}
SettledDelivery is read from the API. Fields it does not send are zero.
type TestEventData ¶
TestEventData is read from the API. Fields it does not send are zero.
type UnknownPayload ¶
type UnknownPayload struct {
Type rendobar.WebhookEvent
Data json.RawMessage
}
UnknownPayload is the data of an event this SDK does not know yet.
type WebhookJobCost ¶
type WebhookJobCost struct {
Amount int64 `json:"amount"`
Currency rendobar.Currency `json:"currency"`
Formatted string `json:"formatted"`
Breakdown string `json:"breakdown"`
}
WebhookJobCost is read from the API. Fields it does not send are zero.
type WebhookJobStep ¶
type WebhookJobStep struct {
ID string `json:"id"`
Name string `json:"name"`
Status rendobar.StepStatus `json:"status"`
DurationMs int64 `json:"durationMs"`
Error string `json:"error"`
}
WebhookJobStep is read from the API. Fields it does not send are zero.