Skip to content
Snippets Groups Projects
Select Git revision
  • 1e86cfd6860b4e387206299fc101984e6d61259d
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

sqs.go

Blame
  • sqs.go 4.79 KiB
    package sqs
    
    /*Package sqs provides a simple interface to send messages to AWS SQS*/
    
    import (
    	"encoding/json"
    	"fmt"
    	"github.com/google/uuid"
    	"gitlab.com/uafrica/go-utils/s3"
    	"gitlab.com/uafrica/go-utils/string_utils"
    	"io/ioutil"
    	"time"
    
    	"github.com/aws/aws-sdk-go/aws"
    	"github.com/aws/aws-sdk-go/aws/session"
    	"github.com/aws/aws-sdk-go/service/sqs"
    	"gitlab.com/uafrica/go-utils/logs"
    )
    
    var sqsClient *sqs.SQS
    
    const SQSMessageOnS3Key = "message-on-s3"
    
    // Messenger sends an arbitrary message via SQS
    type Messenger struct {
    	QueueName          string
    	QueueURL           string
    	Region             string
    	S3Session          *s3.SessionWithHelpers
    	S3BucketName       string
    	MessageGroupID     *string
    	RequestIDHeaderKey string
    }
    
    // NewSQSClient constructs a Messenger which sends messages to an SQS queue
    // awsRegion - region that the queue was created
    // awsQueue - name of the queue
    // Note: Calling code needs SQS IAM permissions
    func NewSQSClient(awsRegion string) (*sqs.SQS, error) {
    	// Make an AWS session
    	sess, err := session.NewSessionWithOptions(session.Options{
    		Config: aws.Config{
    			Region: aws.String(awsRegion),
    		},
    	})
    
    	if err != nil {
    		return nil, err
    	}
    
    	// Create SQS service
    	sqsClient = sqs.New(sess)
    	return sqsClient, err
    }
    
    // SendSQSMessage sends a message to the queue associated with the messenger
    // headers - string message attributes of the SQS message (see AWS SQS documentation)
    // body - body of the SQS message (see AWS SQS documentation)
    func (m *Messenger) SendSQSMessage(headers map[string]string, body string, currentRequestID *string, sqsType string) (string, error) {
    	msgAttrs := make(map[string]*sqs.MessageAttributeValue)
    
    	for key, val := range headers {
    		msgAttrs[key] = &sqs.MessageAttributeValue{
    			DataType:    aws.String("String"),
    			StringValue: aws.String(val),
    		}
    	}
    
    	// Add request ID
    	if currentRequestID != nil {