chore: initialize containerized Python learning workbook (2026 DevOps/SRE)
This commit is contained in:
commit
ae4c421815
63 files changed
+1273
No files matched your search
@@ -0,0 +1,10 @@
|
||||
# 02 — Web API z `FastAPI` + `Pydantic`
|
||||
|
||||
## Zadania (`api_fastapi.py`)
|
||||
1. `GET /health` -> `{"status": "ok"}`.
|
||||
2. `POST /echo` -> przyjmuje `EchoRequest(message: str)`; odrzuca pusty napis (po `strip()`) kodem `400`, w przeciwnym razie zwraca `EchoResponse(echoed=..., length=...)`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test MOD=03-ecosystems/02-web-api-fastapi
|
||||
```
|
||||
@@ -0,0 +1,26 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI(title="Python Learning API")
|
||||
|
||||
|
||||
class EchoRequest(BaseModel):
|
||||
message: str
|
||||
|
||||
|
||||
class EchoResponse(BaseModel):
|
||||
echoed: str
|
||||
length: int
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
def health() -> dict[str, str]:
|
||||
raise NotImplementedError("Zwróć {'status': 'ok'}")
|
||||
|
||||
|
||||
@app.post("/echo", response_model=EchoResponse)
|
||||
def echo(payload: EchoRequest) -> EchoResponse:
|
||||
_ = HTTPException
|
||||
raise NotImplementedError(
|
||||
"Odrzuć pusty payload.message przez HTTPException(400), inaczej zwróć EchoResponse"
|
||||
)
|
||||
@@ -0,0 +1,19 @@
|
||||
from api_fastapi import app
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_health() -> None:
|
||||
resp = client.get("/health")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json() == {"status": "ok"}
|
||||
|
||||
|
||||
def test_echo_validation() -> None:
|
||||
ok = client.post("/echo", json={"message": "devops"})
|
||||
assert ok.status_code == 200
|
||||
assert ok.json() == {"echoed": "devops", "length": 6}
|
||||
|
||||
bad = client.post("/echo", json={"message": " "})
|
||||
assert bad.status_code == 400
|
||||
Reference in new issue
Block a user