Skip to content

Commit

Permalink
Cleanup some handler code
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Oct 3, 2023
1 parent a8dbe8e commit 89c7fa5
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 84 deletions.
6 changes: 2 additions & 4 deletions handlers/bandwidth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 2 additions & 4 deletions handlers/dialog360/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions handlers/facebook_legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package facebook_legacy
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions handlers/firebase/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions handlers/freshchat/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io"
Expand All @@ -21,6 +20,7 @@ import (

"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
)

Expand Down Expand Up @@ -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))

Expand Down
7 changes: 2 additions & 5 deletions handlers/messagebird/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"strconv"

"fmt"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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))

Expand Down
27 changes: 11 additions & 16 deletions handlers/meta/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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

Check warning on line 1100 in handlers/meta/handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers/meta/handlers.go#L1100

Added line #L1100 was not covered by tests
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions handlers/playmobile/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package playmobile
import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
Expand All @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions handlers/rocketchat/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package rocketchat
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
)

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions handlers/twitter/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"mime/multipart"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 2 additions & 5 deletions handlers/wavy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package wavy
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/jsonx"
)

var (
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 4 additions & 14 deletions handlers/whatsapp_legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 89c7fa5

Please sign in to comment.