api with oauth2 provider

This commit is contained in:
2025-09-25 23:17:28 -03:00
parent a3e13a113c
commit 187a064687
11 changed files with 1580 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
from typing import Generic, Mapping
from aws_lambda_powertools.event_handler import content_types
from aws_lambda_powertools.event_handler.api_gateway import Response, ResponseT
class JSONResponse(Response, Generic[ResponseT]):
def __init__(
self,
status_code: int,
body: ResponseT | None = None,
headers: Mapping[str, str | list[str]] | None = None,
):
super().__init__(
status_code,
content_types.APPLICATION_JSON,
body,
headers,
compress=True,
)

View File

@@ -0,0 +1,84 @@
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', 'X-Tenant'],
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)

View File

@@ -0,0 +1,22 @@
import os
from typing import TYPE_CHECKING
import boto3
if TYPE_CHECKING:
from mypy_boto3_dynamodb.client import DynamoDBClient
else:
DynamoDBClient = object
AWS_SAM_LOCAL = os.getenv('AWS_SAM_LOCAL')
def get_dynamodb_client() -> DynamoDBClient:
if not AWS_SAM_LOCAL:
return boto3.client('dynamodb')
host = 'host.docker.internal' if AWS_SAM_LOCAL else '127.0.0.1'
return boto3.client('dynamodb', endpoint_url=f'http://{host}:8000')
dynamodb_client: DynamoDBClient = get_dynamodb_client()

View File

@@ -0,0 +1,3 @@
import os
COURSE_TABLE: str = os.getenv('COURSE_TABLE') # type: ignore