Skip to content

Commit

Permalink
Merge pull request #90 from nyaruka/jiochat-media-req-builder-interface
Browse files Browse the repository at this point in the history
Add interface for media download request builder
  • Loading branch information
norkans7 committed Jan 25, 2018
2 parents ef0146f + dfa2ae7 commit 81a69b7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
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 {
mediaDownloadRequestBuilder, isMediaDownloadRequestBuilder := handler.(courier.MediaDownloadRequestBuilder)
if isMediaDownloadRequestBuilder {
req, err = mediaDownloadRequestBuilder.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
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
19 changes: 19 additions & 0 deletions handlers/jiochat/jiochat.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,22 @@ func (h *handler) DescribeURN(ctx context.Context, channel courier.Channel, urn

return map[string]string{"name": nickname}, nil
}

// BuildDownloadMediaRequest download media for message attachment
func (h *handler) BuildDownloadMediaRequest(ctx context.Context, channel courier.Channel, attachmentURL string) (*http.Request, error) {
parsedURL, err := url.Parse(attachmentURL)
if err != nil {
return nil, err
}

accessToken := getAccessToken(channel)

// first fetch our media
req, err := http.NewRequest("GET", parsedURL.String(), nil)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
if err != nil {
return nil, err
}

return req, nil
}

0 comments on commit 81a69b7

Please sign in to comment.