8 changed files with 159 additions and 7 deletions
@ -0,0 +1,30 @@ |
|||||
|
from fastapi import Depends, FastAPI, HTTPException |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
data = { |
||||
|
"plumbus": {"description": "Freshly pickled plumbus", "owner": "Morty"}, |
||||
|
"portal-gun": {"description": "Gun to create portals", "owner": "Rick"}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
class OwnerError(Exception): |
||||
|
pass |
||||
|
|
||||
|
|
||||
|
def get_username(): |
||||
|
try: |
||||
|
yield "Rick" |
||||
|
except OwnerError as e: |
||||
|
raise HTTPException(status_code=400, detail=f"Owner error: {e}") |
||||
|
|
||||
|
|
||||
|
@app.get("/items/{item_id}") |
||||
|
def get_item(item_id: str, username: str = Depends(get_username)): |
||||
|
if item_id not in data: |
||||
|
raise HTTPException(status_code=404, detail="Item not found") |
||||
|
item = data[item_id] |
||||
|
if item["owner"] != username: |
||||
|
raise OwnerError(username) |
||||
|
return item |
@ -0,0 +1,31 @@ |
|||||
|
from fastapi import Depends, FastAPI, HTTPException |
||||
|
from typing_extensions import Annotated |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
data = { |
||||
|
"plumbus": {"description": "Freshly pickled plumbus", "owner": "Morty"}, |
||||
|
"portal-gun": {"description": "Gun to create portals", "owner": "Rick"}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
class OwnerError(Exception): |
||||
|
pass |
||||
|
|
||||
|
|
||||
|
def get_username(): |
||||
|
try: |
||||
|
yield "Rick" |
||||
|
except OwnerError as e: |
||||
|
raise HTTPException(status_code=400, detail=f"Owner error: {e}") |
||||
|
|
||||
|
|
||||
|
@app.get("/items/{item_id}") |
||||
|
def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): |
||||
|
if item_id not in data: |
||||
|
raise HTTPException(status_code=404, detail="Item not found") |
||||
|
item = data[item_id] |
||||
|
if item["owner"] != username: |
||||
|
raise OwnerError(username) |
||||
|
return item |
@ -0,0 +1,32 @@ |
|||||
|
from typing import Annotated |
||||
|
|
||||
|
from fastapi import Depends, FastAPI, HTTPException |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
data = { |
||||
|
"plumbus": {"description": "Freshly pickled plumbus", "owner": "Morty"}, |
||||
|
"portal-gun": {"description": "Gun to create portals", "owner": "Rick"}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
class OwnerError(Exception): |
||||
|
pass |
||||
|
|
||||
|
|
||||
|
def get_username(): |
||||
|
try: |
||||
|
yield "Rick" |
||||
|
except OwnerError as e: |
||||
|
raise HTTPException(status_code=400, detail=f"Owner error: {e}") |
||||
|
|
||||
|
|
||||
|
@app.get("/items/{item_id}") |
||||
|
def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): |
||||
|
if item_id not in data: |
||||
|
raise HTTPException(status_code=404, detail="Item not found") |
||||
|
item = data[item_id] |
||||
|
if item["owner"] != username: |
||||
|
raise OwnerError(username) |
||||
|
return item |
@ -0,0 +1,39 @@ |
|||||
|
import importlib |
||||
|
|
||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from ...utils import needs_py39 |
||||
|
|
||||
|
|
||||
|
@pytest.fixture( |
||||
|
name="client", |
||||
|
params=[ |
||||
|
"tutorial008b", |
||||
|
"tutorial008b_an", |
||||
|
pytest.param("tutorial008b_an_py39", marks=needs_py39), |
||||
|
], |
||||
|
) |
||||
|
def get_client(request: pytest.FixtureRequest): |
||||
|
mod = importlib.import_module(f"docs_src.dependencies.{request.param}") |
||||
|
|
||||
|
client = TestClient(mod.app) |
||||
|
return client |
||||
|
|
||||
|
|
||||
|
def test_get_no_item(client: TestClient): |
||||
|
response = client.get("/items/foo") |
||||
|
assert response.status_code == 404, response.text |
||||
|
assert response.json() == {"detail": "Item not found"} |
||||
|
|
||||
|
|
||||
|
def test_owner_error(client: TestClient): |
||||
|
response = client.get("/items/plumbus") |
||||
|
assert response.status_code == 400, response.text |
||||
|
assert response.json() == {"detail": "Owner error: Rick"} |
||||
|
|
||||
|
|
||||
|
def test_get_item(client: TestClient): |
||||
|
response = client.get("/items/portal-gun") |
||||
|
assert response.status_code == 200, response.text |
||||
|
assert response.json() == {"description": "Gun to create portals", "owner": "Rick"} |
Loading…
Reference in new issue