Skip to content
Snippets Groups Projects
Select Git revision
  • 80ed08a3e7328405c1104d468e6f7b0352b5fc1a
  • 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 5.29 KiB
    package sqs
    
    /*Package sqs provides a simple interface to send messages to AWS SQS*/
    
    import (
    	"context"
    	"encoding/json"
    	"fmt"
    	"io"
    	"sync"
    	"time"
    
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils"
    
    	"github.com/google/uuid"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/s3"
    
    	"github.com/aws/aws-sdk-go-v2/aws"
    	"github.com/aws/aws-sdk-go-v2/config"
    	"github.com/aws/aws-sdk-go-v2/service/sqs"
    	"github.com/aws/aws-sdk-go-v2/service/sqs/types"
    	"github.com/go-resty/resty/v2"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
    )
    
    var sqsClient *sqs.Client
    
    const SQSMessageOnS3Key = "message-on-s3"
    
    // Messenger sends an arbitrary message via SQS
    type Messenger struct {
    	QueueName          string
    	QueueURL           string
    	Region             string
    	S3Client           *s3.ClientWithHelpers
    	S3BucketName       string
    	MessageGroupID     *string
    	DelaySeconds       *int64
    	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.Client, error) {
    	cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(awsRegion))
    	if err != nil {
    		return nil, err
    	}
    
    	sqsClient := sqs.NewFromConfig(cfg)
    
    	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]types.MessageAttributeValue)
    
    	for key, val := range headers {
    		msgAttrs[key] = types.MessageAttributeValue{
    			DataType:    aws.String("String"),
    			StringValue: aws.String(val),
    		}
    	}
    
    	// Add request ID