add courses

This commit is contained in:
2025-07-02 19:50:53 -03:00
parent 8901f44ca3
commit 9b927bdbcd
14 changed files with 1413 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
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()

View File

@@ -0,0 +1,4 @@
import os
COURSE_TABLE: str = os.environ.get('COURSE_TABLE') # type: ignore
API_URL = 'https://eduseg.com.br/api'

View File

View File

@@ -0,0 +1,35 @@
import requests
from aws_lambda_powertools.utilities.data_classes import (
DynamoDBStreamEvent,
event_source,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer
from boto3clients import dynamodb_client
from config import API_URL, COURSE_TABLE
course_layer = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
@event_source(data_class=DynamoDBStreamEvent)
def lambda_handler(event: DynamoDBStreamEvent, context: LambdaContext) -> bool:
r = requests.get(f'{API_URL}/courses.json')
r.raise_for_status()
data = r.json()
now_ = now()
with course_layer.batch_writer() as batch:
for course in data:
batch.put_item(
{
'id': {'S': course['id']},
'sk': {'S': 'metadata#unit_price'},
'unit_price': {'N': str(course['metadata']['unit_price'])},
'create_date': {'S': now_.isoformat()},
}
)
return True