forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.go
86 lines (71 loc) · 2.77 KB
/
responses.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
package courier
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/pressly/lg"
"github.com/sirupsen/logrus"
validator "gopkg.in/go-playground/validator.v9"
)
// WriteError writes a JSON response for the passed in error
func WriteError(w http.ResponseWriter, r *http.Request, err error) error {
errors := []string{err.Error()}
vErrs, isValidation := err.(validator.ValidationErrors)
if isValidation {
errors = []string{}
for i := range vErrs {
errors = append(errors, fmt.Sprintf("field '%s' %s", strings.ToLower(vErrs[i].Field()), vErrs[i].Tag()))
}
} else {
lg.Log(r.Context()).WithError(err).Error()
}
return writeJSONResponse(w, http.StatusBadRequest, &errorResponse{errors})
}
// WriteIgnored writes a JSON response for the passed in message
func WriteIgnored(w http.ResponseWriter, r *http.Request, message string) error {
lg.Log(r.Context()).Info("ignored message")
return writeData(w, http.StatusOK, message, struct{}{})
}
// WriteReceiveSuccess writes a JSON response for the passed in msg indicating we handled it
func WriteReceiveSuccess(w http.ResponseWriter, r *http.Request, msg Msg) error {
lg.Log(r.Context()).WithFields(logrus.Fields{
"channel_uuid": msg.Channel().UUID(),
"msg_uuid": msg.UUID(),
"msg_id": msg.ID().Int64,
"msg_urn": msg.URN().Identity(),
"msg_text": msg.Text(),
"msg_attachments": msg.Attachments(),
}).Info("message received")
return writeData(w, http.StatusOK, "Message Accepted", &receiveData{msg.UUID()})
}
// WriteStatusSuccess writes a JSON response for the passed in status update indicating we handled it
func WriteStatusSuccess(w http.ResponseWriter, r *http.Request, status MsgStatus) error {
if status.ID() != NilMsgID {
lg.Log(r.Context()).WithField("channel_uuid", status.ChannelUUID()).WithField("msg_id", status.ID().Int64).Info("status updated")
} else {
lg.Log(r.Context()).WithField("channel_uuid", status.ChannelUUID()).WithField("msg_external_id", status.ExternalID()).Info("status updated")
}
return writeData(w, http.StatusOK, "Status Update Accepted", &statusData{status.Status()})
}
type errorResponse struct {
Text []string `json:"errors"`
}
type successResponse struct {
Message string `json:"message"`
Data interface{} `json:"data"`
}
type receiveData struct {
UUID MsgUUID `json:"uuid"`
}
type statusData struct {
Status MsgStatusValue `json:"status"`
}
func writeJSONResponse(w http.ResponseWriter, statusCode int, response interface{}) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
return json.NewEncoder(w).Encode(response)
}
func writeData(w http.ResponseWriter, statusCode int, message string, response interface{}) error {
return writeJSONResponse(w, statusCode, &successResponse{message, response})
}