"""SQS connector.""" import json import math import uuid from typing import Any import boto3 from notifications.config import AWS_REGION client = boto3.client('sqs', region_name=AWS_REGION) def send_messages(queue_url: str, messages: list[dict[str, Any]]) -> tuple[int, int]: """Add messages to queue via SQS.""" added = 0 failed = 0 # split messages into batches batch_size = 10 batches = [ messages[x * batch_size : (x * batch_size) + batch_size] for x in range(0, math.ceil(len(messages) / batch_size)) ] # add each batch to sqs queue for batch in batches: results = client.send_message_batch( QueueUrl=queue_url, Entries=[{'Id': str(uuid.uuid4()), 'MessageBody': json.dumps(x)} for x in batch], ) added += len(results.get('Successful', [])) failed += len(results.get('Failed', [])) return (added, failed)