update batch
This commit is contained in:
@@ -1,3 +1,14 @@
|
||||
import os
|
||||
|
||||
import boto3
|
||||
|
||||
|
||||
def get_dynamodb_client():
|
||||
if os.getenv('AWS_LAMBDA_FUNCTION_NAME'):
|
||||
return boto3.client('dynamodb')
|
||||
|
||||
return boto3.client('dynamodb', endpoint_url='http://localhost:8000')
|
||||
|
||||
|
||||
dynamodb_client = get_dynamodb_client()
|
||||
s3_client = boto3.client('s3')
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
import os
|
||||
|
||||
USER_TABLE: str = os.getenv('USER_TABLE') # type: ignore
|
||||
CHUNK_SIZE = 50
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import csv
|
||||
from io import StringIO
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from aws_lambda_powertools.utilities.data_classes import (
|
||||
EventBridgeEvent,
|
||||
@@ -9,6 +10,11 @@ from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
|
||||
from boto3clients import s3_client
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from mypy_boto3_s3.client import S3Client
|
||||
else:
|
||||
S3Client = object
|
||||
|
||||
transport_params = {'client': s3_client}
|
||||
|
||||
|
||||
@@ -36,7 +42,7 @@ def _get_s3_object_range(
|
||||
*,
|
||||
start_byte: int,
|
||||
end_byte: int,
|
||||
s3_client,
|
||||
s3_client: S3Client,
|
||||
) -> StringIO:
|
||||
bucket, key = s3_uri.replace('s3://', '').split('/', 1)
|
||||
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
import urllib.parse as urllib_parse
|
||||
from email.utils import parseaddr
|
||||
from typing import Any, Iterator
|
||||
|
||||
from aws_lambda_powertools import Logger
|
||||
from aws_lambda_powertools.utilities.data_classes import SESEvent, event_source
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import USER_TABLE
|
||||
from ses_utils import get_header_value
|
||||
|
||||
logger = Logger(__name__)
|
||||
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
@logger.inject_lambda_context
|
||||
@@ -15,31 +20,21 @@ def lambda_handler(event: SESEvent, context: LambdaContext) -> dict:
|
||||
ses = event.record.ses
|
||||
to = urllib_parse.unquote(ses.receipt.recipients[0]).lower()
|
||||
name, email_from = parseaddr(get_header_value(ses.mail.headers, 'from'))
|
||||
subject = get_header_value(
|
||||
ses.mail.headers,
|
||||
'subject',
|
||||
default='',
|
||||
raise_on_missing=False,
|
||||
|
||||
org_id = user_layer.collection.get_item(
|
||||
KeyPair('email', SortKey(to, path_spec='user_id')),
|
||||
raise_on_error=False,
|
||||
default={},
|
||||
)
|
||||
|
||||
if email_from == 'sergio@somosbeta.com.br':
|
||||
return {'disposition': 'CONTINUE'}
|
||||
if not org_id:
|
||||
return {'disposition': 'STOP_RULE_SET'}
|
||||
|
||||
return {'disposition': 'STOP_RULE_SET'}
|
||||
print(
|
||||
{
|
||||
'id': f'mailbox#{org_id}',
|
||||
'sk': ses.mail.message_id,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def get_header_value(
|
||||
headers: Iterator,
|
||||
header_name: str,
|
||||
*,
|
||||
default: Any = None,
|
||||
raise_on_missing: bool = True,
|
||||
) -> str:
|
||||
for header in headers:
|
||||
if header.name.lower() == header_name:
|
||||
return header.value
|
||||
|
||||
if raise_on_missing:
|
||||
raise ValueError(f'{header_name} not found.')
|
||||
|
||||
return default
|
||||
return {'disposition': 'CONTINUE'}
|
||||
|
||||
Reference in New Issue
Block a user