add courses
This commit is contained in:
13
courses-events/app/boto3clients.py
Normal file
13
courses-events/app/boto3clients.py
Normal 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()
|
||||
4
courses-events/app/config.py
Normal file
4
courses-events/app/config.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import os
|
||||
|
||||
COURSE_TABLE: str = os.environ.get('COURSE_TABLE') # type: ignore
|
||||
API_URL = 'https://eduseg.com.br/api'
|
||||
0
courses-events/app/events/__init__.py
Normal file
0
courses-events/app/events/__init__.py
Normal file
35
courses-events/app/events/daily_sync_course_metadata.py
Normal file
35
courses-events/app/events/daily_sync_course_metadata.py
Normal 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
|
||||
Reference in New Issue
Block a user