-
Notifications
You must be signed in to change notification settings - Fork 20
/
services.go
173 lines (141 loc) · 5.39 KB
/
services.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
package flows
import (
"net/http"
"time"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/goflow/utils/httpx"
"github.com/shopspring/decimal"
)
// Services groups together interfaces for several services whose implementation is provided outside of the flow engine.
type Services interface {
Email(Session) (EmailService, error)
Webhook(Session) (WebhookService, error)
Classification(Session, *Classifier) (ClassificationService, error)
Ticket(Session, *Ticketer) (TicketService, error)
Airtime(Session) (AirtimeService, error)
}
// EmailService provides email functionality to the engine
type EmailService interface {
Send(session Session, addresses []string, subject, body string) error
}
// CallStatus represents the status of a call to an external service
type CallStatus string
const (
// CallStatusSuccess represents that the webhook was successful
CallStatusSuccess CallStatus = "success"
// CallStatusConnectionError represents that the webhook had a connection error
CallStatusConnectionError CallStatus = "connection_error"
// CallStatusResponseError represents that the webhook response had a non 2xx status code
CallStatusResponseError CallStatus = "response_error"
// CallStatusSubscriberGone represents a special state of resthook responses which indicate the caller must remove that subscriber
CallStatusSubscriberGone CallStatus = "subscriber_gone"
)
// WebhookCall is the result of a webhook call
type WebhookCall struct {
*httpx.Trace
ValidJSON bool
}
// WebhookService provides webhook functionality to the engine
type WebhookService interface {
Call(session Session, request *http.Request) (*WebhookCall, error)
}
// ExtractedIntent models an intent match
type ExtractedIntent struct {
Name string `json:"name"`
Confidence decimal.Decimal `json:"confidence"`
}
// ExtractedEntity models an entity match
type ExtractedEntity struct {
Value string `json:"value"`
Confidence decimal.Decimal `json:"confidence"`
}
// Classification is the result of an NLU classification
type Classification struct {
Intents []ExtractedIntent `json:"intents,omitempty"`
Entities map[string][]ExtractedEntity `json:"entities,omitempty"`
}
// ClassificationService provides NLU functionality to the engine
type ClassificationService interface {
Classify(session Session, input string, logHTTP HTTPLogCallback) (*Classification, error)
}
// TicketService provides ticketing functionality to the engine
type TicketService interface {
// Open tries to open a new ticket
Open(session Session, subject, body string, logHTTP HTTPLogCallback) (*Ticket, error)
}
// AirtimeTransferStatus is a status of a airtime transfer
type AirtimeTransferStatus string
// possible values for airtime transfer statuses
const (
AirtimeTransferStatusSuccess AirtimeTransferStatus = "success"
AirtimeTransferStatusFailed AirtimeTransferStatus = "failed"
)
// AirtimeTransfer is the result of an attempted airtime transfer
type AirtimeTransfer struct {
Sender urns.URN
Recipient urns.URN
Currency string
DesiredAmount decimal.Decimal
ActualAmount decimal.Decimal
}
// AirtimeService provides airtime functionality to the engine
type AirtimeService interface {
// Transfer transfers airtime to the given URN
Transfer(session Session, sender urns.URN, recipient urns.URN, amounts map[string]decimal.Decimal, logHTTP HTTPLogCallback) (*AirtimeTransfer, error)
}
// HTTPLog describes an HTTP request/response
type HTTPLog struct {
URL string `json:"url" validate:"required"`
Status CallStatus `json:"status" validate:"required"`
Request string `json:"request" validate:"required"`
Response string `json:"response,omitempty"`
CreatedOn time.Time `json:"created_on" validate:"required"`
ElapsedMS int `json:"elapsed_ms"`
}
// HTTPLogCallback is a function that handles an HTTP log
type HTTPLogCallback func(*HTTPLog)
// HTTPLogger logs HTTP logs
type HTTPLogger struct {
Logs []*HTTPLog
}
// Log logs the given HTTP log
func (l *HTTPLogger) Log(h *HTTPLog) {
l.Logs = append(l.Logs, h)
}
// HTTPStatusResolver is a function that determines the status of an HTTP log from the response
type HTTPStatusResolver func(t *httpx.Trace) CallStatus
// HTTPStatusFromCode uses the status code to determine status of an HTTP log
func HTTPStatusFromCode(t *httpx.Trace) CallStatus {
if t.Response == nil {
return CallStatusConnectionError
} else if t.Response.StatusCode >= 400 {
return CallStatusResponseError
}
return CallStatusSuccess
}
// RedactionMask is the redaction mask for HTTP service logs
const RedactionMask = "****************"
// NewHTTPLog creates a new HTTP log from a trace
func NewHTTPLog(trace *httpx.Trace, statusFn HTTPStatusResolver, redact utils.Redactor) *HTTPLog {
return newHTTPLogWithStatus(trace, statusFn(trace), redact)
}
// creates a new HTTP log from a trace with an explicit status
func newHTTPLogWithStatus(trace *httpx.Trace, status CallStatus, redact utils.Redactor) *HTTPLog {
url := trace.Request.URL.String()
request := string(trace.RequestTrace)
response := trace.ResponseTraceUTF8("...")
if redact != nil {
url = redact(url)
request = redact(request)
response = redact(response)
}
return &HTTPLog{
URL: url,
Status: status,
Request: request,
Response: response,
CreatedOn: trace.StartTime,
ElapsedMS: int((trace.EndTime.Sub(trace.StartTime)) / time.Millisecond),
}
}