diff --git a/docs/tutorial/src/tutorial05-04.py b/docs/tutorial/src/tutorial05-04.py deleted file mode 100644 index 54191b66a..000000000 --- a/docs/tutorial/src/tutorial05-04.py +++ /dev/null @@ -1,10 +0,0 @@ -from fastapi import FastAPI -from fastapi import FastAPI - -app = FastAPI() - -@app.get("/items/{item_id}") -async def read_item(item_id: str, q: str = None): - if q: - return {"item_id": item_id, "q": q} - return {"item_id": item_id} diff --git a/docs/tutorial/src/tutorial05-01.py b/docs/tutorial/src/tutorial05.py similarity index 100% rename from docs/tutorial/src/tutorial05-01.py rename to docs/tutorial/src/tutorial05.py diff --git a/docs/tutorial/src/tutorial06.py b/docs/tutorial/src/tutorial06.py index 30f0cd6d5..54191b66a 100644 --- a/docs/tutorial/src/tutorial06.py +++ b/docs/tutorial/src/tutorial06.py @@ -1,12 +1,10 @@ from fastapi import FastAPI +from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") -async def read_item(item_id: str, q: str = None, short: bool = False): - item = {"item_id": item_id} +async def read_item(item_id: str, q: str = None): if q: - item.update({"q": q}) - if not short: - item.update({"description": "This is an amazing item that has a long description"}) - return item + return {"item_id": item_id, "q": q} + return {"item_id": item_id} diff --git a/docs/tutorial/src/tutorial07.py b/docs/tutorial/src/tutorial07.py index d45ebba9f..30f0cd6d5 100644 --- a/docs/tutorial/src/tutorial07.py +++ b/docs/tutorial/src/tutorial07.py @@ -2,9 +2,9 @@ from fastapi import FastAPI app = FastAPI() -@app.get("/users/{user_id}/items/{item_id}") -async def read_user_item(user_id: int, item_id: str, q: str = None, short: bool = False): - item = {"item_id": item_id, "owner_id": user_id} +@app.get("/items/{item_id}") +async def read_item(item_id: str, q: str = None, short: bool = False): + item = {"item_id": item_id} if q: item.update({"q": q}) if not short: diff --git a/docs/tutorial/src/tutorial08.py b/docs/tutorial/src/tutorial08.py index c0b122793..d45ebba9f 100644 --- a/docs/tutorial/src/tutorial08.py +++ b/docs/tutorial/src/tutorial08.py @@ -3,8 +3,8 @@ from fastapi import FastAPI app = FastAPI() @app.get("/users/{user_id}/items/{item_id}") -async def read_user_item(user_id: int, item_id: str, needy: str, q: str = None, short: bool = False): - item = {"item_id": item_id, "owner_id": user_id, "needy": needy} +async def read_user_item(user_id: int, item_id: str, q: str = None, short: bool = False): + item = {"item_id": item_id, "owner_id": user_id} if q: item.update({"q": q}) if not short: diff --git a/docs/tutorial/src/tutorial09.py b/docs/tutorial/src/tutorial09.py index 78b813f52..c0b122793 100644 --- a/docs/tutorial/src/tutorial09.py +++ b/docs/tutorial/src/tutorial09.py @@ -1,15 +1,12 @@ from fastapi import FastAPI -from pydantic import BaseModel - -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - app = FastAPI() -@app.post("/items/") -async def create_item(item: Item): +@app.get("/users/{user_id}/items/{item_id}") +async def read_user_item(user_id: int, item_id: str, needy: str, q: str = None, short: bool = False): + item = {"item_id": item_id, "owner_id": user_id, "needy": needy} + if q: + item.update({"q": q}) + if not short: + item.update({"description": "This is an amazing item that has a long description"}) return item diff --git a/docs/tutorial/src/tutorial10.py b/docs/tutorial/src/tutorial10.py index 925f11062..78b813f52 100644 --- a/docs/tutorial/src/tutorial10.py +++ b/docs/tutorial/src/tutorial10.py @@ -12,8 +12,4 @@ app = FastAPI() @app.post("/items/") async def create_item(item: Item): - item_dict = item.dict() - if item.tax: - price_with_tax = item.price + item.tax - item_dict.update({"price_with_tax": price_with_tax}) - return item_dict + return item diff --git a/docs/tutorial/src/tutorial11.py b/docs/tutorial/src/tutorial11.py index 34e9849a1..925f11062 100644 --- a/docs/tutorial/src/tutorial11.py +++ b/docs/tutorial/src/tutorial11.py @@ -10,6 +10,10 @@ class Item(BaseModel): app = FastAPI() -@app.put("/items/{item_id}") -async def create_item(item_id: int, item: Item): - return {"item_id": item_id, **item.dict()} +@app.post("/items/") +async def create_item(item: Item): + item_dict = item.dict() + if item.tax: + price_with_tax = item.price + item.tax + item_dict.update({"price_with_tax": price_with_tax}) + return item_dict diff --git a/docs/tutorial/src/tutorial12.py b/docs/tutorial/src/tutorial12.py index be6239d8f..34e9849a1 100644 --- a/docs/tutorial/src/tutorial12.py +++ b/docs/tutorial/src/tutorial12.py @@ -11,8 +11,5 @@ class Item(BaseModel): app = FastAPI() @app.put("/items/{item_id}") -async def create_item(item_id: int, item: Item, q: str = None): - result = {"item_id": item_id, **item.dict()} - if q: - result.update({"q": q}) - return result +async def create_item(item_id: int, item: Item): + return {"item_id": item_id, **item.dict()} diff --git a/docs/tutorial/src/tutorial13.py b/docs/tutorial/src/tutorial13.py index 183180cd7..be6239d8f 100644 --- a/docs/tutorial/src/tutorial13.py +++ b/docs/tutorial/src/tutorial13.py @@ -1,11 +1,18 @@ -from fastapi import FastAPI, Query +from fastapi import FastAPI +from pydantic import BaseModel + +class Item(BaseModel): + name: str + description: str = None + price: float + tax: float = None -app = FastAPI() +app = FastAPI() -@app.get("/items/") -async def read_items(q: str = Query(None, max_length=50)): - results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} +@app.put("/items/{item_id}") +async def create_item(item_id: int, item: Item, q: str = None): + result = {"item_id": item_id, **item.dict()} if q: - results.update({"q": q}) - return results + result.update({"q": q}) + return result diff --git a/docs/tutorial/src/tutorial14.py b/docs/tutorial/src/tutorial14.py index 311ece816..183180cd7 100644 --- a/docs/tutorial/src/tutorial14.py +++ b/docs/tutorial/src/tutorial14.py @@ -4,7 +4,7 @@ app = FastAPI() @app.get("/items/") -async def read_items(q: str = Query(None, min_length=3, max_length=50)): +async def read_items(q: str = Query(None, max_length=50)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) diff --git a/docs/tutorial/src/tutorial15.py b/docs/tutorial/src/tutorial15.py index bdaf5c1a4..311ece816 100644 --- a/docs/tutorial/src/tutorial15.py +++ b/docs/tutorial/src/tutorial15.py @@ -4,7 +4,7 @@ app = FastAPI() @app.get("/items/") -async def read_items(q: str = Query(None, min_length=3, max_length=50, regex="^fixedquery$")): +async def read_items(q: str = Query(None, min_length=3, max_length=50)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) diff --git a/docs/tutorial/src/tutorial16.py b/docs/tutorial/src/tutorial16.py index 116829bef..bdaf5c1a4 100644 --- a/docs/tutorial/src/tutorial16.py +++ b/docs/tutorial/src/tutorial16.py @@ -4,11 +4,7 @@ app = FastAPI() @app.get("/items/") -async def read_items( - q: str = Query( - None, title="Query string", min_length=3, max_length=50, regex="^fixedquery$" - ) -): +async def read_items(q: str = Query(None, min_length=3, max_length=50, regex="^fixedquery$")): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) diff --git a/docs/tutorial/src/tutorial17.py b/docs/tutorial/src/tutorial17.py index 67d5fd6a5..116829bef 100644 --- a/docs/tutorial/src/tutorial17.py +++ b/docs/tutorial/src/tutorial17.py @@ -6,12 +6,7 @@ app = FastAPI() @app.get("/items/") async def read_items( q: str = Query( - None, - title="Query string", - description="Query string for the items to search in the database that have a good match", - min_length=3, - max_length=50, - regex="^fixedquery$", + None, title="Query string", min_length=3, max_length=50, regex="^fixedquery$" ) ): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} diff --git a/docs/tutorial/src/tutorial18-01.py b/docs/tutorial/src/tutorial18-01.py deleted file mode 100644 index a4e3b816c..000000000 --- a/docs/tutorial/src/tutorial18-01.py +++ /dev/null @@ -1,21 +0,0 @@ -from fastapi import FastAPI, Query - -app = FastAPI() - - -@app.get("/items/") -async def read_items( - q: str = Query( - "fixedquery", - alias="item-query", - title="Query string", - description="Query string for the items to search in the database that have a good match", - min_length=3, - max_length=50, - regex="^fixedquery$", - ) -): - results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} - if q: - results.update({"q": q}) - return results diff --git a/docs/tutorial/src/tutorial18-03.py b/docs/tutorial/src/tutorial18-03.py deleted file mode 100644 index b9bffcaff..000000000 --- a/docs/tutorial/src/tutorial18-03.py +++ /dev/null @@ -1,21 +0,0 @@ -from fastapi import FastAPI, Query - -app = FastAPI() - - -@app.get("/items/") -async def read_items( - q: str = Query( - ..., - alias="item-query", - title="Query string", - description="Query string for the items to search in the database that have a good match", - min_length=3, - max_length=50, - regex="^fixedquery$", - ) -): - results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} - if q: - results.update({"q": q}) - return results diff --git a/docs/tutorial/src/tutorial18-02.py b/docs/tutorial/src/tutorial18.py similarity index 94% rename from docs/tutorial/src/tutorial18-02.py rename to docs/tutorial/src/tutorial18.py index f261c9554..67d5fd6a5 100644 --- a/docs/tutorial/src/tutorial18-02.py +++ b/docs/tutorial/src/tutorial18.py @@ -7,7 +7,6 @@ app = FastAPI() async def read_items( q: str = Query( None, - alias="item-query", title="Query string", description="Query string for the items to search in the database that have a good match", min_length=3, diff --git a/docs/tutorial/src/tutorial19.py b/docs/tutorial/src/tutorial19.py index 051656c76..a4e3b816c 100644 --- a/docs/tutorial/src/tutorial19.py +++ b/docs/tutorial/src/tutorial19.py @@ -6,14 +6,13 @@ app = FastAPI() @app.get("/items/") async def read_items( q: str = Query( - None, + "fixedquery", alias="item-query", title="Query string", description="Query string for the items to search in the database that have a good match", min_length=3, max_length=50, regex="^fixedquery$", - deprecated=True, ) ): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} diff --git a/docs/tutorial/src/tutorial20.py b/docs/tutorial/src/tutorial20.py index 0600c7f83..f261c9554 100644 --- a/docs/tutorial/src/tutorial20.py +++ b/docs/tutorial/src/tutorial20.py @@ -1,14 +1,21 @@ -from fastapi import FastAPI, Query, Path +from fastapi import FastAPI, Query app = FastAPI() -@app.get("/items/{item_id}") +@app.get("/items/") async def read_items( - item_id: int = Path(..., title="The ID of the item to get"), - q: str = Query(None, alias="item-query"), + q: str = Query( + None, + alias="item-query", + title="Query string", + description="Query string for the items to search in the database that have a good match", + min_length=3, + max_length=50, + regex="^fixedquery$", + ) ): - results = {"item_id": item_id} + results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results diff --git a/docs/tutorial/src/tutorial21.py b/docs/tutorial/src/tutorial21.py index c14950716..b9bffcaff 100644 --- a/docs/tutorial/src/tutorial21.py +++ b/docs/tutorial/src/tutorial21.py @@ -1,14 +1,21 @@ -from fastapi import FastAPI, Query, Path +from fastapi import FastAPI, Query app = FastAPI() -@app.get("/items/{item_id}") +@app.get("/items/") async def read_items( - q: str, - item_id: int = Path(..., title="The ID of the item to get"), + q: str = Query( + ..., + alias="item-query", + title="Query string", + description="Query string for the items to search in the database that have a good match", + min_length=3, + max_length=50, + regex="^fixedquery$", + ) ): - results = {"item_id": item_id} + results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results diff --git a/docs/tutorial/src/tutorial22.py b/docs/tutorial/src/tutorial22.py index 4ac37c409..051656c76 100644 --- a/docs/tutorial/src/tutorial22.py +++ b/docs/tutorial/src/tutorial22.py @@ -1,15 +1,22 @@ -from fastapi import FastAPI, Query, Path +from fastapi import FastAPI, Query app = FastAPI() -@app.get("/items/{item_id}") +@app.get("/items/") async def read_items( - *, - item_id: int = Path(..., title="The ID of the item to get"), - q: str, + q: str = Query( + None, + alias="item-query", + title="Query string", + description="Query string for the items to search in the database that have a good match", + min_length=3, + max_length=50, + regex="^fixedquery$", + deprecated=True, + ) ): - results = {"item_id": item_id} + results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results diff --git a/docs/tutorial/src/tutorial23.py b/docs/tutorial/src/tutorial23.py index 97611c273..0600c7f83 100644 --- a/docs/tutorial/src/tutorial23.py +++ b/docs/tutorial/src/tutorial23.py @@ -5,9 +5,8 @@ app = FastAPI() @app.get("/items/{item_id}") async def read_items( - *, - item_id: int = Path(..., title="The ID of the item to get", ge=1), - q: str, + item_id: int = Path(..., title="The ID of the item to get"), + q: str = Query(None, alias="item-query"), ): results = {"item_id": item_id} if q: diff --git a/docs/tutorial/src/tutorial24.py b/docs/tutorial/src/tutorial24.py index 4dabe3537..c14950716 100644 --- a/docs/tutorial/src/tutorial24.py +++ b/docs/tutorial/src/tutorial24.py @@ -5,9 +5,8 @@ app = FastAPI() @app.get("/items/{item_id}") async def read_items( - *, - item_id: int = Path(..., title="The ID of the item to get", gt=0, le=1000), q: str, + item_id: int = Path(..., title="The ID of the item to get"), ): results = {"item_id": item_id} if q: diff --git a/docs/tutorial/src/tutorial25.py b/docs/tutorial/src/tutorial25.py index 72b2bba1f..4ac37c409 100644 --- a/docs/tutorial/src/tutorial25.py +++ b/docs/tutorial/src/tutorial25.py @@ -6,9 +6,8 @@ app = FastAPI() @app.get("/items/{item_id}") async def read_items( *, - item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000), + item_id: int = Path(..., title="The ID of the item to get"), q: str, - size: float = Query(..., gt=0, lt=10.5) ): results = {"item_id": item_id} if q: diff --git a/docs/tutorial/src/tutorial26.py b/docs/tutorial/src/tutorial26.py index 00836004e..97611c273 100644 --- a/docs/tutorial/src/tutorial26.py +++ b/docs/tutorial/src/tutorial26.py @@ -1,26 +1,15 @@ from fastapi import FastAPI, Query, Path -from pydantic import BaseModel app = FastAPI() -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - - -@app.put("/items/{item_id}") -async def update_item( +@app.get("/items/{item_id}") +async def read_items( *, - item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000), + item_id: int = Path(..., title="The ID of the item to get", ge=1), q: str, - item: Item = None, ): results = {"item_id": item_id} if q: results.update({"q": q}) - if item: - results.update({"item": item}) return results diff --git a/docs/tutorial/src/tutorial27.py b/docs/tutorial/src/tutorial27.py index 31dbb82c9..4dabe3537 100644 --- a/docs/tutorial/src/tutorial27.py +++ b/docs/tutorial/src/tutorial27.py @@ -1,27 +1,15 @@ from fastapi import FastAPI, Query, Path -from pydantic import BaseModel app = FastAPI() -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - - -class User(BaseModel): - username: str - full_name: str = None - - -@app.put("/items/{item_id}") -async def update_item( +@app.get("/items/{item_id}") +async def read_items( *, - item_id: int, - item: Item, - user: User, + item_id: int = Path(..., title="The ID of the item to get", gt=0, le=1000), + q: str, ): - results = {"item_id": item_id, "item": item, "user": user} + results = {"item_id": item_id} + if q: + results.update({"q": q}) return results diff --git a/docs/tutorial/src/tutorial28.py b/docs/tutorial/src/tutorial28.py index a1302229b..72b2bba1f 100644 --- a/docs/tutorial/src/tutorial28.py +++ b/docs/tutorial/src/tutorial28.py @@ -1,28 +1,16 @@ -from fastapi import FastAPI, Query, Path, Body -from pydantic import BaseModel +from fastapi import FastAPI, Query, Path app = FastAPI() -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - - -class User(BaseModel): - username: str - full_name: str = None - - -@app.put("/items/{item_id}") -async def update_item( +@app.get("/items/{item_id}") +async def read_items( *, - item_id: int, - item: Item, - user: User, - access_token: str = Body(...), + item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000), + q: str, + size: float = Query(..., gt=0, lt=10.5) ): - results = {"item_id": item_id, "item": item, "user": user, "access_token": access_token} + results = {"item_id": item_id} + if q: + results.update({"q": q}) return results diff --git a/docs/tutorial/src/tutorial29.py b/docs/tutorial/src/tutorial29.py index 584451ed4..00836004e 100644 --- a/docs/tutorial/src/tutorial29.py +++ b/docs/tutorial/src/tutorial29.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, Query, Path, Body +from fastapi import FastAPI, Query, Path from pydantic import BaseModel app = FastAPI() @@ -11,21 +11,16 @@ class Item(BaseModel): tax: float = None -class User(BaseModel): - username: str - full_name: str = None - - @app.put("/items/{item_id}") async def update_item( *, - item_id: int, - item: Item, - user: User, - access_token: str = Body(...), - q: str = None, + item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000), + q: str, + item: Item = None, ): - results = {"item_id": item_id, "item": item, "user": user, "access_token": access_token} + results = {"item_id": item_id} if q: results.update({"q": q}) + if item: + results.update({"item": item}) return results diff --git a/docs/tutorial/src/tutorial30.py b/docs/tutorial/src/tutorial30.py index 559356f99..31dbb82c9 100644 --- a/docs/tutorial/src/tutorial30.py +++ b/docs/tutorial/src/tutorial30.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, Query, Path, Body +from fastapi import FastAPI, Query, Path from pydantic import BaseModel app = FastAPI() @@ -11,11 +11,17 @@ class Item(BaseModel): tax: float = None +class User(BaseModel): + username: str + full_name: str = None + + @app.put("/items/{item_id}") async def update_item( *, item_id: int, - item: Item = Body(..., embed=True), + item: Item, + user: User, ): - results = {"item_id": item_id, "item": item} + results = {"item_id": item_id, "item": item, "user": user} return results diff --git a/docs/tutorial/src/tutorial31.py b/docs/tutorial/src/tutorial31.py index 89b97e8c8..a1302229b 100644 --- a/docs/tutorial/src/tutorial31.py +++ b/docs/tutorial/src/tutorial31.py @@ -1,21 +1,28 @@ from fastapi import FastAPI, Query, Path, Body -from pydantic import BaseModel, Schema +from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str - description: str = Schema(None, title="The description of the item", max_length=300) - price: float = Schema(..., gt=0, description="The price must be greater than zero") + description: str = None + price: float tax: float = None +class User(BaseModel): + username: str + full_name: str = None + + @app.put("/items/{item_id}") async def update_item( *, item_id: int, - item: Item = Body(..., embed=True), + item: Item, + user: User, + access_token: str = Body(...), ): - results = {"item_id": item_id, "item": item} + results = {"item_id": item_id, "item": item, "user": user, "access_token": access_token} return results diff --git a/docs/tutorial/src/tutorial32.py b/docs/tutorial/src/tutorial32.py index e6fa5843a..584451ed4 100644 --- a/docs/tutorial/src/tutorial32.py +++ b/docs/tutorial/src/tutorial32.py @@ -9,7 +9,11 @@ class Item(BaseModel): description: str = None price: float tax: float = None - tags: list = [] + + +class User(BaseModel): + username: str + full_name: str = None @app.put("/items/{item_id}") @@ -17,6 +21,11 @@ async def update_item( *, item_id: int, item: Item, + user: User, + access_token: str = Body(...), + q: str = None, ): - results = {"item_id": item_id, "item": item} + results = {"item_id": item_id, "item": item, "user": user, "access_token": access_token} + if q: + results.update({"q": q}) return results diff --git a/docs/tutorial/src/tutorial33.py b/docs/tutorial/src/tutorial33.py index 652ec7c57..559356f99 100644 --- a/docs/tutorial/src/tutorial33.py +++ b/docs/tutorial/src/tutorial33.py @@ -1,6 +1,5 @@ -from fastapi import Body, FastAPI, Path, Query +from fastapi import FastAPI, Query, Path, Body from pydantic import BaseModel -from typing import List app = FastAPI() @@ -10,14 +9,13 @@ class Item(BaseModel): description: str = None price: float tax: float = None - tags: List[str] = [] @app.put("/items/{item_id}") async def update_item( *, item_id: int, - item: Item, + item: Item = Body(..., embed=True), ): results = {"item_id": item_id, "item": item} return results diff --git a/docs/tutorial/src/tutorial34.py b/docs/tutorial/src/tutorial34.py index 444f73ac5..89b97e8c8 100644 --- a/docs/tutorial/src/tutorial34.py +++ b/docs/tutorial/src/tutorial34.py @@ -1,23 +1,21 @@ -from fastapi import Body, FastAPI, Path, Query -from pydantic import BaseModel -from typing import Set +from fastapi import FastAPI, Query, Path, Body +from pydantic import BaseModel, Schema app = FastAPI() class Item(BaseModel): name: str - description: str = None - price: float + description: str = Schema(None, title="The description of the item", max_length=300) + price: float = Schema(..., gt=0, description="The price must be greater than zero") tax: float = None - tags: Set[str] = [] @app.put("/items/{item_id}") async def update_item( *, item_id: int, - item: Item, + item: Item = Body(..., embed=True), ): results = {"item_id": item_id, "item": item} return results diff --git a/docs/tutorial/src/tutorial35.py b/docs/tutorial/src/tutorial35.py index 6cfbeff39..e6fa5843a 100644 --- a/docs/tutorial/src/tutorial35.py +++ b/docs/tutorial/src/tutorial35.py @@ -1,22 +1,15 @@ -from fastapi import Body, FastAPI, Path, Query +from fastapi import FastAPI, Query, Path, Body from pydantic import BaseModel -from typing import Set app = FastAPI() -class Image(BaseModel): - url: str - name: str - - class Item(BaseModel): name: str description: str = None price: float tax: float = None - tags: Set[str] = [] - image: Image = None + tags: list = [] @app.put("/items/{item_id}") diff --git a/docs/tutorial/src/tutorial36.py b/docs/tutorial/src/tutorial36.py index d04e72d80..652ec7c57 100644 --- a/docs/tutorial/src/tutorial36.py +++ b/docs/tutorial/src/tutorial36.py @@ -1,23 +1,16 @@ from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel -from pydantic.types import UrlStr -from typing import Set +from typing import List app = FastAPI() -class Image(BaseModel): - url: UrlStr - name: str - - class Item(BaseModel): name: str description: str = None price: float tax: float = None - tags: Set[str] = [] - image: Image = None + tags: List[str] = [] @app.put("/items/{item_id}") diff --git a/docs/tutorial/src/tutorial37.py b/docs/tutorial/src/tutorial37.py index e5c25e4b6..444f73ac5 100644 --- a/docs/tutorial/src/tutorial37.py +++ b/docs/tutorial/src/tutorial37.py @@ -1,23 +1,16 @@ from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel -from pydantic.types import UrlStr -from typing import Set, List +from typing import Set app = FastAPI() -class Image(BaseModel): - url: UrlStr - name: str - - class Item(BaseModel): name: str description: str = None price: float tax: float = None tags: Set[str] = [] - image: List[Image] = None @app.put("/items/{item_id}") diff --git a/docs/tutorial/src/tutorial38.py b/docs/tutorial/src/tutorial38.py index 3dd1a0554..6cfbeff39 100644 --- a/docs/tutorial/src/tutorial38.py +++ b/docs/tutorial/src/tutorial38.py @@ -1,13 +1,12 @@ from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel -from pydantic.types import UrlStr -from typing import Set, List +from typing import Set app = FastAPI() class Image(BaseModel): - url: UrlStr + url: str name: str @@ -17,16 +16,14 @@ class Item(BaseModel): price: float tax: float = None tags: Set[str] = [] - image: List[Image] = None + image: Image = None -class Offer(BaseModel): - name: str - description: str = None - price: float - items: List[Item] - - -@app.post("/offers/") -async def create_offer(*, offer: Offer): - return offer +@app.put("/items/{item_id}") +async def update_item( + *, + item_id: int, + item: Item, +): + results = {"item_id": item_id, "item": item} + return results diff --git a/docs/tutorial/src/tutorial39.py b/docs/tutorial/src/tutorial39.py index b57a243d7..d04e72d80 100644 --- a/docs/tutorial/src/tutorial39.py +++ b/docs/tutorial/src/tutorial39.py @@ -1,7 +1,7 @@ from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel from pydantic.types import UrlStr -from typing import Set, List +from typing import Set app = FastAPI() @@ -11,6 +11,20 @@ class Image(BaseModel): name: str -@app.post("/images/multiple/") -async def create_multiple_images(*, images: List[Image]): - return images +class Item(BaseModel): + name: str + description: str = None + price: float + tax: float = None + tags: Set[str] = [] + image: Image = None + + +@app.put("/items/{item_id}") +async def update_item( + *, + item_id: int, + item: Item, +): + results = {"item_id": item_id, "item": item} + return results diff --git a/docs/tutorial/src/tutorial40.py b/docs/tutorial/src/tutorial40.py index f1cc8b2c6..e5c25e4b6 100644 --- a/docs/tutorial/src/tutorial40.py +++ b/docs/tutorial/src/tutorial40.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, Cookie +from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel from pydantic.types import UrlStr from typing import Set, List @@ -6,6 +6,25 @@ from typing import Set, List app = FastAPI() -@app.get("/items/") -async def read_items(*, ads_id: str = Cookie(None)): - return {"ads_id": ads_id} +class Image(BaseModel): + url: UrlStr + name: str + + +class Item(BaseModel): + name: str + description: str = None + price: float + tax: float = None + tags: Set[str] = [] + image: List[Image] = None + + +@app.put("/items/{item_id}") +async def update_item( + *, + item_id: int, + item: Item, +): + results = {"item_id": item_id, "item": item} + return results diff --git a/docs/tutorial/src/tutorial41.py b/docs/tutorial/src/tutorial41.py index 9dbb9912c..3dd1a0554 100644 --- a/docs/tutorial/src/tutorial41.py +++ b/docs/tutorial/src/tutorial41.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, Header +from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel from pydantic.types import UrlStr from typing import Set, List @@ -6,6 +6,27 @@ from typing import Set, List app = FastAPI() -@app.get("/items/") -async def read_items(*, accept_encoding: str = Header(None)): - return {"Accept-Encoding": accept_encoding} +class Image(BaseModel): + url: UrlStr + name: str + + +class Item(BaseModel): + name: str + description: str = None + price: float + tax: float = None + tags: Set[str] = [] + image: List[Image] = None + + +class Offer(BaseModel): + name: str + description: str = None + price: float + items: List[Item] + + +@app.post("/offers/") +async def create_offer(*, offer: Offer): + return offer diff --git a/docs/tutorial/src/tutorial42.py b/docs/tutorial/src/tutorial42.py index 070c7fe51..b57a243d7 100644 --- a/docs/tutorial/src/tutorial42.py +++ b/docs/tutorial/src/tutorial42.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, Header +from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel from pydantic.types import UrlStr from typing import Set, List @@ -6,6 +6,11 @@ from typing import Set, List app = FastAPI() -@app.get("/items/") -async def read_items(*, strange_header: str = Header(None, convert_underscores=False)): - return {"strange_header": strange_header} +class Image(BaseModel): + url: UrlStr + name: str + + +@app.post("/images/multiple/") +async def create_multiple_images(*, images: List[Image]): + return images diff --git a/docs/tutorial/src/tutorial43.py b/docs/tutorial/src/tutorial43.py index e89278270..f1cc8b2c6 100644 --- a/docs/tutorial/src/tutorial43.py +++ b/docs/tutorial/src/tutorial43.py @@ -1,4 +1,4 @@ -from fastapi import Body, FastAPI, Path, Query +from fastapi import FastAPI, Cookie from pydantic import BaseModel from pydantic.types import UrlStr from typing import Set, List @@ -6,14 +6,6 @@ from typing import Set, List app = FastAPI() -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - tags: Set[str] = [] - - -@app.post("/items/", response_model=Item) -async def create_item(*, item: Item): - return item +@app.get("/items/") +async def read_items(*, ads_id: str = Cookie(None)): + return {"ads_id": ads_id} diff --git a/docs/tutorial/src/tutorial44.py b/docs/tutorial/src/tutorial44.py index 5ec7fa4aa..9dbb9912c 100644 --- a/docs/tutorial/src/tutorial44.py +++ b/docs/tutorial/src/tutorial44.py @@ -1,20 +1,11 @@ -from fastapi import Body, FastAPI, Path, Query +from fastapi import FastAPI, Header from pydantic import BaseModel -from pydantic.types import EmailStr +from pydantic.types import UrlStr from typing import Set, List app = FastAPI() -class UserIn(BaseModel): - username: str - password: str - email: EmailStr - full_name: str = None - - - -# Don't do this in production! -@app.post("/user/", response_model=UserIn) -async def create_user(*, user: UserIn): - return user +@app.get("/items/") +async def read_items(*, accept_encoding: str = Header(None)): + return {"Accept-Encoding": accept_encoding} diff --git a/docs/tutorial/src/tutorial45.py b/docs/tutorial/src/tutorial45.py index d9cdd86d5..070c7fe51 100644 --- a/docs/tutorial/src/tutorial45.py +++ b/docs/tutorial/src/tutorial45.py @@ -1,24 +1,11 @@ -from fastapi import Body, FastAPI, Path, Query +from fastapi import FastAPI, Header from pydantic import BaseModel -from pydantic.types import EmailStr +from pydantic.types import UrlStr from typing import Set, List app = FastAPI() -class UserIn(BaseModel): - username: str - password: str - email: EmailStr - full_name: str = None - - -class UserOut(BaseModel): - username: str - email: EmailStr - full_name: str = None - - -@app.post("/user/", response_model=UserOut) -async def create_user(*, user: UserIn): - return user +@app.get("/items/") +async def read_items(*, strange_header: str = Header(None, convert_underscores=False)): + return {"strange_header": strange_header} diff --git a/docs/tutorial/src/tutorial46.py b/docs/tutorial/src/tutorial46.py index c2811eba0..e89278270 100644 --- a/docs/tutorial/src/tutorial46.py +++ b/docs/tutorial/src/tutorial46.py @@ -1,43 +1,19 @@ from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel -from pydantic.types import EmailStr +from pydantic.types import UrlStr from typing import Set, List -from uuid import UUID, uuid4 app = FastAPI() -class UserIn(BaseModel): - username: str - password: str - email: EmailStr - full_name: str = None +class Item(BaseModel): + name: str + description: str = None + price: float + tax: float = None + tags: Set[str] = [] -class UserOut(BaseModel): - username: str - email: EmailStr - full_name: str = None - - -class UserInDB(BaseModel): - username: str - hashed_password: str - email: EmailStr - full_name: str = None - - -def fake_password_hasher(raw_password: str): - return "supersecret" + raw_password - - -def fake_save_user(user_in: UserIn): - hashed_password = fake_password_hasher(user_in.password) - user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password) - print("User saved! ..not really") - return user_in_db - -@app.post("/user/", response_model=UserOut) -async def create_user(*, user_in: UserIn): - user_saved = fake_save_user(user_in) - return user_saved +@app.post("/items/", response_model=Item) +async def create_item(*, item: Item): + return item diff --git a/docs/tutorial/src/tutorial47-01.py b/docs/tutorial/src/tutorial47-01.py deleted file mode 100644 index b61e62b83..000000000 --- a/docs/tutorial/src/tutorial47-01.py +++ /dev/null @@ -1,37 +0,0 @@ -from fastapi import Body, FastAPI, Path, Query -from pydantic import BaseModel -from pydantic.types import EmailStr -from typing import Set, List - -app = FastAPI() - -class UserBase(BaseModel): - username: str - email: EmailStr - full_name: str = None - -class UserIn(UserBase): - password: str - - -class UserOut(UserBase): - pass - -class UserInDB(UserBase): - hashed_password: str - - -def fake_password_hasher(raw_password: str): - return "supersecret" + raw_password - - -def fake_save_user(user_in: UserIn): - hashed_password = fake_password_hasher(user_in.password) - user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password) - print("User saved! ..not really") - return user_in_db - -@app.post("/user/", response_model=UserOut) -async def create_user(*, user_in: UserIn): - user_saved = fake_save_user(user_in) - return user_saved diff --git a/docs/tutorial/src/tutorial47-02.py b/docs/tutorial/src/tutorial47-02.py deleted file mode 100644 index 52a93d7ff..000000000 --- a/docs/tutorial/src/tutorial47-02.py +++ /dev/null @@ -1,11 +0,0 @@ -from fastapi import Body, FastAPI, Path, Query, Form -from pydantic import BaseModel -from pydantic.types import EmailStr -from typing import Set, List - -app = FastAPI() - - -@app.post("/login/") -async def login(*, username: str = Form(...), password: str = Form(...)): - return {"username": username} diff --git a/docs/tutorial/src/tutorial47-03.py b/docs/tutorial/src/tutorial47-03.py deleted file mode 100644 index 3a517f7a1..000000000 --- a/docs/tutorial/src/tutorial47-03.py +++ /dev/null @@ -1,11 +0,0 @@ -from fastapi import Body, FastAPI, Path, Query, File -from pydantic import BaseModel -from pydantic.types import EmailStr -from typing import Set, List - -app = FastAPI() - - -@app.post("/files/") -async def create_file(*, file: bytes = File(...)): - return {"file_size": len(file)} diff --git a/docs/tutorial/src/tutorial47-04.py b/docs/tutorial/src/tutorial47-04.py deleted file mode 100644 index 19382b815..000000000 --- a/docs/tutorial/src/tutorial47-04.py +++ /dev/null @@ -1,11 +0,0 @@ -from fastapi import Body, FastAPI, Path, Query, File, Form -from pydantic import BaseModel -from pydantic.types import EmailStr -from typing import Set, List - -app = FastAPI() - - -@app.post("/files/") -async def create_file(*, file: bytes = File(...), token: str = Form(...)): - return {"file_size": len(file), "token": token} diff --git a/docs/tutorial/src/tutorial47.py b/docs/tutorial/src/tutorial47.py new file mode 100644 index 000000000..5ec7fa4aa --- /dev/null +++ b/docs/tutorial/src/tutorial47.py @@ -0,0 +1,20 @@ +from fastapi import Body, FastAPI, Path, Query +from pydantic import BaseModel +from pydantic.types import EmailStr +from typing import Set, List + +app = FastAPI() + + +class UserIn(BaseModel): + username: str + password: str + email: EmailStr + full_name: str = None + + + +# Don't do this in production! +@app.post("/user/", response_model=UserIn) +async def create_user(*, user: UserIn): + return user diff --git a/docs/tutorial/src/tutorial48.py b/docs/tutorial/src/tutorial48.py index fb32535c2..d9cdd86d5 100644 --- a/docs/tutorial/src/tutorial48.py +++ b/docs/tutorial/src/tutorial48.py @@ -1,20 +1,24 @@ from fastapi import Body, FastAPI, Path, Query -from starlette.status import HTTP_201_CREATED from pydantic import BaseModel -from pydantic.types import UrlStr +from pydantic.types import EmailStr from typing import Set, List app = FastAPI() -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - tags: Set[str] = [] +class UserIn(BaseModel): + username: str + password: str + email: EmailStr + full_name: str = None -@app.post("/items/", response_model=Item, status_code=HTTP_201_CREATED) -async def create_item(*, item: Item): - return item +class UserOut(BaseModel): + username: str + email: EmailStr + full_name: str = None + + +@app.post("/user/", response_model=UserOut) +async def create_user(*, user: UserIn): + return user diff --git a/docs/tutorial/src/tutorial49.py b/docs/tutorial/src/tutorial49.py index bd0bef7f9..c2811eba0 100644 --- a/docs/tutorial/src/tutorial49.py +++ b/docs/tutorial/src/tutorial49.py @@ -1,20 +1,43 @@ from fastapi import Body, FastAPI, Path, Query -from starlette.status import HTTP_201_CREATED from pydantic import BaseModel -from pydantic.types import UrlStr +from pydantic.types import EmailStr from typing import Set, List +from uuid import UUID, uuid4 app = FastAPI() -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - tags: Set[str] = [] +class UserIn(BaseModel): + username: str + password: str + email: EmailStr + full_name: str = None -@app.post("/items/", response_model=Item, tags=["items"]) -async def create_item(*, item: Item): - return item +class UserOut(BaseModel): + username: str + email: EmailStr + full_name: str = None + + +class UserInDB(BaseModel): + username: str + hashed_password: str + email: EmailStr + full_name: str = None + + +def fake_password_hasher(raw_password: str): + return "supersecret" + raw_password + + +def fake_save_user(user_in: UserIn): + hashed_password = fake_password_hasher(user_in.password) + user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password) + print("User saved! ..not really") + return user_in_db + +@app.post("/user/", response_model=UserOut) +async def create_user(*, user_in: UserIn): + user_saved = fake_save_user(user_in) + return user_saved diff --git a/docs/tutorial/src/tutorial50.py b/docs/tutorial/src/tutorial50.py index 8a163389d..b61e62b83 100644 --- a/docs/tutorial/src/tutorial50.py +++ b/docs/tutorial/src/tutorial50.py @@ -1,25 +1,37 @@ from fastapi import Body, FastAPI, Path, Query -from starlette.status import HTTP_201_CREATED from pydantic import BaseModel -from pydantic.types import UrlStr +from pydantic.types import EmailStr from typing import Set, List app = FastAPI() +class UserBase(BaseModel): + username: str + email: EmailStr + full_name: str = None -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - tags: Set[str] = [] +class UserIn(UserBase): + password: str -@app.post( - "/items/", - response_model=Item, - summary="Create an item", - description="Create an item with all the information, name, description, price, tax and a set of unique tags", -) -async def create_item(*, item: Item): - return item +class UserOut(UserBase): + pass + +class UserInDB(UserBase): + hashed_password: str + + +def fake_password_hasher(raw_password: str): + return "supersecret" + raw_password + + +def fake_save_user(user_in: UserIn): + hashed_password = fake_password_hasher(user_in.password) + user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password) + print("User saved! ..not really") + return user_in_db + +@app.post("/user/", response_model=UserOut) +async def create_user(*, user_in: UserIn): + user_saved = fake_save_user(user_in) + return user_saved diff --git a/docs/tutorial/src/tutorial51.py b/docs/tutorial/src/tutorial51.py index 479ac7d58..52a93d7ff 100644 --- a/docs/tutorial/src/tutorial51.py +++ b/docs/tutorial/src/tutorial51.py @@ -1,29 +1,11 @@ -from fastapi import Body, FastAPI, Path, Query -from starlette.status import HTTP_201_CREATED +from fastapi import Body, FastAPI, Path, Query, Form from pydantic import BaseModel -from pydantic.types import UrlStr +from pydantic.types import EmailStr from typing import Set, List app = FastAPI() -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - tags: Set[str] = [] - - -@app.post("/items/", response_model=Item, summary="Create an item") -async def create_item(*, item: Item): - """ - Create an item with all the information: - - * name: each item must have a name - * description: a long description - * price: required - * tax: if the item doesn't have tax, you can omit this - * tags: a set of unique tag strings for this item - """ - return item +@app.post("/login/") +async def login(*, username: str = Form(...), password: str = Form(...)): + return {"username": username} diff --git a/docs/tutorial/src/tutorial52.py b/docs/tutorial/src/tutorial52.py index bec8a6ad2..3a517f7a1 100644 --- a/docs/tutorial/src/tutorial52.py +++ b/docs/tutorial/src/tutorial52.py @@ -1,29 +1,11 @@ -from fastapi import Body, FastAPI, Path, Query -from starlette.status import HTTP_201_CREATED +from fastapi import Body, FastAPI, Path, Query, File from pydantic import BaseModel -from pydantic.types import UrlStr +from pydantic.types import EmailStr from typing import Set, List app = FastAPI() -class Item(BaseModel): - name: str - description: str = None - price: float - tax: float = None - tags: Set[str] = [] - - -@app.post("/items/", response_model=Item, summary="Create an item", response_description="The created item") -async def create_item(*, item: Item): - """ - Create an item with all the information: - - * name: each item must have a name - * description: a long description - * price: required - * tax: if the item doesn't have tax, you can omit this - * tags: a set of unique tag strings for this item - """ - return item +@app.post("/files/") +async def create_file(*, file: bytes = File(...)): + return {"file_size": len(file)} diff --git a/docs/tutorial/src/tutorial53.py b/docs/tutorial/src/tutorial53.py index 1a77be023..19382b815 100644 --- a/docs/tutorial/src/tutorial53.py +++ b/docs/tutorial/src/tutorial53.py @@ -1,13 +1,11 @@ -from typing import List, Set - -from fastapi import Body, FastAPI, Path, Query +from fastapi import Body, FastAPI, Path, Query, File, Form from pydantic import BaseModel -from pydantic.types import UrlStr -from starlette.status import HTTP_201_CREATED +from pydantic.types import EmailStr +from typing import Set, List app = FastAPI() -@app.get("/items/", deprecated=True) -async def read_items(): - return [{"item_id": "Foo"}] +@app.post("/files/") +async def create_file(*, file: bytes = File(...), token: str = Form(...)): + return {"file_size": len(file), "token": token} diff --git a/docs/tutorial/src/tutorial54.py b/docs/tutorial/src/tutorial54.py index 31793c985..fb32535c2 100644 --- a/docs/tutorial/src/tutorial54.py +++ b/docs/tutorial/src/tutorial54.py @@ -1,13 +1,20 @@ -from typing import List, Set - from fastapi import Body, FastAPI, Path, Query +from starlette.status import HTTP_201_CREATED from pydantic import BaseModel from pydantic.types import UrlStr -from starlette.status import HTTP_201_CREATED +from typing import Set, List app = FastAPI() -@app.get("/items/", operation_id="some_specific_id_you_define") -async def read_items(): - return [{"item_id": "Foo"}] +class Item(BaseModel): + name: str + description: str = None + price: float + tax: float = None + tags: Set[str] = [] + + +@app.post("/items/", response_model=Item, status_code=HTTP_201_CREATED) +async def create_item(*, item: Item): + return item diff --git a/docs/tutorial/src/tutorial55.py b/docs/tutorial/src/tutorial55.py index 34f9e8563..bd0bef7f9 100644 --- a/docs/tutorial/src/tutorial55.py +++ b/docs/tutorial/src/tutorial55.py @@ -1,13 +1,20 @@ -from typing import List, Set - from fastapi import Body, FastAPI, Path, Query +from starlette.status import HTTP_201_CREATED from pydantic import BaseModel from pydantic.types import UrlStr -from starlette.status import HTTP_201_CREATED +from typing import Set, List app = FastAPI() -@app.get("/items/", include_in_schema=False) -async def read_items(): - return [{"item_id": "Foo"}] +class Item(BaseModel): + name: str + description: str = None + price: float + tax: float = None + tags: Set[str] = [] + + +@app.post("/items/", response_model=Item, tags=["items"]) +async def create_item(*, item: Item): + return item diff --git a/docs/tutorial/src/tutorial56.py b/docs/tutorial/src/tutorial56.py index dec2f53e2..8a163389d 100644 --- a/docs/tutorial/src/tutorial56.py +++ b/docs/tutorial/src/tutorial56.py @@ -1,14 +1,25 @@ -from typing import List, Set - from fastapi import Body, FastAPI, Path, Query +from starlette.status import HTTP_201_CREATED from pydantic import BaseModel from pydantic.types import UrlStr -from starlette.status import HTTP_201_CREATED -from starlette.responses import UJSONResponse +from typing import Set, List app = FastAPI() -@app.get("/items/", content_type=UJSONResponse) -async def read_items(): - return [{"item_id": "Foo"}] +class Item(BaseModel): + name: str + description: str = None + price: float + tax: float = None + tags: Set[str] = [] + + +@app.post( + "/items/", + response_model=Item, + summary="Create an item", + description="Create an item with all the information, name, description, price, tax and a set of unique tags", +) +async def create_item(*, item: Item): + return item diff --git a/docs/tutorial/src/tutorial57.py b/docs/tutorial/src/tutorial57.py index f2300f8d4..479ac7d58 100644 --- a/docs/tutorial/src/tutorial57.py +++ b/docs/tutorial/src/tutorial57.py @@ -1,23 +1,29 @@ -from typing import List, Set - from fastapi import Body, FastAPI, Path, Query +from starlette.status import HTTP_201_CREATED from pydantic import BaseModel from pydantic.types import UrlStr -from starlette.status import HTTP_201_CREATED -from starlette.responses import HTMLResponse +from typing import Set, List app = FastAPI() -@app.get("/items/", content_type=HTMLResponse) -async def read_items(): - return """ - -
-