(feat) use background tasks forwarding and add missing documentation
This commit is contained in:
40
main.py
40
main.py
@@ -1,9 +1,37 @@
|
||||
from fastapi import FastAPI
|
||||
from base_types import Notification
|
||||
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
||||
from base_types import Notification, NotificationType
|
||||
import logging
|
||||
|
||||
app = FastAPI()
|
||||
app = FastAPI(
|
||||
title="Cloud Accelerator Coding Challenge",
|
||||
description="Simple Notification App for the Coding Challenge.",
|
||||
version="0.0.1",
|
||||
terms_of_service="TOS",
|
||||
contact={
|
||||
"name": "Nicolas Sebastian Schuler",
|
||||
"url": "https://n-schuler.dev",
|
||||
"email": "mail@n-schuler.dev",
|
||||
},
|
||||
license_info={"name": "MIT", "identifier": "MIT"},
|
||||
)
|
||||
|
||||
|
||||
@app.post("/notification")
|
||||
async def notification(ntfy: Notification):
|
||||
print(ntfy)
|
||||
def log_notification(ntfy: Notification):
|
||||
"""Log Notification."""
|
||||
logging.warning(ntfy)
|
||||
|
||||
|
||||
@app.post("/notification", status_code=204)
|
||||
async def notification(ntfy: Notification, background_tasks: BackgroundTasks):
|
||||
"""Forward Notification
|
||||
|
||||
* Forwards Notification if `Type: Warning`.
|
||||
"""
|
||||
match ntfy.type:
|
||||
case NotificationType.INFO:
|
||||
pass
|
||||
case NotificationType.WARNING:
|
||||
background_tasks.add_task(log_notification, ntfy)
|
||||
case _:
|
||||
# Already catched by Pydantic
|
||||
raise HTTPException(status_code=400, detail="Malformed Request Body")
|
||||
|
||||
Reference in New Issue
Block a user