Creates a new webhook handler based on the url and filter function.
(ctx context.Context, url string, filterFnString string, timeout *uint64, options map[string]interface{})
| 51 | |
| 52 | // Creates a new webhook handler based on the url and filter function. |
| 53 | func NewWebhook(ctx context.Context, url string, filterFnString string, timeout *uint64, options map[string]interface{}) (*Webhook, error) { |
| 54 | |
| 55 | var err error |
| 56 | |
| 57 | if url == "" { |
| 58 | err = errors.New("url parameter must be defined for webhook events.") |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | wh := &Webhook{ |
| 63 | url: url, |
| 64 | } |
| 65 | if filterFnString != "" { |
| 66 | wh.filter = NewJSEventFunction(ctx, filterFnString) |
| 67 | } |
| 68 | |
| 69 | if timeout != nil { |
| 70 | wh.timeout = time.Duration(*timeout) * time.Second |
| 71 | } else { |
| 72 | wh.timeout = time.Duration(kDefaultWebhookTimeout) * time.Second |
| 73 | } |
| 74 | |
| 75 | // Initialize transport and client |
| 76 | transport := base.DefaultHTTPTransport() |
| 77 | transport.DisableKeepAlives = false |
| 78 | wh.client = &http.Client{Transport: transport, Timeout: wh.timeout} |
| 79 | |
| 80 | if options != nil { |
| 81 | wh.options.DocumentChangedWinningRevOnly, _ = options[EventOptionDocumentChangedWinningRevOnly].(bool) |
| 82 | } |
| 83 | |
| 84 | return wh, err |
| 85 | } |
| 86 | |
| 87 | // Performs an HTTP POST to the url defined for the handler. If a filter function is defined, |
| 88 | // calls it to determine whether to POST. The payload for the POST is depends |