(feat) use background tasks forwarding and add missing documentation

This commit is contained in:
Nicolas Sebastian Schuler
2025-07-16 09:48:37 +02:00
parent 128a3b8809
commit 8b13bdbb6d
4 changed files with 99 additions and 6 deletions

View File

@@ -3,11 +3,15 @@ from pydantic import BaseModel
class NotificationType(StrEnum):
"""Notification Types"""
INFO = "Info"
WARNING = "Warning"
class Notification(BaseModel):
"""Notification"""
type: NotificationType
name: str
description: str

40
main.py
View File

@@ -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")

View File

@@ -7,3 +7,9 @@ requires-python = ">=3.12"
dependencies = [
"fastapi[standard]>=0.116.1",
]
[tool.uv]
dev-dependencies = [
"httpx>=0.28.1",
"pytest>=8.4.1",
]

55
uv.lock generated
View File

@@ -41,9 +41,21 @@ dependencies = [
{ name = "fastapi", extra = ["standard"] },
]
[package.dev-dependencies]
dev = [
{ name = "httpx" },
{ name = "pytest" },
]
[package.metadata]
requires-dist = [{ name = "fastapi", extras = ["standard"], specifier = ">=0.116.1" }]
[package.metadata.requires-dev]
dev = [
{ name = "httpx", specifier = ">=0.28.1" },
{ name = "pytest", specifier = ">=8.4.1" },
]
[[package]]
name = "click"
version = "8.2.1"
@@ -217,6 +229,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442 },
]
[[package]]
name = "iniconfig"
version = "2.1.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/f2/97/ebf4da567aa6827c909642694d71c9fcf53e5b504f2d96afea02718862f3/iniconfig-2.1.0.tar.gz", hash = "sha256:3abbd2e30b36733fee78f9c7f7308f2d0050e88f0087fd25c2645f63c773e1c7", size = 4793 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/2c/e1/e6716421ea10d38022b952c159d5161ca1193197fb744506875fbb87ea7b/iniconfig-2.1.0-py3-none-any.whl", hash = "sha256:9deba5723312380e77435581c6bf4935c94cbfab9b1ed33ef8d238ea168eb760", size = 6050 },
]
[[package]]
name = "jinja2"
version = "3.1.6"
@@ -288,6 +309,24 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/b3/38/89ba8ad64ae25be8de66a6d463314cf1eb366222074cfda9ee839c56a4b4/mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8", size = 9979 },
]
[[package]]
name = "packaging"
version = "25.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/a1/d4/1fc4078c65507b51b96ca8f8c3ba19e6a61c8253c72794544580a7b6c24d/packaging-25.0.tar.gz", hash = "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f", size = 165727 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469 },
]
[[package]]
name = "pluggy"
version = "1.6.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/f9/e2/3e91f31a7d2b083fe6ef3fa267035b518369d9511ffab804f839851d2779/pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3", size = 69412 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538 },
]
[[package]]
name = "pydantic"
version = "2.11.7"
@@ -359,6 +398,22 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/c7/21/705964c7812476f378728bdf590ca4b771ec72385c533964653c68e86bdc/pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b", size = 1225217 },
]
[[package]]
name = "pytest"
version = "8.4.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "colorama", marker = "sys_platform == 'win32'" },
{ name = "iniconfig" },
{ name = "packaging" },
{ name = "pluggy" },
{ name = "pygments" },
]
sdist = { url = "https://files.pythonhosted.org/packages/08/ba/45911d754e8eba3d5a841a5ce61a65a685ff1798421ac054f85aa8747dfb/pytest-8.4.1.tar.gz", hash = "sha256:7c67fd69174877359ed9371ec3af8a3d2b04741818c51e5e99cc1742251fa93c", size = 1517714 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/29/16/c8a903f4c4dffe7a12843191437d7cd8e32751d5de349d45d3fe69544e87/pytest-8.4.1-py3-none-any.whl", hash = "sha256:539c70ba6fcead8e78eebbf1115e8b589e7565830d7d006a8723f19ac8a0afb7", size = 365474 },
]
[[package]]
name = "python-dotenv"
version = "1.1.1"