-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Belo
authored and
Lucas Belo
committed
Oct 7, 2024
1 parent
b546d95
commit b055f31
Showing
9 changed files
with
298 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from typing import Optional | ||
|
||
from pydantic_settings import BaseSettings | ||
|
||
|
||
class Config(BaseSettings): | ||
TABLE_NAME: str = "" | ||
DYNAMODB_URL: Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,87 @@ | ||
from fastapi import FastAPI | ||
import uuid | ||
from typing import Union | ||
|
||
import jwt | ||
from fastapi import Depends, FastAPI, Header | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from mangum import Mangum | ||
from starlette import status | ||
|
||
from config import Config | ||
from models import Task | ||
from schemas import APITask, APITaskList, CreateTask, CloseTask | ||
from store import TaskStore | ||
|
||
app = FastAPI() | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins="*", | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
config = Config() | ||
|
||
|
||
def get_task_store() -> TaskStore: | ||
return TaskStore(config.TABLE_NAME, dynamodb_url=config.DYNAMODB_URL) | ||
|
||
|
||
def get_user_email(authorization: Union[str, None] = Header(default=None)) -> str: | ||
return jwt.decode(authorization, options={"verify_signature": False})[ | ||
"cognito:username" | ||
] | ||
|
||
|
||
@app.get("/api/health-check/") | ||
def health_check(): | ||
return {"message": "OK"} | ||
|
||
|
||
@app.post( | ||
"/api/create-task", response_model=APITask, status_code=status.HTTP_201_CREATED | ||
) | ||
def create_task( | ||
parameters: CreateTask, | ||
user_email: str = Depends(get_user_email), | ||
task_store: TaskStore = Depends(get_task_store), | ||
): | ||
task = Task.create(id_=uuid.uuid4(), title=parameters.title, owner=user_email) | ||
task_store.add(task) | ||
|
||
return task | ||
|
||
|
||
@app.get("/api/open-tasks", response_model=APITaskList) | ||
def open_tasks( | ||
user_email: str = Depends(get_user_email), | ||
task_store: TaskStore = Depends(get_task_store), | ||
): | ||
results=task_store.list_open(owner=user_email) | ||
print("Results: ", results) | ||
return APITaskList(results=results) | ||
|
||
|
||
handle = Mangum(app) | ||
|
||
|
||
@app.post("/api/close-task", response_model=APITask) | ||
def close_task( | ||
parameters: CloseTask, | ||
user_email: str = Depends(get_user_email), | ||
task_store: TaskStore = Depends(get_task_store), | ||
): | ||
task = task_store.get_by_id(task_id=parameters.id, owner=user_email) | ||
task.close() | ||
task_store.add(task) | ||
|
||
return task | ||
|
||
|
||
@app.get("/api/closed-tasks", response_model=APITaskList) | ||
def closed_tasks( | ||
user_email: str = Depends(get_user_email), | ||
task_store: TaskStore = Depends(get_task_store), | ||
): | ||
return APITaskList(results=task_store.list_closed(owner=user_email)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel, ConfigDict | ||
|
||
from models import TaskStatus | ||
|
||
|
||
class CreateTask(BaseModel): | ||
title: str | ||
|
||
|
||
class APITask(BaseModel): | ||
id: UUID | ||
title: str | ||
status: TaskStatus | ||
owner: str | ||
|
||
model_config = ConfigDict(from_attributes=True) | ||
|
||
|
||
class APITaskList(BaseModel): | ||
results: list[APITask] | ||
|
||
model_config = ConfigDict(from_attributes=True) | ||
|
||
|
||
class CloseTask(BaseModel): | ||
id: UUID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.