85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
import json
|
|
import os
|
|
from datetime import date
|
|
from functools import partial
|
|
from typing import Any
|
|
|
|
from aws_lambda_powertools import Logger, Tracer
|
|
from aws_lambda_powertools.event_handler.api_gateway import (
|
|
APIGatewayHttpResolver,
|
|
CORSConfig,
|
|
)
|
|
from aws_lambda_powertools.event_handler.exceptions import NotFoundError, ServiceError
|
|
from aws_lambda_powertools.logging import correlation_paths
|
|
from aws_lambda_powertools.shared.json_encoder import Encoder
|
|
from aws_lambda_powertools.utilities.typing import LambdaContext
|
|
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
|
|
|
from api_gateway import JSONResponse
|
|
from boto3clients import dynamodb_client
|
|
from config import COURSE_TABLE
|
|
|
|
dyn = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
|
|
|
|
|
|
class JSONEncoder(Encoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, date):
|
|
return obj.isoformat()
|
|
|
|
return super().default(obj)
|
|
|
|
|
|
tracer = Tracer()
|
|
logger = Logger(__name__)
|
|
cors = CORSConfig(
|
|
allow_origin='*',
|
|
allow_headers=['Content-Type', 'X-Requested-With', 'Authorization'],
|
|
max_age=600,
|
|
allow_credentials=False,
|
|
)
|
|
app = APIGatewayHttpResolver(
|
|
enable_validation=True,
|
|
cors=cors,
|
|
debug='AWS_SAM_LOCAL' in os.environ,
|
|
serializer=partial(json.dumps, separators=(',', ':'), cls=JSONEncoder),
|
|
)
|
|
|
|
|
|
@app.get('/users/<user_id>')
|
|
@tracer.capture_method
|
|
def get_user(user_id: str):
|
|
return {'id': user_id}
|
|
|
|
|
|
@app.get('/users/<user_id>/emails')
|
|
@tracer.capture_method
|
|
def get_emails(user_id: str):
|
|
return [{'email': 'sergio@somosbeta.com.br'}]
|
|
|
|
|
|
@app.get('/courses/<course_id>')
|
|
@tracer.capture_method
|
|
def get_course(course_id: str):
|
|
return dyn.collection.get_item(
|
|
KeyPair(course_id, '0'),
|
|
exc_cls=NotFoundError,
|
|
)
|
|
|
|
|
|
@app.exception_handler(ServiceError)
|
|
def exc_error(exc: ServiceError):
|
|
return JSONResponse(
|
|
body={
|
|
'type': type(exc).__name__,
|
|
'message': str(exc),
|
|
},
|
|
status_code=exc.status_code,
|
|
)
|
|
|
|
|
|
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP)
|
|
@tracer.capture_lambda_handler
|
|
def lambda_handler(event: dict[str, Any], context: LambdaContext) -> dict[str, Any]:
|
|
return app.resolve(event, context)
|