-
Notifications
You must be signed in to change notification settings - Fork 20
/
services.go
152 lines (124 loc) · 4.78 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
package flows
import (
"net/http"
"time"
"github.com/nyaruka/gocommon/urns"
"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)
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
BodyIgnored 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)
}
// 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
}
// NewHTTPLog creates a new HTTP log from a trace
func NewHTTPLog(trace *httpx.Trace, statusFn HTTPStatusResolver) *HTTPLog {
return newHTTPLogWithStatus(trace, statusFn(trace))
}
// creates a new HTTP log from a trace with an explicit status
func newHTTPLogWithStatus(trace *httpx.Trace, status CallStatus) *HTTPLog {
return &HTTPLog{
URL: trace.Request.URL.String(),
Status: status,
Request: string(trace.RequestTrace),
Response: string(trace.ResponseTrace),
CreatedOn: trace.StartTime,
ElapsedMS: int((trace.EndTime.Sub(trace.StartTime)) / time.Millisecond),
}
}