Send and receive SMS messages using your Android phone programmatically via a simple HTTP API
Added cache for csv uploads to avoid 1000s of duplicate http requests
Jake Daynesf2a0ab8a6483
diff --git a/api/pkg/validators/bulk_message_handler_validator.go b/api/pkg/validators/bulk_message_handler_validator.goindex b96829af..137442d0 100644--- a/api/pkg/validators/bulk_message_handler_validator.go+++ b/api/pkg/validators/bulk_message_handler_validator.go@@ -17,6 +17,7 @@ import ("github.com/NdoleStudio/httpsms/pkg/requests""github.com/NdoleStudio/httpsms/pkg/services""github.com/NdoleStudio/httpsms/pkg/telemetry"+ "github.com/NdoleStudio/httpsms/pkg/cache""github.com/dustin/go-humanize""github.com/jszwec/csvutil""github.com/nyaruka/phonenumbers"@@ -30,6 +31,7 @@ type BulkMessageHandlerValidator struct {userService *services.UserServicelogger telemetry.Loggertracer telemetry.Tracer+ cache cache.Cache}// NewBulkMessageHandlerValidator creates a new handlers.BulkMessageHandlerValidator validator@@ -38,12 +40,14 @@ func NewBulkMessageHandlerValidator(tracer telemetry.Tracer,phoneService *services.PhoneService,userService *services.UserService,+ appCache cache.Cache,) (v *BulkMessageHandlerValidator) {return &BulkMessageHandlerValidator{logger: logger.WithService(fmt.Sprintf("%T", v)),tracer: tracer,userService: userService,phoneService: phoneService,+ cache: appCache,}}@@ -79,7 +83,7 @@ func (v *BulkMessageHandlerValidator) ValidateStore(ctx context.Context, userIDmessages[index] = message.Sanitize()}- result = v.validateMessages(messages)+ result = v.validateMessages(ctx, messages)if len(result) != 0 {return messages, result}@@ -215,7 +219,7 @@ func (v *BulkMessageHandlerValidator) parseCSV(ctxLogger telemetry.Logger, userreturn messages, url.Values{}}-func (v *BulkMessageHandlerValidator) validateMessages(messages []*requests.BulkMessage) url.Values {+func (v *BulkMessageHandlerValidator) validateMessages(ctx context.Context, messages []*requests.BulkMessage) url.Values {result := url.Values{}for index, message := range messages {@@ -238,7 +242,7 @@ func (v *BulkMessageHandlerValidator) validateMessages(messages []*requests.Bulk} else if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {result.Add("document", fmt.Sprintf("Row [%d]: The attachment URL [%s] must use http or https.", index+2, cleanURL))} else {- if err := validateAttachmentURL(cleanURL); err != nil {+ if err := validateAttachmentURL(ctx, v.cache, cleanURL); err != nil {result.Add("attachments", fmt.Sprintf("Row [%d]: The attachment URL [%s] failed validation: %s", index+2, cleanURL, err.Error()))}}diff --git a/api/pkg/validators/message_handler_validator.go b/api/pkg/validators/message_handler_validator.goindex ec23ab78..52741891 100644--- a/api/pkg/validators/message_handler_validator.go+++ b/api/pkg/validators/message_handler_validator.go@@ -7,6 +7,7 @@ import ("strings""time"+ "github.com/NdoleStudio/httpsms/pkg/cache""github.com/NdoleStudio/httpsms/pkg/repositories""github.com/NdoleStudio/httpsms/pkg/services""github.com/palantir/stacktrace"@@ -25,6 +26,7 @@ type MessageHandlerValidator struct {tracer telemetry.TracerphoneService *services.PhoneServicetokenValidator *TurnstileTokenValidator+ cache cache.Cache}// NewMessageHandlerValidator creates a new handlers.MessageHandler validator@@ -33,12 +35,14 @@ func NewMessageHandlerValidator(tracer telemetry.Tracer,phoneService *services.PhoneService,tokenValidator *TurnstileTokenValidator,+ appCache cache.Cache,) (v *MessageHandlerValidator) {return &MessageHandlerValidator{logger: logger.WithService(fmt.Sprintf("%T", v)),tracer: tracer,phoneService: phoneService,tokenValidator: tokenValidator,+ cache: appCache,}}@@ -124,7 +128,7 @@ func (validator MessageHandlerValidator) ValidateMessageSend(ctx context.Context} else if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {result.Add("attachments", fmt.Sprintf("attachment at index %d must use http or https scheme", i))} else {- if err := validateAttachmentURL(attachment.URL); err != nil {+ if err := validateAttachmentURL(ctx, validator.cache, attachment.URL); err != nil {result.Add("attachments", fmt.Sprintf("attachment at index %d failed validation: %s", i, err.Error()))}}@@ -199,7 +203,7 @@ func (validator MessageHandlerValidator) ValidateMessageBulkSend(ctx context.Con} else if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {result.Add("attachments", fmt.Sprintf("attachment at index %d must use http or https scheme", i))} else {- if err := validateAttachmentURL(attachment.URL); err != nil {+ if err := validateAttachmentURL(ctx, validator.cache, attachment.URL); err != nil {result.Add("attachments", fmt.Sprintf("attachment at index %d failed validation: %s", i, err.Error()))}}diff --git a/api/pkg/validators/validator.go b/api/pkg/validators/validator.goindex bcef0a9b..850d1e76 100644--- a/api/pkg/validators/validator.go+++ b/api/pkg/validators/validator.go@@ -1,6 +1,7 @@package validatorsimport (+ "context""fmt""net/http""net/url"@@ -8,6 +9,7 @@ import ("strings""time"+ "github.com/NdoleStudio/httpsms/pkg/cache""github.com/NdoleStudio/httpsms/pkg/events""github.com/nyaruka/phonenumbers"@@ -163,31 +165,53 @@ func (validator *validator) ValidateUUID(ID string, name string) url.Values {return v.ValidateStruct()}-func validateAttachmentURL(attachmentURL string) error {+func validateAttachmentURL(ctx context.Context, c cache.Cache, attachmentURL string) error {+ cacheKey := "mms-url-validation:" + attachmentURL++ if cachedVal, err := c.Get(ctx, cacheKey); err == nil {+ if cachedVal == "valid" {+ return nil+ }+ return fmt.Errorf(cachedVal)+ }+client := &http.Client{Timeout: 5 * time.Second,}req, err := http.NewRequest(http.MethodHead, attachmentURL, nil)if err != nil {- return fmt.Errorf("invalid url format")+ errMsg := fmt.Sprintf("invalid url format")+ saveToCache(ctx, c, cacheKey, errMsg)+ return fmt.Errorf(errMsg)}resp, err := client.Do(req)if err != nil {- return fmt.Errorf("could not reach the url")+ errMsg := fmt.Sprintf("could not reach the url")+ saveToCache(ctx, c, cacheKey, errMsg)+ return fmt.Errorf(errMsg)}defer resp.Body.Close()if resp.StatusCode < 200 || resp.StatusCode >= 400 {- return fmt.Errorf("url returned an error status code: %d", resp.StatusCode)+ errMsg := fmt.Sprintf("url returned an error status code: %d", resp.StatusCode)+ saveToCache(ctx, c, cacheKey, errMsg)+ return fmt.Errorf(errMsg)}const maxSizeBytes = 1.5 * 1024 * 1024if resp.ContentLength > int64(maxSizeBytes) {- return fmt.Errorf("file size (%.2f MB) exceeds the 1.5 MB carrier limit", float64(resp.ContentLength)/(1024*1024))+ errMsg := fmt.Sprintf("file size (%.2f MB) exceeds the 1.5 MB carrier limit", float64(resp.ContentLength)/(1024*1024))+ saveToCache(ctx, c, cacheKey, errMsg)+ return fmt.Errorf(errMsg)}+ saveToCache(ctx, c, cacheKey, "valid")return nil}++func saveToCache(ctx context.Context, c cache.Cache, key string, value string) {+ _ = c.Set(ctx, key, value, 24*time.Hour)+}