Skip to content

Commit

Permalink
improve webhook comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-enebeli committed Sep 11, 2024
1 parent 934a1f7 commit b10d5a3
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ import (
"github.com/hibiken/asynq"
)

// NewWebhook represents the structure of a webhook notification.
// It includes an event type and associated payload data.
type NewWebhook struct {
Event string `json:"event"`
Payload interface{} `json:"data"`
Event string `json:"event"` // The event type that triggered the webhook.
Payload interface{} `json:"data"` // The data associated with the event.
}

// getEventFromStatus maps a transaction status to a corresponding event string.
//
// Parameters:
// - status string: The status of the transaction.
//
// Returns:
// - string: The corresponding event string for the transaction status.
func getEventFromStatus(status string) string {
switch strings.ToLower(status) {
case strings.ToLower(StatusQueued):
Expand All @@ -55,6 +63,12 @@ func getEventFromStatus(status string) string {
}

// processHTTP sends a webhook notification via HTTP POST request.
//
// Parameters:
// - data NewWebhook: The webhook notification data to send.
//
// Returns:
// - error: An error if the request or processing fails.
func processHTTP(data NewWebhook) error {
conf, err := config.Fetch()
if err != nil {
Expand Down Expand Up @@ -106,11 +120,16 @@ func processHTTP(data NewWebhook) error {
}

log.Println("Webhook notification sent successfully:", response)

return nil
}

// SendWebhook enqueues a webhook notification task.
//
// Parameters:
// - newWebhook NewWebhook: The webhook notification data to enqueue.
//
// Returns:
// - error: An error if the task could not be enqueued.
func SendWebhook(newWebhook NewWebhook) error {
conf, err := config.Fetch()
if err != nil {
Expand All @@ -137,7 +156,14 @@ func SendWebhook(newWebhook NewWebhook) error {
return err
}

// ProcessWebhook processes a webhook notification task.
// ProcessWebhook processes a webhook notification task from the queue.
//
// Parameters:
// - _ context.Context: The context for the operation.
// - task *asynq.Task: The task containing the webhook notification data.
//
// Returns:
// - error: An error if the webhook processing fails.
func ProcessWebhook(_ context.Context, task *asynq.Task) error {
conf, err := config.Fetch()
if err != nil {
Expand Down

0 comments on commit b10d5a3

Please sign in to comment.