forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
175 lines (130 loc) · 5.05 KB
/
channel.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package courier
import (
"database/sql/driver"
"errors"
"strings"
"github.com/nyaruka/null"
"github.com/gofrs/uuid"
)
const (
// ConfigAPIKey is a constant key for channel configs
ConfigAPIKey = "api_key"
// ConfigAuthToken is a constant key for channel configs
ConfigAuthToken = "auth_token"
// ConfigBaseURL is a constant key for channel configs
ConfigBaseURL = "base_url"
// ConfigCallbackDomain is the domain that should be used for this channel when registering callbacks
ConfigCallbackDomain = "callback_domain"
// ConfigContentType is a constant key for channel configs
ConfigContentType = "content_type"
// ConfigMaxLength is the maximum size of a message in characters
ConfigMaxLength = "max_length"
// ConfigPassword is a constant key for channel configs
ConfigPassword = "password"
// ConfigSecret is the secret used for signing commands by the channel
ConfigSecret = "secret"
// ConfigSendAuthorization is a constant key for channel configs
ConfigSendAuthorization = "send_authorization"
// ConfigSendBody is a constant key for channel configs
ConfigSendBody = "body"
// ConfigSendMethod is a constant key for channel configs
ConfigSendMethod = "method"
// ConfigSendURL is a constant key for channel configs
ConfigSendURL = "send_url"
// ConfigUsername is a constant key for channel configs
ConfigUsername = "username"
// ConfigUseNational is a constant key for channel configs
ConfigUseNational = "use_national"
// ConfigSendHeaders is a constant key for channel configs
ConfigSendHeaders = "headers"
)
// ChannelType is our typing of the two char channel types
type ChannelType string
// AnyChannelType is our empty channel type used when doing lookups without channel type assertions
var AnyChannelType = ChannelType("")
func (ct ChannelType) String() string {
return string(ct)
}
// ChannelRole is a role that a channel can perform
type ChannelRole string
// different roles that channels can perform
const (
ChannelRoleSend ChannelRole = "S"
ChannelRoleReceive ChannelRole = "R"
ChannelRoleCall ChannelRole = "C"
ChannelRoleAnswer ChannelRole = "A"
)
// ChannelUUID is our typing of a channel's UUID
type ChannelUUID struct {
uuid.UUID
}
// NilChannelUUID is our nil value for channel UUIDs
var NilChannelUUID = ChannelUUID{uuid.Nil}
// NewChannelUUID creates a new ChannelUUID for the passed in string
func NewChannelUUID(u string) (ChannelUUID, error) {
channelUUID, err := uuid.FromString(strings.ToLower(u))
if err != nil {
return NilChannelUUID, err
}
return ChannelUUID{channelUUID}, nil
}
// ChannelID is our SQL type for a channel's id
type ChannelID null.Int
// NilChannelID represents a nil channel id
const NilChannelID = ChannelID(0)
// ChannelAddress is our SQL type for a channel address
type ChannelAddress null.String
// NilChannelAddress represents a nil channel address
const NilChannelAddress = ChannelAddress("")
func (address ChannelAddress) String() string {
return string(address)
}
// MarshalJSON marshals into JSON. 0 values will become null
func (i ChannelID) MarshalJSON() ([]byte, error) {
return null.Int(i).MarshalJSON()
}
// UnmarshalJSON unmarshals from JSON. null values become 0
func (i *ChannelID) UnmarshalJSON(b []byte) error {
return null.UnmarshalInt(b, (*null.Int)(i))
}
// Value returns the db value, null is returned for 0
func (i ChannelID) Value() (driver.Value, error) {
return null.Int(i).Value()
}
// Scan scans from the db value. null values become 0
func (i *ChannelID) Scan(value interface{}) error {
return null.ScanInt(value, (*null.Int)(i))
}
// NewChannelID creates a new ChannelID for the passed in int64
func NewChannelID(id int64) ChannelID {
return ChannelID(id)
}
// ErrChannelExpired is returned when our cached channel has outlived it's TTL
var ErrChannelExpired = errors.New("channel expired")
// ErrChannelNotFound is returned when we fail to find a channel in the db
var ErrChannelNotFound = errors.New("channel not found")
// ErrChannelWrongType is returned when we find a channel with the set UUID but with a different type
var ErrChannelWrongType = errors.New("channel type wrong")
//-----------------------------------------------------------------------------
// Channel Interface
//-----------------------------------------------------------------------------
// Channel defines the general interface backend Channel implementations must adhere to
type Channel interface {
UUID() ChannelUUID
Name() string
ChannelType() ChannelType
Schemes() []string
Country() string
Address() string
ChannelAddress() ChannelAddress
Roles() []ChannelRole
// is this channel for the passed in scheme (and only that scheme)
IsScheme(string) bool
// CallbackDomain returns the domain that should be used for any callbacks the channel registers
CallbackDomain(fallbackDomain string) string
ConfigForKey(key string, defaultValue interface{}) interface{}
StringConfigForKey(key string, defaultValue string) string
BoolConfigForKey(key string, defaultValue bool) bool
IntConfigForKey(key string, defaultValue int) int
OrgConfigForKey(key string, defaultValue interface{}) interface{}
}