Skip to content

Commit

Permalink
pass context down to backend to support timeouts for Zenvia
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Dec 20, 2017
1 parent 78b99d1 commit 6abe598
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions handlers/zenvia/zenvia.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zenvia

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -121,7 +122,7 @@ func (h *handler) Initialize(s courier.Server) error {
}

// ReceiveMessage is our HTTP handler function for incoming messages
func (h *handler) ReceiveMessage(channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
func (h *handler) ReceiveMessage(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
// get our params
zvMsg := &messageRequest{}
err := handlers.DecodeAndValidateJSON(zvMsg, r)
Expand All @@ -143,16 +144,16 @@ func (h *handler) ReceiveMessage(channel courier.Channel, w http.ResponseWriter,
msg := h.Backend().NewIncomingMsg(channel, urn, zvMsg.CallbackMORequest.Text).WithExternalID(zvMsg.CallbackMORequest.ExternalID).WithReceivedOn(date.UTC())

// and finally queue our message
err = h.Backend().WriteMsg(msg)
err = h.Backend().WriteMsg(ctx, msg)
if err != nil {
return nil, err
}

return []courier.Event{msg}, courier.WriteMsgSuccess(w, r, []courier.Msg{msg})
return []courier.Event{msg}, courier.WriteMsgSuccess(ctx, w, r, []courier.Msg{msg})
}

// StatusMessage is our HTTP handler function for status updates
func (h *handler) StatusMessage(channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
func (h *handler) StatusMessage(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
// get our params
zvStatus := &statusRequest{}
err := handlers.DecodeAndValidateJSON(zvStatus, r)
Expand All @@ -167,17 +168,17 @@ func (h *handler) StatusMessage(channel courier.Channel, w http.ResponseWriter,

// write our status
status := h.Backend().NewMsgStatusForExternalID(channel, zvStatus.CallbackMTRequest.ID, msgStatus)
err = h.Backend().WriteMsgStatus(status)
err = h.Backend().WriteMsgStatus(ctx, status)
if err != nil {
return nil, err
}

return []courier.Event{status}, courier.WriteStatusSuccess(w, r, []courier.MsgStatus{status})
return []courier.Event{status}, courier.WriteStatusSuccess(ctx, w, r, []courier.MsgStatus{status})

}

// SendMsg sends the passed in message, returning any error
func (h *handler) SendMsg(msg courier.Msg) (courier.MsgStatus, error) {
func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStatus, error) {
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
if username == "" {
return nil, fmt.Errorf("no username set for Zenvia channel")
Expand Down

0 comments on commit 6abe598

Please sign in to comment.