forked from omise/omise-go
/
webhook.go
42 lines (34 loc) · 1.31 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
package omise
import (
"encoding/json"
"net/http"
)
// EventHandler is an interface for handling events from the webhook http.Handler.
type EventHandler interface {
HandleEvent(http.ResponseWriter, *http.Request, *Event)
}
// EventHandlerFunc lets you use a plain function as an EventHandler type.
type EventHandlerFunc func(http.ResponseWriter, *http.Request, *Event)
// HandleEvent implements the EventHandler interface by calling the underlying funciton.
func (f EventHandlerFunc) HandleEvent(resp http.ResponseWriter, req *http.Request, event *Event) {
f(resp, req, event)
}
type webhookHTTPHandler struct {
eventHandler EventHandler
}
func (h *webhookHTTPHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
event := &Event{}
if err := json.NewDecoder(req.Body).Decode(event); err != nil {
http.Error(resp, err.Error(), http.StatusBadRequest)
return
}
h.eventHandler.HandleEvent(resp, req, event)
}
// WebhookHTTPHandler creates an http.Handler that you can use to receive Omise's webhook
// API calls for events. The returned handler will automatically consume the request body
// and unmarshals an Event object from JSON for you.
//
// See https://www.omise.co/api-webhooks for more information.
func WebhookHTTPHandler(handler EventHandler) http.Handler {
return &webhookHTTPHandler{handler}
}