mirror of https://github.com/hrfee/jfa-go
Webhooks send a POST to an admin-supplied URL when something happens, with relevant information sent in JSON. One has been added for creating users in Settings > Webhooks > User Created. Lazily, the portion of GetUsers which generates a respUser has been factored out, and is called to send the JSON payload. A stripped-down common.Req method has been added, which is used by the barebones WebhookSender struct.pull/297/head
parent
8307d3da90
commit
e5f79c60ae
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/hrfee/jfa-go/common"
|
||||
"github.com/hrfee/jfa-go/logger"
|
||||
lm "github.com/hrfee/jfa-go/logmessages"
|
||||
)
|
||||
|
||||
type WebhookSender struct {
|
||||
httpClient *http.Client
|
||||
timeoutHandler common.TimeoutHandler
|
||||
log *logger.Logger
|
||||
}
|
||||
|
||||
// SetTransport sets the http.Transport to use for requests. Can be used to set a proxy.
|
||||
func (ws *WebhookSender) SetTransport(t *http.Transport) {
|
||||
ws.httpClient.Transport = t
|
||||
}
|
||||
|
||||
func NewWebhookSender(timeoutHandler common.TimeoutHandler, log *logger.Logger) *WebhookSender {
|
||||
return &WebhookSender{
|
||||
httpClient: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
},
|
||||
timeoutHandler: timeoutHandler,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *WebhookSender) Send(uri string, payload any) (int, error) {
|
||||
_, status, err := common.Req(ws.httpClient, ws.timeoutHandler, http.MethodPost, uri, payload, url.Values{}, nil, true)
|
||||
ws.log.Printf(lm.WebhookRequest, uri, status, err)
|
||||
return status, err
|
||||
}
|
Loading…
Reference in new issue