/
webhook.go
64 lines (52 loc) · 1.52 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package accountmigration
import (
"net/http"
"net/url"
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/hook"
"github.com/authgear/authgear-server/pkg/util/httputil"
"github.com/authgear/authgear-server/pkg/util/log"
)
type WebhookMiddlewareLogger struct{ *log.Logger }
func NewWebhookMiddlewareLogger(lf *log.Factory) WebhookMiddlewareLogger {
return WebhookMiddlewareLogger{lf.New("account-migration-webhook")}
}
type AccountMigrationWebHook struct {
hook.WebHook
Client HookHTTPClient
Logger WebhookMiddlewareLogger
}
func (h *AccountMigrationWebHook) Call(u *url.URL, hookReq *HookRequest) (*HookResponse, error) {
req, err := h.PrepareRequest(u, hookReq)
if err != nil {
return nil, err
}
resp, err := h.PerformWithResponse(h.Client.Client, req)
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
if err != nil {
h.Logger.WithError(err).Error("failed to call webhook")
return nil, err
}
var hookResp *HookResponse
hookResp, err = ParseHookResponse(resp.Body)
if err != nil {
apiError := apierrors.AsAPIError(err)
err = hook.WebHookInvalidResponse.NewWithInfo("invalid response body", apiError.Info)
return nil, err
}
return hookResp, nil
}
var _ Hook = &AccountMigrationWebHook{}
type HookHTTPClient struct {
*http.Client
}
func NewHookHTTPClient(cfg *config.AccountMigrationHookConfig) HookHTTPClient {
return HookHTTPClient{
httputil.NewExternalClient(cfg.Timeout.Duration()),
}
}