From 89c7fa5015e86d827b020f5f9449a8b986fe9513 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 3 Oct 2023 17:02:36 -0500 Subject: [PATCH] Cleanup some handler code --- handlers/bandwidth/handler.go | 6 ++---- handlers/dialog360/handler.go | 6 ++---- handlers/facebook_legacy/handler.go | 7 ++----- handlers/firebase/handler.go | 6 ++---- handlers/freshchat/handler.go | 7 ++----- handlers/messagebird/handler.go | 7 ++----- handlers/meta/handlers.go | 27 +++++++++++---------------- handlers/playmobile/handler.go | 8 ++------ handlers/rocketchat/handler.go | 7 ++----- handlers/twitter/handler.go | 7 ++----- handlers/wavy/handler.go | 7 ++----- handlers/whatsapp_legacy/handler.go | 18 ++++-------------- handlers/zenvia/handlers.go | 8 ++------ 13 files changed, 37 insertions(+), 84 deletions(-) diff --git a/handlers/bandwidth/handler.go b/handlers/bandwidth/handler.go index e7d231067..51aa15f31 100644 --- a/handlers/bandwidth/handler.go +++ b/handlers/bandwidth/handler.go @@ -13,6 +13,7 @@ import ( "github.com/nyaruka/courier/handlers" "github.com/nyaruka/courier/utils" "github.com/nyaruka/gocommon/httpx" + "github.com/nyaruka/gocommon/jsonx" ) var ( @@ -215,10 +216,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch payload.Media = attachments } - jsonBody, err := json.Marshal(payload) - if err != nil { - return status, err - } + jsonBody := jsonx.MustMarshal(payload) // build our request req, err := http.NewRequest(http.MethodPost, fmt.Sprintf(sendURL, accountID), bytes.NewReader(jsonBody)) diff --git a/handlers/dialog360/handler.go b/handlers/dialog360/handler.go index 9cad1494a..742c9606f 100644 --- a/handlers/dialog360/handler.go +++ b/handlers/dialog360/handler.go @@ -16,6 +16,7 @@ import ( "github.com/nyaruka/courier/handlers" "github.com/nyaruka/courier/handlers/meta/whatsapp" "github.com/nyaruka/courier/utils" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" "github.com/pkg/errors" ) @@ -590,10 +591,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch } func requestD3C(payload whatsapp.SendRequest, accessToken string, status courier.StatusUpdate, wacPhoneURL *url.URL, zeroIndex bool, clog *courier.ChannelLog) (courier.StatusUpdate, error) { - jsonBody, err := json.Marshal(payload) - if err != nil { - return status, err - } + jsonBody := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, wacPhoneURL.String(), bytes.NewReader(jsonBody)) if err != nil { diff --git a/handlers/facebook_legacy/handler.go b/handlers/facebook_legacy/handler.go index 2a1267ca5..207133dff 100644 --- a/handlers/facebook_legacy/handler.go +++ b/handlers/facebook_legacy/handler.go @@ -3,7 +3,6 @@ package facebook_legacy import ( "bytes" "context" - "encoding/json" "fmt" "net/http" "net/url" @@ -14,6 +13,7 @@ import ( "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" "github.com/nyaruka/courier/utils" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -540,10 +540,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch payload.Message.QuickReplies = nil } - jsonBody, err := json.Marshal(payload) - if err != nil { - return status, err - } + jsonBody := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, msgURL.String(), bytes.NewReader(jsonBody)) if err != nil { diff --git a/handlers/firebase/handler.go b/handlers/firebase/handler.go index 43df982ea..672cbc6cb 100644 --- a/handlers/firebase/handler.go +++ b/handlers/firebase/handler.go @@ -11,6 +11,7 @@ import ( "github.com/buger/jsonparser" "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" ) @@ -185,10 +186,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch payload.ContentAvailable = true } - jsonPayload, err := json.Marshal(payload) - if err != nil { - return nil, err - } + jsonPayload := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, sendURL, bytes.NewReader(jsonPayload)) if err != nil { diff --git a/handlers/freshchat/handler.go b/handlers/freshchat/handler.go index 50e81adaf..8cfd0cb4a 100644 --- a/handlers/freshchat/handler.go +++ b/handlers/freshchat/handler.go @@ -11,7 +11,6 @@ import ( "crypto/sha256" "crypto/x509" "encoding/base64" - "encoding/json" "encoding/pem" "fmt" "io" @@ -21,6 +20,7 @@ import ( "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" ) @@ -147,10 +147,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch } } - jsonBody, err := json.Marshal(payload) - if err != nil { - return nil, err - } + jsonBody := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(jsonBody)) diff --git a/handlers/messagebird/handler.go b/handlers/messagebird/handler.go index 9ba67ed4f..4b01076aa 100644 --- a/handlers/messagebird/handler.go +++ b/handlers/messagebird/handler.go @@ -9,7 +9,6 @@ import ( "crypto/hmac" "crypto/sha256" "encoding/hex" - "encoding/json" "strconv" "fmt" @@ -21,6 +20,7 @@ import ( "github.com/golang-jwt/jwt/v5" "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" "github.com/sirupsen/logrus" ) @@ -216,10 +216,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch payload.MediaURLs = append(payload.MediaURLs, mediaURL) } - jsonBody, err := json.Marshal(payload) - if err != nil { - return nil, err - } + jsonBody := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, sendUrl, bytes.NewReader(jsonBody)) diff --git a/handlers/meta/handlers.go b/handlers/meta/handlers.go index 59c0af62e..b9fc25119 100644 --- a/handlers/meta/handlers.go +++ b/handlers/meta/handlers.go @@ -20,6 +20,7 @@ import ( "github.com/nyaruka/courier/handlers/meta/messenger" "github.com/nyaruka/courier/handlers/meta/whatsapp" "github.com/nyaruka/courier/utils" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" "github.com/pkg/errors" ) @@ -719,16 +720,13 @@ func (h *handler) sendFacebookInstagramMsg(ctx context.Context, msg courier.MsgO // include any quick replies on the last piece we send if part.IsLast { for _, qr := range msg.QuickReplies() { - payload.Message.QuickReplies = append(payload.Message.QuickReplies, messenger.QuickReply{qr, qr, "text"}) + payload.Message.QuickReplies = append(payload.Message.QuickReplies, messenger.QuickReply{Title: qr, Payload: qr, ContentType: "text"}) } } else { payload.Message.QuickReplies = nil } - jsonBody, err := json.Marshal(payload) - if err != nil { - return status, err - } + jsonBody := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, msgURL.String(), bytes.NewReader(jsonBody)) if err != nil { @@ -1013,7 +1011,7 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog zeroIndex = true } payloadAudio = whatsapp.SendRequest{MessagingProduct: "whatsapp", RecipientType: "individual", To: msg.URN().Path(), Type: "audio", Audio: &whatsapp.Media{Link: attURL}} - status, err := requestWAC(payloadAudio, accessToken, status, wacPhoneURL, zeroIndex, clog) + err := requestWAC(payloadAudio, accessToken, status, wacPhoneURL, zeroIndex, clog) if err != nil { return status, nil } @@ -1082,7 +1080,7 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog zeroIndex = true } - status, err := requestWAC(payload, accessToken, status, wacPhoneURL, zeroIndex, clog) + err := requestWAC(payload, accessToken, status, wacPhoneURL, zeroIndex, clog) if err != nil { return status, err } @@ -1094,15 +1092,12 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog return status, nil } -func requestWAC(payload whatsapp.SendRequest, accessToken string, status courier.StatusUpdate, wacPhoneURL *url.URL, zeroIndex bool, clog *courier.ChannelLog) (courier.StatusUpdate, error) { - jsonBody, err := json.Marshal(payload) - if err != nil { - return status, err - } +func requestWAC(payload whatsapp.SendRequest, accessToken string, status courier.StatusUpdate, wacPhoneURL *url.URL, zeroIndex bool, clog *courier.ChannelLog) error { + jsonBody := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, wacPhoneURL.String(), bytes.NewReader(jsonBody)) if err != nil { - return nil, err + return err } req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken)) @@ -1114,12 +1109,12 @@ func requestWAC(payload whatsapp.SendRequest, accessToken string, status courier err = json.Unmarshal(respBody, respPayload) if err != nil { clog.Error(courier.ErrorResponseUnparseable("JSON")) - return status, nil + return nil } if respPayload.Error.Code != 0 { clog.Error(courier.ErrorExternal(strconv.Itoa(respPayload.Error.Code), respPayload.Error.Message)) - return status, nil + return nil } externalID := respPayload.Messages[0].ID @@ -1128,7 +1123,7 @@ func requestWAC(payload whatsapp.SendRequest, accessToken string, status courier } // this was wired successfully status.SetStatus(courier.MsgStatusWired) - return status, nil + return nil } // DescribeURN looks up URN metadata for new contacts diff --git a/handlers/playmobile/handler.go b/handlers/playmobile/handler.go index 22f0b20c7..ecce2fff0 100644 --- a/handlers/playmobile/handler.go +++ b/handlers/playmobile/handler.go @@ -3,7 +3,6 @@ package playmobile import ( "bytes" "context" - "encoding/json" "encoding/xml" "errors" "fmt" @@ -13,6 +12,7 @@ import ( "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" "github.com/nyaruka/gocommon/httpx" + "github.com/nyaruka/gocommon/jsonx" ) const ( @@ -183,11 +183,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch message.SMS.Content.Text = part payload.Messages = append(payload.Messages, message) - jsonBody, err := json.Marshal(payload) - - if err != nil { - return nil, err - } + jsonBody := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, fmt.Sprintf(sendURL, baseURL), bytes.NewReader(jsonBody)) if err != nil { diff --git a/handlers/rocketchat/handler.go b/handlers/rocketchat/handler.go index a37e19f03..2937bdd41 100644 --- a/handlers/rocketchat/handler.go +++ b/handlers/rocketchat/handler.go @@ -3,7 +3,6 @@ package rocketchat import ( "bytes" "context" - "encoding/json" "errors" "fmt" "net/http" @@ -11,6 +10,7 @@ import ( "github.com/buger/jsonparser" "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" ) @@ -123,10 +123,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch payload.Attachments = append(payload.Attachments, Attachment{mimeType, url}) } - body, err := json.Marshal(payload) - if err != nil { - return status, err - } + body := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, baseURL+"/message", bytes.NewReader(body)) if err != nil { diff --git a/handlers/twitter/handler.go b/handlers/twitter/handler.go index 1c6810ece..a5ee3d19f 100644 --- a/handlers/twitter/handler.go +++ b/handlers/twitter/handler.go @@ -6,7 +6,6 @@ import ( "crypto/hmac" "crypto/sha256" "encoding/base64" - "encoding/json" "fmt" "io" "mime/multipart" @@ -21,6 +20,7 @@ import ( "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" "github.com/nyaruka/courier/utils" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" "github.com/pkg/errors" ) @@ -322,10 +322,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch payload.Event.MessageCreate.MessageData.QuickReply = qrs } - jsonBody, err := json.Marshal(payload) - if err != nil { - return status, err - } + jsonBody := jsonx.MustMarshal(payload) req, _ := http.NewRequest(http.MethodPost, sendURL, bytes.NewReader(jsonBody)) req.Header.Set("Content-Type", "application/json") diff --git a/handlers/wavy/handler.go b/handlers/wavy/handler.go index 407e90b0c..073315425 100644 --- a/handlers/wavy/handler.go +++ b/handlers/wavy/handler.go @@ -3,7 +3,6 @@ package wavy import ( "bytes" "context" - "encoding/json" "fmt" "net/http" "strings" @@ -12,6 +11,7 @@ import ( "github.com/buger/jsonparser" "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" + "github.com/nyaruka/gocommon/jsonx" ) var ( @@ -137,10 +137,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch payload.Destination = strings.TrimPrefix(msg.URN().Path(), "+") payload.Message = handlers.GetTextAndAttachments(msg) - jsonPayload, err := json.Marshal(payload) - if err != nil { - return nil, err - } + jsonPayload := jsonx.MustMarshal(payload) req, err := http.NewRequest(http.MethodPost, sendURL, bytes.NewReader(jsonPayload)) if err != nil { diff --git a/handlers/whatsapp_legacy/handler.go b/handlers/whatsapp_legacy/handler.go index 86b4ee928..b1de6774b 100644 --- a/handlers/whatsapp_legacy/handler.go +++ b/handlers/whatsapp_legacy/handler.go @@ -18,6 +18,7 @@ import ( "github.com/nyaruka/courier/utils" "github.com/nyaruka/gocommon/httpx" "github.com/nyaruka/gocommon/i18n" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/redisx" "github.com/patrickmn/go-cache" @@ -909,11 +910,8 @@ func (h *handler) fetchMediaID(msg courier.MsgOut, mimeType, mediaURL string, cl } func sendWhatsAppMsg(rc redis.Conn, msg courier.MsgOut, sendPath *url.URL, payload any, clog *courier.ChannelLog) (string, string, error) { - jsonBody, err := json.Marshal(payload) + jsonBody := jsonx.MustMarshal(payload) - if err != nil { - return "", "", err - } req, _ := http.NewRequest(http.MethodPost, sendPath.String(), bytes.NewReader(jsonBody)) req.Header = buildWhatsAppHeaders(msg.Channel()) @@ -997,11 +995,7 @@ func sendWhatsAppMsg(rc redis.Conn, msg courier.MsgOut, sendPath *url.URL, paylo // marshal updated payload if updatedPayload != nil { payload = updatedPayload - jsonBody, err = json.Marshal(payload) - - if err != nil { - return "", "", err - } + jsonBody = jsonx.MustMarshal(payload) } } // try send msg again @@ -1091,11 +1085,7 @@ func checkWhatsAppContact(channel courier.Channel, baseURL string, urn urns.URN, Contacts: []string{fmt.Sprintf("+%s", urn.Path())}, ForceCheck: true, } - reqBody, err := json.Marshal(payload) - - if err != nil { - return nil, err - } + reqBody := jsonx.MustMarshal(payload) sendURL := fmt.Sprintf("%s/v1/contacts", baseURL) req, _ := http.NewRequest(http.MethodPost, sendURL, bytes.NewReader(reqBody)) req.Header = buildWhatsAppHeaders(channel) diff --git a/handlers/zenvia/handlers.go b/handlers/zenvia/handlers.go index ef2d25898..392e8d300 100644 --- a/handlers/zenvia/handlers.go +++ b/handlers/zenvia/handlers.go @@ -3,7 +3,6 @@ package zenvia import ( "bytes" "context" - "encoding/json" "fmt" "net/http" "strings" @@ -12,6 +11,7 @@ import ( "github.com/buger/jsonparser" "github.com/nyaruka/courier" "github.com/nyaruka/courier/handlers" + "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/urns" ) @@ -223,11 +223,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch }) } - jsonBody, err := json.Marshal(payload) - if err != nil { - return status, err - } - + jsonBody := jsonx.MustMarshal(payload) sendURL := whatsappSendURL if channel.ChannelType() == "ZVS" { sendURL = smsSendURL