]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:truck: Re-sort tutorial example files
authorSebastián Ramírez <tiangolo@gmail.com>
Thu, 13 Dec 2018 17:25:45 +0000 (21:25 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Thu, 13 Dec 2018 17:25:45 +0000 (21:25 +0400)
71 files changed:
docs/tutorial/src/tutorial05-04.py [deleted file]
docs/tutorial/src/tutorial05.py [moved from docs/tutorial/src/tutorial05-01.py with 100% similarity]
docs/tutorial/src/tutorial06.py
docs/tutorial/src/tutorial07.py
docs/tutorial/src/tutorial08.py
docs/tutorial/src/tutorial09.py
docs/tutorial/src/tutorial10.py
docs/tutorial/src/tutorial11.py
docs/tutorial/src/tutorial12.py
docs/tutorial/src/tutorial13.py
docs/tutorial/src/tutorial14.py
docs/tutorial/src/tutorial15.py
docs/tutorial/src/tutorial16.py
docs/tutorial/src/tutorial17.py
docs/tutorial/src/tutorial18-01.py [deleted file]
docs/tutorial/src/tutorial18-03.py [deleted file]
docs/tutorial/src/tutorial18.py [moved from docs/tutorial/src/tutorial18-02.py with 94% similarity]
docs/tutorial/src/tutorial19.py
docs/tutorial/src/tutorial20.py
docs/tutorial/src/tutorial21.py
docs/tutorial/src/tutorial22.py
docs/tutorial/src/tutorial23.py
docs/tutorial/src/tutorial24.py
docs/tutorial/src/tutorial25.py
docs/tutorial/src/tutorial26.py
docs/tutorial/src/tutorial27.py
docs/tutorial/src/tutorial28.py
docs/tutorial/src/tutorial29.py
docs/tutorial/src/tutorial30.py
docs/tutorial/src/tutorial31.py
docs/tutorial/src/tutorial32.py
docs/tutorial/src/tutorial33.py
docs/tutorial/src/tutorial34.py
docs/tutorial/src/tutorial35.py
docs/tutorial/src/tutorial36.py
docs/tutorial/src/tutorial37.py
docs/tutorial/src/tutorial38.py
docs/tutorial/src/tutorial39.py
docs/tutorial/src/tutorial40.py
docs/tutorial/src/tutorial41.py
docs/tutorial/src/tutorial42.py
docs/tutorial/src/tutorial43.py
docs/tutorial/src/tutorial44.py
docs/tutorial/src/tutorial45.py
docs/tutorial/src/tutorial46.py
docs/tutorial/src/tutorial47-01.py [deleted file]
docs/tutorial/src/tutorial47-02.py [deleted file]
docs/tutorial/src/tutorial47-03.py [deleted file]
docs/tutorial/src/tutorial47-04.py [deleted file]
docs/tutorial/src/tutorial47.py [new file with mode: 0644]
docs/tutorial/src/tutorial48.py
docs/tutorial/src/tutorial49.py
docs/tutorial/src/tutorial50.py
docs/tutorial/src/tutorial51.py
docs/tutorial/src/tutorial52.py
docs/tutorial/src/tutorial53.py
docs/tutorial/src/tutorial54.py
docs/tutorial/src/tutorial55.py
docs/tutorial/src/tutorial56.py
docs/tutorial/src/tutorial57.py
docs/tutorial/src/tutorial58.py [new file with mode: 0644]
docs/tutorial/src/tutorial59.py
docs/tutorial/src/tutorial60.py
docs/tutorial/src/tutorial61.py [new file with mode: 0644]
docs/tutorial/src/tutorial62.py [new file with mode: 0644]
docs/tutorial/src/tutorial63.py [new file with mode: 0644]
docs/tutorial/src/tutorial64.py [moved from docs/tutorial/src/tutorial58-01.py with 100% similarity]
docs/tutorial/src/tutorial65.py [moved from docs/tutorial/src/tutorial58-02.py with 100% similarity]
docs/tutorial/src/tutorial66.py [moved from docs/tutorial/src/tutorial58-03.py with 100% similarity]
docs/tutorial/src/tutorial67.py [new file with mode: 0644]
docs/tutorial/src/tutorial68.py [new file with mode: 0644]

diff --git a/docs/tutorial/src/tutorial05-04.py b/docs/tutorial/src/tutorial05-04.py
deleted file mode 100644 (file)
index 54191b6..0000000
+++ /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}
index 30f0cd6d5bc32a108f92142772eeaff79a4d8e1c..54191b66a0a5bbb26911dd19c65894aa5b401c3d 100644 (file)
@@ -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}
index d45ebba9fd44b22328fc7514b3cb265e80ee5afe..30f0cd6d5bc32a108f92142772eeaff79a4d8e1c 100644 (file)
@@ -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:
index c0b12279317456b6ac409142938abd4db6c3df21..d45ebba9fd44b22328fc7514b3cb265e80ee5afe 100644 (file)
@@ -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:
index 78b813f52892946e39b08f672655dfdf52f61fb8..c0b12279317456b6ac409142938abd4db6c3df21 100644 (file)
@@ -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
index 925f11062b0abf9d744a329e39543489557b606f..78b813f52892946e39b08f672655dfdf52f61fb8 100644 (file)
@@ -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
index 34e9849a1ce148cad64ba2a18f3fdcaff8fea646..925f11062b0abf9d744a329e39543489557b606f 100644 (file)
@@ -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
index be6239d8fb62b122e59cab9f98da354a39d93707..34e9849a1ce148cad64ba2a18f3fdcaff8fea646 100644 (file)
@@ -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()}
index 183180cd742b03fa1152ab5bf207f85d01051eaf..be6239d8fb62b122e59cab9f98da354a39d93707 100644 (file)
@@ -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
index 311ece816a59bc45a89ee3c3e2d9c66ff31c4089..183180cd742b03fa1152ab5bf207f85d01051eaf 100644 (file)
@@ -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})
index bdaf5c1a4bd6af369a64ed39465bf6f9297bc10d..311ece816a59bc45a89ee3c3e2d9c66ff31c4089 100644 (file)
@@ -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})
index 116829bef9303182e1dba1d00debd60bcc3aba7a..bdaf5c1a4bd6af369a64ed39465bf6f9297bc10d 100644 (file)
@@ -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})
index 67d5fd6a5ef0d32b5f491d07caa1fb22368c0ddc..116829bef9303182e1dba1d00debd60bcc3aba7a 100644 (file)
@@ -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 (file)
index a4e3b81..0000000
+++ /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 (file)
index b9bffca..0000000
+++ /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
similarity index 94%
rename from docs/tutorial/src/tutorial18-02.py
rename to docs/tutorial/src/tutorial18.py
index f261c955483fc3dca96f4d62ee836cd45f8be007..67d5fd6a5ef0d32b5f491d07caa1fb22368c0ddc 100644 (file)
@@ -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,
index 051656c768b62435b608c58824f622fd0d662b0f..a4e3b816cff403f46646295516f606436151ec56 100644 (file)
@@ -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"}]}
index 0600c7f832e7871fc8a5092ded1ffc987c7b3923..f261c955483fc3dca96f4d62ee836cd45f8be007 100644 (file)
@@ -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
index c14950716ebfaebb98a541e15ae77f6578f00497..b9bffcaff63c595e9cc4ff8d80ca280bf1a77281 100644 (file)
@@ -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
index 4ac37c409806a2f3c479d06f9efd9471b0a2e56f..051656c768b62435b608c58824f622fd0d662b0f 100644 (file)
@@ -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
index 97611c273d725f02bf3d339e3e1da8e6ecef1924..0600c7f832e7871fc8a5092ded1ffc987c7b3923 100644 (file)
@@ -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:
index 4dabe35379a22b7d8b257c2b7859fdf0d2e2ebb1..c14950716ebfaebb98a541e15ae77f6578f00497 100644 (file)
@@ -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:
index 72b2bba1ff3c4f2703bbd49f48eef57d9bf4b537..4ac37c409806a2f3c479d06f9efd9471b0a2e56f 100644 (file)
@@ -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:
index 00836004e7a40f6f71369b36668584dca1676a52..97611c273d725f02bf3d339e3e1da8e6ecef1924 100644 (file)
@@ -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
index 31dbb82c949cc0855ea3bf2c3ceee9fa182703ac..4dabe35379a22b7d8b257c2b7859fdf0d2e2ebb1 100644 (file)
@@ -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
index a1302229bdf441af9e034393413815b55f625c4f..72b2bba1ff3c4f2703bbd49f48eef57d9bf4b537 100644 (file)
@@ -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
index 584451ed4b190c817a8d0bd47a2d969b9e05a9ac..00836004e7a40f6f71369b36668584dca1676a52 100644 (file)
@@ -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
index 559356f99ba9f2a590e74d7d3e744adb0bd32500..31dbb82c949cc0855ea3bf2c3ceee9fa182703ac 100644 (file)
@@ -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
index 89b97e8c8d3fbca3425ff0a758574aec4a004231..a1302229bdf441af9e034393413815b55f625c4f 100644 (file)
@@ -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
index e6fa5843ae1c9f889b774a0f34aadf35b76c0829..584451ed4b190c817a8d0bd47a2d969b9e05a9ac 100644 (file)
@@ -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
index 652ec7c57a5909d6a4907474e23b941049124e22..559356f99ba9f2a590e74d7d3e744adb0bd32500 100644 (file)
@@ -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
index 444f73ac5280383c03fc2a5a1415ff5422c5284a..89b97e8c8d3fbca3425ff0a758574aec4a004231 100644 (file)
@@ -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
index 6cfbeff397350e7f1e527f69344f41cf2ab30160..e6fa5843ae1c9f889b774a0f34aadf35b76c0829 100644 (file)
@@ -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}")
index d04e72d80b32dc9e55c3a4ff010b5ddc0606e813..652ec7c57a5909d6a4907474e23b941049124e22 100644 (file)
@@ -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}")
index e5c25e4b6555f2d0a36bc64af9f0456efb3bbc1b..444f73ac5280383c03fc2a5a1415ff5422c5284a 100644 (file)
@@ -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}")
index 3dd1a05547f9935bb3927347fae52fa6ed891e29..6cfbeff397350e7f1e527f69344f41cf2ab30160 100644 (file)
@@ -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
index b57a243d73e4a2344d133c4216999608ebe89c64..d04e72d80b32dc9e55c3a4ff010b5ddc0606e813 100644 (file)
@@ -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
index f1cc8b2c6daed13513c54ee7a40e4176ca11604c..e5c25e4b6555f2d0a36bc64af9f0456efb3bbc1b 100644 (file)
@@ -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
index 9dbb9912c5e580c847cc26a74d5ed1843ce554d1..3dd1a05547f9935bb3927347fae52fa6ed891e29 100644 (file)
@@ -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
index 070c7fe51880b772f2cbaacb0b7115c166c417cf..b57a243d73e4a2344d133c4216999608ebe89c64 100644 (file)
@@ -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
index e892782700ff217dc8c8a75f45952a40aa56915c..f1cc8b2c6daed13513c54ee7a40e4176ca11604c 100644 (file)
@@ -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}
index 5ec7fa4aad91ecb1ce57018c920c40ff00b4ce15..9dbb9912c5e580c847cc26a74d5ed1843ce554d1 100644 (file)
@@ -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}
index d9cdd86d5e1a21303f2dbf697e48827232f699d9..070c7fe51880b772f2cbaacb0b7115c166c417cf 100644 (file)
@@ -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}
index c2811eba013c48cbafd445470557408408d8c583..e892782700ff217dc8c8a75f45952a40aa56915c 100644 (file)
@@ -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 (file)
index b61e62b..0000000
+++ /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 (file)
index 52a93d7..0000000
+++ /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 (file)
index 3a517f7..0000000
+++ /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 (file)
index 19382b8..0000000
+++ /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 (file)
index 0000000..5ec7fa4
--- /dev/null
@@ -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
index fb32535c28ab6db4c2ff8f954d4c0d9a7fc0a108..d9cdd86d5e1a21303f2dbf697e48827232f699d9 100644 (file)
@@ -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
index bd0bef7f933a7e77eaca687b900a473a85544a5e..c2811eba013c48cbafd445470557408408d8c583 100644 (file)
@@ -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
index 8a163389d37b8822997efaadc15e547160b9def6..b61e62b83093489c1d5dfca7b1418d850be1e47e 100644 (file)
@@ -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
index 479ac7d58eae39396ef7009b27c20862cf60a7d3..52a93d7ffd365739f06677a20c6a650011ce3d24 100644 (file)
@@ -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}
index bec8a6ad2d720708dd953550b5fe2372a9136b4f..3a517f7a1ea56d7a9ea9bf42fbba79bd58e01f16 100644 (file)
@@ -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)}
index 1a77be023324429cec7c74f0d84b8a90aeabe736..19382b8150287116121d84a362c2f241f67fdd76 100644 (file)
@@ -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}
index 31793c985097671213dd15f14a4173d5b84a0dc5..fb32535c28ab6db4c2ff8f954d4c0d9a7fc0a108 100644 (file)
@@ -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
index 34f9e85633335bd12bf65e569ff1b5b34665b061..bd0bef7f933a7e77eaca687b900a473a85544a5e 100644 (file)
@@ -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
index dec2f53e20a6f108ae6d6b4d483536698201690f..8a163389d37b8822997efaadc15e547160b9def6 100644 (file)
@@ -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
index f2300f8d46e67e570b15503b3049311663e36891..479ac7d58eae39396ef7009b27c20862cf60a7d3 100644 (file)
@@ -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 """
-    <html>
-        <head>
-            <title>Some HTML in here</title>
-        </head>
-        <body>
-            <h1>Look ma! HTML!</h1>
-        </body>
-    </html>
+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
diff --git a/docs/tutorial/src/tutorial58.py b/docs/tutorial/src/tutorial58.py
new file mode 100644 (file)
index 0000000..bec8a6a
--- /dev/null
@@ -0,0 +1,29 @@
+from fastapi import Body, FastAPI, Path, Query
+from starlette.status import HTTP_201_CREATED
+from pydantic import BaseModel
+from pydantic.types import UrlStr
+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
index 442a93d176ae9cbc2e73a3d8a64ffd426f3816f5..1a77be023324429cec7c74f0d84b8a90aeabe736 100644 (file)
@@ -1,59 +1,13 @@
 from typing import List, Set
 
-from fastapi import Body, FastAPI, Path, Query, Depends, Cookie
+from fastapi import Body, FastAPI, Path, Query
 from pydantic import BaseModel
 from pydantic.types import UrlStr
 from starlette.status import HTTP_201_CREATED
-from starlette.responses import HTMLResponse
-from random import choice
 
-from sqlalchemy import create_engine
-from sqlalchemy.orm import scoped_session, sessionmaker
-from sqlalchemy import Column, Integer, DateTime, String, Boolean, ForeignKey
-from sqlalchemy.ext.declarative import declarative_base, declared_attr
-
-
-# SQLAlchemy specific code, as with any other app
-
-
-SQLALCHEMY_DATABASE_URI = "postgresql://user:password@postgresserver/db"
-
-# By creating this a CustomBase class and inheriting from it, your models will have
-# automatic __tablename__ attributes. So you don't have to declare them.
-# So, your models will behave very similarly to, for example, Flask-SQLAlchemy
-
-class CustomBase:
-    # Generate __tablename__ automatically
-    @declared_attr
-    def __tablename__(cls):
-        return cls.__name__.lower()
-
-
-Base = declarative_base(cls=CustomBase)
-
-
-class User(Base):
-    # Own properties
-    id = Column(Integer, primary_key=True, index=True)
-    email = Column(String, unique=True, index=True)
-    hashed_password = Column(String)
-    is_active = Column(Boolean(), default=True)
-
-
-engine = create_engine(SQLALCHEMY_DATABASE_URI, convert_unicode=True)
-db_session = scoped_session(
-    sessionmaker(autocommit=False, autoflush=False, bind=engine)
-)
-
-
-def get_user(username, db_session):
-    return db_session.query(User).filter(User.id == username).first()
-
-# FastAPI specific code
 app = FastAPI()
 
 
-@app.get("/users/{username}")
-def read_user(username: str):
-    user = get_user(username, db_session)
-    return user
+@app.get("/items/", deprecated=True)
+async def read_items():
+    return [{"item_id": "Foo"}]
index 9725142caddc204341343b71bb1ef6d233f6ae07..31793c985097671213dd15f14a4173d5b84a0dc5 100644 (file)
@@ -1,66 +1,13 @@
 from typing import List, Set
 
-from fastapi import Body, FastAPI, Path, Query, Depends, Cookie
+from fastapi import Body, FastAPI, Path, Query
 from pydantic import BaseModel
 from pydantic.types import UrlStr
 from starlette.status import HTTP_201_CREATED
-from starlette.responses import HTMLResponse
-from random import choice
 
-from typing import List, Optional, Union
-
-from pydantic import BaseModel
-
-from app.models.config import USERPROFILE_DOC_TYPE
-from app.models.role import RoleEnum
-from couchbase.bucket import Bucket
-from couchbase.cluster import Cluster, PasswordAuthenticator
-from couchbase import LOCKMODE_WAIT
-
-
-def get_bucket():
-    cluster = Cluster("couchbase://couchbasehost:8091")
-    authenticator = PasswordAuthenticator("username", "password")
-    cluster.authenticate(authenticator)
-    bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
-    return bucket
-
-
-class User(BaseModel):
-    username: str
-    email: Optional[str] = None
-    full_name: Optional[str] = None
-    disabled: Optional[bool] = None
-
-
-class UserInDB(User):
-    type: str = USERPROFILE_DOC_TYPE
-    hashed_password: str
-
-    class Meta:
-        key: Optional[str] = None
-
-
-def get_user_doc_id(username):
-    return f"userprofile::{username}"
-
-
-def get_user(bucket: Bucket, username: str):
-    doc_id = get_user_doc_id(username)
-    result = bucket.get(doc_id, quiet=True)
-    if not result.value:
-        return None
-    user = UserInDB(**result.value)
-    user.Meta.key = result.key
-    return user
-
-
-# FastAPI specific code
 app = FastAPI()
 
 
-@app.get("/users/{username}")
-def read_user(username: str):
-    bucket = get_bucket()
-    user = get_user(bucket=bucket, username=username)
-    return user
+@app.get("/items/", operation_id="some_specific_id_you_define")
+async def read_items():
+    return [{"item_id": "Foo"}]
diff --git a/docs/tutorial/src/tutorial61.py b/docs/tutorial/src/tutorial61.py
new file mode 100644 (file)
index 0000000..34f9e85
--- /dev/null
@@ -0,0 +1,13 @@
+from typing import List, Set
+
+from fastapi import Body, FastAPI, Path, Query
+from pydantic import BaseModel
+from pydantic.types import UrlStr
+from starlette.status import HTTP_201_CREATED
+
+app = FastAPI()
+
+
+@app.get("/items/", include_in_schema=False)
+async def read_items():
+    return [{"item_id": "Foo"}]
diff --git a/docs/tutorial/src/tutorial62.py b/docs/tutorial/src/tutorial62.py
new file mode 100644 (file)
index 0000000..dec2f53
--- /dev/null
@@ -0,0 +1,14 @@
+from typing import List, Set
+
+from fastapi import Body, FastAPI, Path, Query
+from pydantic import BaseModel
+from pydantic.types import UrlStr
+from starlette.status import HTTP_201_CREATED
+from starlette.responses import UJSONResponse
+
+app = FastAPI()
+
+
+@app.get("/items/", content_type=UJSONResponse)
+async def read_items():
+    return [{"item_id": "Foo"}]
diff --git a/docs/tutorial/src/tutorial63.py b/docs/tutorial/src/tutorial63.py
new file mode 100644 (file)
index 0000000..f2300f8
--- /dev/null
@@ -0,0 +1,23 @@
+from typing import List, Set
+
+from fastapi import Body, FastAPI, Path, Query
+from pydantic import BaseModel
+from pydantic.types import UrlStr
+from starlette.status import HTTP_201_CREATED
+from starlette.responses import HTMLResponse
+
+app = FastAPI()
+
+
+@app.get("/items/", content_type=HTMLResponse)
+async def read_items():
+    return """
+    <html>
+        <head>
+            <title>Some HTML in here</title>
+        </head>
+        <body>
+            <h1>Look ma! HTML!</h1>
+        </body>
+    </html>
+    """
diff --git a/docs/tutorial/src/tutorial67.py b/docs/tutorial/src/tutorial67.py
new file mode 100644 (file)
index 0000000..442a93d
--- /dev/null
@@ -0,0 +1,59 @@
+from typing import List, Set
+
+from fastapi import Body, FastAPI, Path, Query, Depends, Cookie
+from pydantic import BaseModel
+from pydantic.types import UrlStr
+from starlette.status import HTTP_201_CREATED
+from starlette.responses import HTMLResponse
+from random import choice
+
+from sqlalchemy import create_engine
+from sqlalchemy.orm import scoped_session, sessionmaker
+from sqlalchemy import Column, Integer, DateTime, String, Boolean, ForeignKey
+from sqlalchemy.ext.declarative import declarative_base, declared_attr
+
+
+# SQLAlchemy specific code, as with any other app
+
+
+SQLALCHEMY_DATABASE_URI = "postgresql://user:password@postgresserver/db"
+
+# By creating this a CustomBase class and inheriting from it, your models will have
+# automatic __tablename__ attributes. So you don't have to declare them.
+# So, your models will behave very similarly to, for example, Flask-SQLAlchemy
+
+class CustomBase:
+    # Generate __tablename__ automatically
+    @declared_attr
+    def __tablename__(cls):
+        return cls.__name__.lower()
+
+
+Base = declarative_base(cls=CustomBase)
+
+
+class User(Base):
+    # Own properties
+    id = Column(Integer, primary_key=True, index=True)
+    email = Column(String, unique=True, index=True)
+    hashed_password = Column(String)
+    is_active = Column(Boolean(), default=True)
+
+
+engine = create_engine(SQLALCHEMY_DATABASE_URI, convert_unicode=True)
+db_session = scoped_session(
+    sessionmaker(autocommit=False, autoflush=False, bind=engine)
+)
+
+
+def get_user(username, db_session):
+    return db_session.query(User).filter(User.id == username).first()
+
+# FastAPI specific code
+app = FastAPI()
+
+
+@app.get("/users/{username}")
+def read_user(username: str):
+    user = get_user(username, db_session)
+    return user
diff --git a/docs/tutorial/src/tutorial68.py b/docs/tutorial/src/tutorial68.py
new file mode 100644 (file)
index 0000000..9725142
--- /dev/null
@@ -0,0 +1,66 @@
+from typing import List, Set
+
+from fastapi import Body, FastAPI, Path, Query, Depends, Cookie
+from pydantic import BaseModel
+from pydantic.types import UrlStr
+from starlette.status import HTTP_201_CREATED
+from starlette.responses import HTMLResponse
+from random import choice
+
+from typing import List, Optional, Union
+
+from pydantic import BaseModel
+
+from app.models.config import USERPROFILE_DOC_TYPE
+from app.models.role import RoleEnum
+from couchbase.bucket import Bucket
+from couchbase.cluster import Cluster, PasswordAuthenticator
+from couchbase import LOCKMODE_WAIT
+
+
+def get_bucket():
+    cluster = Cluster("couchbase://couchbasehost:8091")
+    authenticator = PasswordAuthenticator("username", "password")
+    cluster.authenticate(authenticator)
+    bucket: Bucket = cluster.open_bucket("bucket_name", lockmode=LOCKMODE_WAIT)
+    return bucket
+
+
+class User(BaseModel):
+    username: str
+    email: Optional[str] = None
+    full_name: Optional[str] = None
+    disabled: Optional[bool] = None
+
+
+class UserInDB(User):
+    type: str = USERPROFILE_DOC_TYPE
+    hashed_password: str
+
+    class Meta:
+        key: Optional[str] = None
+
+
+def get_user_doc_id(username):
+    return f"userprofile::{username}"
+
+
+def get_user(bucket: Bucket, username: str):
+    doc_id = get_user_doc_id(username)
+    result = bucket.get(doc_id, quiet=True)
+    if not result.value:
+        return None
+    user = UserInDB(**result.value)
+    user.Meta.key = result.key
+    return user
+
+
+# FastAPI specific code
+app = FastAPI()
+
+
+@app.get("/users/{username}")
+def read_user(username: str):
+    bucket = get_bucket()
+    user = get_user(bucket=bucket, username=username)
+    return user