Skip to content

Commit

Permalink
Merge 3bd77dd into ed9d454
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Feb 2, 2018
2 parents ed9d454 + 3bd77dd commit 9e0ce0e
Show file tree
Hide file tree
Showing 32 changed files with 818 additions and 39 deletions.
4 changes: 4 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/garyburd/redigo/redis"
"github.com/nyaruka/courier/config"
"github.com/nyaruka/gocommon/urns"
)
Expand Down Expand Up @@ -71,6 +72,9 @@ type Backend interface {

// Status returns a string describing the current status, this can detail queue sizes or other attributes
Status() string

// RedisPool returns the redisPool for this backend
RedisPool() *redis.Pool
}

// NewBackend creates the type of backend passed in
Expand Down
5 changes: 5 additions & 0 deletions backends/rapidpro/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ func (b *backend) Cleanup() error {
return b.redisPool.Close()
}

// RedisPool returns the redisPool for this backend
func (b *backend) RedisPool() *redis.Pool {
return b.redisPool
}

// NewBackend creates a new RapidPro backend
func newBackend(config *config.Courier) courier.Backend {
return &backend{
Expand Down
34 changes: 28 additions & 6 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ func writeMsg(ctx context.Context, b *backend, msg courier.Msg) error {
return nil
}

channel := m.Channel()

// if we have media, go download it to S3
for i, attachment := range m.Attachments_ {
if strings.HasPrefix(attachment, "http") {
url, err := downloadMediaToS3(b, m.OrgID_, m.UUID_, attachment)
url, err := downloadMediaToS3(ctx, b, channel, m.OrgID_, m.UUID_, attachment)
if err != nil {
return err
}
Expand Down Expand Up @@ -181,17 +183,37 @@ func readMsgFromDB(b *backend, id courier.MsgID) (*DBMsg, error) {
// Media download and classification
//-----------------------------------------------------------------------------

func downloadMediaToS3(b *backend, orgID OrgID, msgUUID courier.MsgUUID, mediaURL string) (string, error) {
func downloadMediaToS3(ctx context.Context, b *backend, channel courier.Channel, orgID OrgID, msgUUID courier.MsgUUID, mediaURL string) (string, error) {

parsedURL, err := url.Parse(mediaURL)
if err != nil {
return "", err
}

// first fetch our media
req, err := http.NewRequest("GET", mediaURL, nil)
if err != nil {
return "", err
var req *http.Request
handler := courier.GetHandler(channel.ChannelType())
if handler != nil {
builder, isBuilder := handler.(courier.MediaDownloadRequestBuilder)
if isBuilder {
req, err = builder.BuildDownloadMediaRequest(ctx, channel, parsedURL.String())

// in the case of errors, we log the error but move onwards anyways
if err != nil {
logrus.WithField("channel_uuid", channel.UUID()).WithField("channel_type", channel.ChannelType()).WithField("media_url", mediaURL).WithError(err).Error("unable to build media download request")
}
}
}

if req == nil {

// first fetch our media
req, err = http.NewRequest("GET", mediaURL, nil)
if err != nil {
return "", err
}

}

resp, err := utils.GetHTTPClient().Do(req)
if err != nil {
return "", err
Expand Down
1 change: 1 addition & 0 deletions cmd/courier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
_ "github.com/nyaruka/courier/handlers/hub9"
_ "github.com/nyaruka/courier/handlers/infobip"
_ "github.com/nyaruka/courier/handlers/jasmin"
_ "github.com/nyaruka/courier/handlers/jiochat"
_ "github.com/nyaruka/courier/handlers/kannel"
_ "github.com/nyaruka/courier/handlers/m3tech"
_ "github.com/nyaruka/courier/handlers/nexmo"
Expand Down
5 changes: 5 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type URNDescriber interface {
DescribeURN(context.Context, Channel, urns.URN) (map[string]string, error)
}

// MediaDownloadRequestBuilder is the interface handlers which can allow a custom way to download attachment media for messages should satisfy
type MediaDownloadRequestBuilder interface {
BuildDownloadMediaRequest(context.Context, Channel, string) (*http.Request, error)
}

// RegisterHandler adds a new handler for a channel type, this is called by individual handlers when they are initialized
func RegisterHandler(handler ChannelHandler) {
registeredHandlers[handler.ChannelType()] = handler
Expand Down
4 changes: 2 additions & 2 deletions handlers/africastalking/africastalking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ func TestSending(t *testing.T) {
configIsShared: true,
})

RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases)
RunChannelSendTestCases(t, sharedChannel, newHandler(), sharedSendTestCases)
RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil)
RunChannelSendTestCases(t, sharedChannel, newHandler(), sharedSendTestCases, nil)
}
2 changes: 1 addition & 1 deletion handlers/blackmyna/blackmyna_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ func TestSending(t *testing.T) {
courier.ConfigAPIKey: "KEY",
})

RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases)
RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil)
}
2 changes: 1 addition & 1 deletion handlers/clickatell/clickatell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestSending(t *testing.T) {
courier.ConfigAPIKey: "API-KEY",
})

RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases)
RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil)
}

var testChannels = []courier.Channel{
Expand Down
2 changes: 1 addition & 1 deletion handlers/dart/dart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ func TestSending(t *testing.T) {
courier.ConfigPassword: "Password",
})

RunChannelSendTestCases(t, defaultDAChannel, NewHandler("DA", "Dartmedia", sendURL, maxMsgLength), defaultSendTestCases)
RunChannelSendTestCases(t, defaultDAChannel, NewHandler("DA", "Dartmedia", sendURL, maxMsgLength), defaultSendTestCases, nil)
}
2 changes: 1 addition & 1 deletion handlers/dmark/dmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ func TestSending(t *testing.T) {
courier.ConfigAuthToken: "Authy",
})

RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases)
RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil)
}
8 changes: 4 additions & 4 deletions handlers/external/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ func TestSending(t *testing.T) {
courier.ConfigSendMethod: http.MethodPut,
})

RunChannelSendTestCases(t, getChannel, newHandler(), getSendTestCases)
RunChannelSendTestCases(t, postChannel, newHandler(), postSendTestCases)
RunChannelSendTestCases(t, jsonChannel, newHandler(), jsonSendTestCases)
RunChannelSendTestCases(t, xmlChannel, newHandler(), xmlSendTestCases)
RunChannelSendTestCases(t, getChannel, newHandler(), getSendTestCases, nil)
RunChannelSendTestCases(t, postChannel, newHandler(), postSendTestCases, nil)
RunChannelSendTestCases(t, jsonChannel, newHandler(), jsonSendTestCases, nil)
RunChannelSendTestCases(t, xmlChannel, newHandler(), xmlSendTestCases, nil)
}
2 changes: 1 addition & 1 deletion handlers/facebook/facebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,5 +456,5 @@ func TestSending(t *testing.T) {
// shorter max msg length for testing
maxMsgLength = 100
var defaultChannel = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "FB", "2020", "US", map[string]interface{}{courier.ConfigAuthToken: "access_token"})
RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases)
RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil)
}
2 changes: 1 addition & 1 deletion handlers/infobip/infobip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,5 @@ func TestSending(t *testing.T) {
courier.ConfigUsername: "Username",
})

RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases)
RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil)
}
2 changes: 1 addition & 1 deletion handlers/jasmin/jasmin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ func TestSending(t *testing.T) {
"password": "Password",
"username": "Username"})

RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases)
RunChannelSendTestCases(t, defaultChannel, newHandler(), defaultSendTestCases, nil)
}
Loading

0 comments on commit 9e0ce0e

Please sign in to comment.