forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
59 lines (48 loc) · 2.35 KB
/
handler.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
package courier
import (
"context"
"net/http"
"github.com/nyaruka/gocommon/urns"
)
// Event is our interface for the types of things a ChannelHandleFunc can return.
type Event interface {
EventID() int64
}
// ChannelHandleFunc is the interface ChannelHandlers must satisfy to handle incoming requests.
// The Server will take care of looking up the channel by UUID before passing it to this function.
// Errors in format of the request or by the caller should be handled and logged internally. Errors in
// execution or in courier itself should be passed back.
type ChannelHandleFunc func(context.Context, Channel, http.ResponseWriter, *http.Request, *ChannelLog) ([]Event, error)
// ChannelHandler is the interface all handlers must satisfy
type ChannelHandler interface {
Initialize(Server) error
Server() Server
ChannelType() ChannelType
ChannelName() string
UseChannelRouteUUID() bool
RedactValues(Channel) []string
GetChannel(context.Context, *http.Request) (Channel, error)
Send(context.Context, MsgOut, *SendResult, *ChannelLog) error
WriteStatusSuccessResponse(context.Context, http.ResponseWriter, []StatusUpdate) error
WriteMsgSuccessResponse(context.Context, http.ResponseWriter, []MsgIn) error
WriteRequestError(context.Context, http.ResponseWriter, error) error
WriteRequestIgnored(context.Context, http.ResponseWriter, string) error
}
// URNDescriber is the interface handlers which can look up URN metadata for new contacts should satisfy.
type URNDescriber interface {
DescribeURN(context.Context, Channel, urns.URN, *ChannelLog) (map[string]string, error)
}
// AttachmentRequestBuilder is the interface handlers which can allow a custom way to download attachment media for messages should satisfy
type AttachmentRequestBuilder interface {
BuildAttachmentRequest(context.Context, Backend, Channel, string, *ChannelLog) (*http.Request, error)
}
// RegisterHandler adds a new handler for a channel type, this is called by individual handlers when they are initialized
func RegisterHandler(handler ChannelHandler) {
registeredHandlers[handler.ChannelType()] = handler
}
// GetHandler returns the handler for the passed in channel type, or nil if not found
func GetHandler(ct ChannelType) ChannelHandler {
return registeredHandlers[ct]
}
var registeredHandlers = make(map[ChannelType]ChannelHandler)
var activeHandlers = make(map[ChannelType]ChannelHandler)