Skip to content

Commit

Permalink
Rework DA handler to new send
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Feb 22, 2024
1 parent 858d3d8 commit 6b57bec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
30 changes: 10 additions & 20 deletions handlers/dart/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,12 @@ func (h *handler) WriteMsgSuccessResponse(ctx context.Context, w http.ResponseWr
}

func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
// TODO convert functionality from legacy method below
return nil
}

func (h *handler) SendLegacy(ctx context.Context, msg courier.MsgOut, clog *courier.ChannelLog) (courier.StatusUpdate, error) {
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
if username == "" {
return nil, fmt.Errorf("no username set for %s channel", msg.Channel().ChannelType())
}

password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if password == "" {
return nil, fmt.Errorf("no password set for %s channel", msg.Channel().ChannelType())
if username == "" || password == "" {
return courier.ErrChannelConfig

Check warning on line 152 in handlers/dart/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dart/handler.go#L152

Added line #L152 was not covered by tests
}

status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusErrored, clog)
parts := handlers.SplitMsgByChannel(msg.Channel(), handlers.GetTextAndAttachments(msg), h.maxLength)
for i, part := range parts {
form := url.Values{
Expand All @@ -185,24 +175,24 @@ func (h *handler) SendLegacy(ctx context.Context, msg courier.MsgOut, clog *cour

req, err := http.NewRequest(http.MethodGet, partSendURL.String(), nil)
if err != nil {
return nil, err
return err

Check warning on line 178 in handlers/dart/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dart/handler.go#L178

Added line #L178 was not covered by tests
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, respBody, err := h.RequestHTTP(req, clog)
if err != nil || resp.StatusCode/100 != 2 {
return status, nil
if err != nil || resp.StatusCode/100 == 5 {
return courier.ErrConnectionFailed

Check warning on line 185 in handlers/dart/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dart/handler.go#L185

Added line #L185 was not covered by tests
} else if resp.StatusCode/100 != 2 {
return courier.ErrResponseStatus
}

responseCode := stringsx.Truncate(string(respBody), 3)
if responseCode != "000" {
clog.Error(courier.ErrorExternal(responseCode, errorCodes[responseCode]))
return status, nil
return courier.ErrFailedWithReason(responseCode, errorCodes[responseCode])
}

status.SetStatus(courier.MsgStatusWired)

}
return status, nil

return nil
}
11 changes: 3 additions & 8 deletions handlers/dart/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ var defaultSendTestCases = []OutgoingTestCase{
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"Simple Message"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "W",
},
{
Label: "Long Send",
Expand All @@ -123,7 +122,6 @@ var defaultSendTestCases = []OutgoingTestCase{
{Params: url.Values{"message": {"This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say,"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
{Params: url.Values{"message": {"I need to keep adding more things to make it work"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10.2"}}},
},
ExpectedMsgStatus: "W",
},
{
Label: "Send Attachment",
Expand All @@ -138,7 +136,6 @@ var defaultSendTestCases = []OutgoingTestCase{
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"My pic!\nhttps://foo.bar/image.jpg"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "W",
},
{
Label: "Error Sending",
Expand All @@ -152,7 +149,7 @@ var defaultSendTestCases = []OutgoingTestCase{
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"Error Message"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "E",
ExpectedError: courier.ErrResponseStatus,
},
{
Label: "Authentication Error",
Expand All @@ -166,8 +163,7 @@ var defaultSendTestCases = []OutgoingTestCase{
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"Simple Message"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "E",
ExpectedLogErrors: []*courier.ChannelError{courier.ErrorExternal("001", "Authentication error.")},
ExpectedError: courier.ErrFailedWithReason("001", "Authentication error."),
},
{
Label: "Account Expired",
Expand All @@ -181,8 +177,7 @@ var defaultSendTestCases = []OutgoingTestCase{
ExpectedRequests: []ExpectedRequest{
{Params: url.Values{"message": {"Simple Message"}, "sendto": {"250788383383"}, "original": {"2020"}, "userid": {"Username"}, "password": {"Password"}, "dcs": {"0"}, "udhl": {"0"}, "messageid": {"10"}}},
},
ExpectedMsgStatus: "E",
ExpectedLogErrors: []*courier.ChannelError{courier.ErrorExternal("101", "Account expired or invalid parameters.")},
ExpectedError: courier.ErrFailedWithReason("101", "Account expired or invalid parameters."),
},
}

Expand Down

0 comments on commit 6b57bec

Please sign in to comment.