Browse Source

📝 Add stubs for Security, multi-files, app configurations

pull/11/head
Sebastián Ramírez 6 years ago
parent
commit
70b0231f64
  1. 13
      docs/tutorial/application-configuration.md
  2. 13
      docs/tutorial/bigger-applications.md
  3. 2
      docs/tutorial/dependencies/intro.md
  4. 5
      docs/tutorial/security/first-steps.md
  5. 5
      docs/tutorial/security/intro.md
  6. 5
      docs/tutorial/security/oauth2-jwt.md
  7. 5
      docs/tutorial/security/simple-oauth2.md
  8. 11
      docs/tutorial/src/all/tutorial001.py
  9. 11
      docs/tutorial/src/all/tutorial002.py
  10. 13
      docs/tutorial/src/all/tutorial003.py
  11. 15
      docs/tutorial/src/all/tutorial004.py
  12. 20
      docs/tutorial/src/all/tutorial005.py
  13. 21
      docs/tutorial/src/all/tutorial006.py
  14. 21
      docs/tutorial/src/all/tutorial007.py
  15. 21
      docs/tutorial/src/all/tutorial008.py
  16. 22
      docs/tutorial/src/all/tutorial009.py
  17. 14
      docs/tutorial/src/all/tutorial010.py
  18. 13
      docs/tutorial/src/all/tutorial011.py
  19. 13
      docs/tutorial/src/all/tutorial012.py
  20. 13
      docs/tutorial/src/all/tutorial013.py
  21. 15
      docs/tutorial/src/all/tutorial014.py
  22. 16
      docs/tutorial/src/all/tutorial015.py
  23. 26
      docs/tutorial/src/all/tutorial016.py
  24. 22
      docs/tutorial/src/all/tutorial017.py
  25. 29
      docs/tutorial/src/all/tutorial018.py
  26. 36
      docs/tutorial/src/all/tutorial019.py
  27. 17
      docs/tutorial/src/all/tutorial020.py
  28. 17
      docs/tutorial/src/all/tutorial021.py
  29. 29
      docs/tutorial/src/all/tutorial022.py
  30. 18
      docs/tutorial/src/all/tutorial023.py
  31. 20
      docs/tutorial/src/all/tutorial024.py
  32. 20
      docs/tutorial/src/all/tutorial025.py
  33. 26
      docs/tutorial/src/all/tutorial026.py
  34. 27
      docs/tutorial/src/all/tutorial027.py
  35. 27
      docs/tutorial/src/all/tutorial028.py
  36. 33
      docs/tutorial/src/all/tutorial029.py
  37. 17
      docs/tutorial/src/all/tutorial030.py
  38. 8
      docs/tutorial/src/all/tutorial031.py
  39. 8
      docs/tutorial/src/all/tutorial032.py
  40. 8
      docs/tutorial/src/all/tutorial033.py
  41. 19
      docs/tutorial/src/all/tutorial034.py
  42. 18
      docs/tutorial/src/all/tutorial035.py
  43. 23
      docs/tutorial/src/all/tutorial036.py
  44. 42
      docs/tutorial/src/all/tutorial037.py
  45. 40
      docs/tutorial/src/all/tutorial038.py
  46. 8
      docs/tutorial/src/all/tutorial039.py
  47. 8
      docs/tutorial/src/all/tutorial040.py
  48. 8
      docs/tutorial/src/all/tutorial041.py
  49. 20
      docs/tutorial/src/all/tutorial042.py
  50. 19
      docs/tutorial/src/all/tutorial043.py
  51. 24
      docs/tutorial/src/all/tutorial044.py
  52. 28
      docs/tutorial/src/all/tutorial045.py
  53. 33
      docs/tutorial/src/all/tutorial046.py
  54. 8
      docs/tutorial/src/all/tutorial047.py
  55. 8
      docs/tutorial/src/all/tutorial048.py
  56. 8
      docs/tutorial/src/all/tutorial049.py
  57. 9
      docs/tutorial/src/all/tutorial050.py
  58. 18
      docs/tutorial/src/all/tutorial051.py
  59. 27
      docs/tutorial/src/all/tutorial052.py
  60. 34
      docs/tutorial/src/all/tutorial053.py
  61. 49
      docs/tutorial/src/all/tutorial054.py
  62. 52
      docs/tutorial/src/all/tutorial055.py
  63. 57
      docs/tutorial/src/all/tutorial056.py
  64. 0
      docs/tutorial/src/application-configuration/tutorial001.py
  65. 0
      docs/tutorial/src/application-configuration/tutorial002.py
  66. 0
      docs/tutorial/src/application-configuration/tutorial003.py
  67. 0
      docs/tutorial/src/bigger-applications/tutorial001.py
  68. 0
      docs/tutorial/src/bigger-applications/tutorial002.py
  69. 4
      docs/tutorial/src/bigger-applications/tutorial003.py
  70. 0
      docs/tutorial/src/security/tutorial001.py
  71. 0
      docs/tutorial/src/security/tutorial002.py
  72. 0
      docs/tutorial/src/security/tutorial003.py
  73. 0
      docs/tutorial/src/security/tutorial004.py
  74. 12
      mkdocs.yml

13
docs/tutorial/application-configuration.md

@ -0,0 +1,13 @@
Coming soon...
```Python
{!./tutorial/src/application-configuration/tutorial001.py!}
```
```Python
{!./tutorial/src/application-configuration/tutorial002.py!}
```
```Python
{!./tutorial/src/application-configuration/tutorial003.py!}
```

13
docs/tutorial/bigger-applications.md

@ -0,0 +1,13 @@
Coming soon...
```Python
{!./tutorial/src/bigger-applications/tutorial001.py!}
```
```Python
{!./tutorial/src/bigger-applications/tutorial002.py!}
```
```Python
{!./tutorial/src/bigger-applications/tutorial003.py!}
```

2
docs/tutorial/dependencies/intro.md

@ -51,7 +51,7 @@ You can define dependencies that in turn can define dependencies themselves.
In the end, a hierarchical tree of dependencies is built, and the **Dependency Injection** system takes care of solving all these dependencies for you (and your dependencies) and providing the results at each step.
## Integrated with OpenAPI
## Integrated with **OpenAPI**
All these dependencies, while declaring their requirements, might have been adding parameters, validations, etc. to your path operations.

5
docs/tutorial/security/first-steps.md

@ -0,0 +1,5 @@
Coming soon...
```Python
{!./tutorial/src/security/tutorial002.py!}
```

5
docs/tutorial/security/intro.md

@ -0,0 +1,5 @@
Coming soon...
```Python
{!./tutorial/src/security/tutorial001.py!}
```

5
docs/tutorial/security/oauth2-jwt.md

@ -0,0 +1,5 @@
Coming soon...
```Python
{!./tutorial/src/security/tutorial004.py!}
```

5
docs/tutorial/security/simple-oauth2.md

@ -0,0 +1,5 @@
Coming soon...
```Python
{!./tutorial/src/security/tutorial003.py!}
```

11
docs/tutorial/src/all/tutorial001.py

@ -1,11 +0,0 @@
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
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})
return results

11
docs/tutorial/src/all/tutorial002.py

@ -1,11 +0,0 @@
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
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})
return results

13
docs/tutorial/src/all/tutorial003.py

@ -1,13 +0,0 @@
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
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})
return results

15
docs/tutorial/src/all/tutorial004.py

@ -1,15 +0,0 @@
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
async def read_items(
q: str = Query(
None, title="Query string", min_length=3, max_length=50, regex="^fixedquery$"
)
):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
return results

20
docs/tutorial/src/all/tutorial005.py

@ -1,20 +0,0 @@
from fastapi import FastAPI, Query
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$",
)
):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
return results

21
docs/tutorial/src/all/tutorial006.py

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

21
docs/tutorial/src/all/tutorial007.py

@ -1,21 +0,0 @@
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
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,
max_length=50,
regex="^fixedquery$",
)
):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
return results

21
docs/tutorial/src/all/tutorial008.py

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

22
docs/tutorial/src/all/tutorial009.py

@ -1,22 +0,0 @@
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
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,
max_length=50,
regex="^fixedquery$",
deprecated=True,
)
):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
return results

14
docs/tutorial/src/all/tutorial010.py

@ -1,14 +0,0 @@
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
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:
results.update({"q": q})
return results

13
docs/tutorial/src/all/tutorial011.py

@ -1,13 +0,0 @@
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
q: str, item_id: int = Path(..., title="The ID of the item to get")
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results

13
docs/tutorial/src/all/tutorial012.py

@ -1,13 +0,0 @@
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
*, item_id: int = Path(..., title="The ID of the item to get"), q: str
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results

13
docs/tutorial/src/all/tutorial013.py

@ -1,13 +0,0 @@
from fastapi import FastAPI, Path
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
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results

15
docs/tutorial/src/all/tutorial014.py

@ -1,15 +0,0 @@
from fastapi import FastAPI, Path
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,
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results

16
docs/tutorial/src/all/tutorial015.py

@ -1,16 +0,0 @@
from fastapi import FastAPI, Path, Query
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),
q: str,
size: float = Query(..., gt=0, lt=10.5)
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results

26
docs/tutorial/src/all/tutorial016.py

@ -1,26 +0,0 @@
from fastapi import FastAPI, 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(
*,
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}
if q:
results.update({"q": q})
if item:
results.update({"item": item})
return results

22
docs/tutorial/src/all/tutorial017.py

@ -1,22 +0,0 @@
from fastapi import FastAPI
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(*, item_id: int, item: Item, user: User):
results = {"item_id": item_id, "item": item, "user": user}
return results

29
docs/tutorial/src/all/tutorial018.py

@ -1,29 +0,0 @@
from fastapi import Body, FastAPI
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(
*, item_id: int, item: Item, user: User, access_token: str = Body(...)
):
results = {
"item_id": item_id,
"item": item,
"user": user,
"access_token": access_token,
}
return results

36
docs/tutorial/src/all/tutorial019.py

@ -1,36 +0,0 @@
from fastapi import Body, FastAPI
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(
*,
item_id: int,
item: Item,
user: User,
access_token: str = Body(...),
q: str = None,
):
results = {
"item_id": item_id,
"item": item,
"user": user,
"access_token": access_token,
}
if q:
results.update({"q": q})
return results

17
docs/tutorial/src/all/tutorial020.py

@ -1,17 +0,0 @@
from fastapi import Body, FastAPI
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(*, item_id: int, item: Item = Body(..., embed=True)):
results = {"item_id": item_id, "item": item}
return results

17
docs/tutorial/src/all/tutorial021.py

@ -1,17 +0,0 @@
from fastapi import Body, FastAPI
from pydantic import BaseModel, Schema
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")
tax: float = None
@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item = Body(..., embed=True)):
results = {"item_id": item_id, "item": item}
return results

29
docs/tutorial/src/all/tutorial022.py

@ -1,29 +0,0 @@
from fastapi import Body, FastAPI
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(
*,
item_id: int,
item: Item = Body(
...,
example={
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
)
):
results = {"item_id": item_id, "item": item}
return results

18
docs/tutorial/src/all/tutorial023.py

@ -1,18 +0,0 @@
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
tags: list = []
@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

20
docs/tutorial/src/all/tutorial024.py

@ -1,20 +0,0 @@
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
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):
results = {"item_id": item_id, "item": item}
return results

20
docs/tutorial/src/all/tutorial025.py

@ -1,20 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
tags: Set[str] = []
@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

26
docs/tutorial/src/all/tutorial026.py

@ -1,26 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
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
@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

27
docs/tutorial/src/all/tutorial027.py

@ -1,27 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.types import UrlStr
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
@app.put("/items/{item_id}")
async def update_item(*, item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

27
docs/tutorial/src/all/tutorial028.py

@ -1,27 +0,0 @@
from typing import List, Set
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.types import UrlStr
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}")
async def update_item(*, item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results

33
docs/tutorial/src/all/tutorial029.py

@ -1,33 +0,0 @@
from typing import List, Set
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.types import UrlStr
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
class Offer(BaseModel):
name: str
description: str = None
price: float
items: List[Item]
@app.post("/offers/")
async def create_offer(*, offer: Offer):
return offer

17
docs/tutorial/src/all/tutorial030.py

@ -1,17 +0,0 @@
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.types import UrlStr
app = FastAPI()
class Image(BaseModel):
url: UrlStr
name: str
@app.post("/images/multiple/")
async def create_multiple_images(*, images: List[Image]):
return images

8
docs/tutorial/src/all/tutorial031.py

@ -1,8 +0,0 @@
from fastapi import Cookie, FastAPI
app = FastAPI()
@app.get("/items/")
async def read_items(*, ads_id: str = Cookie(None)):
return {"ads_id": ads_id}

8
docs/tutorial/src/all/tutorial032.py

@ -1,8 +0,0 @@
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(*, accept_encoding: str = Header(None)):
return {"Accept-Encoding": accept_encoding}

8
docs/tutorial/src/all/tutorial033.py

@ -1,8 +0,0 @@
from fastapi import FastAPI, Header
app = FastAPI()
@app.get("/items/")
async def read_items(*, strange_header: str = Header(None, convert_underscores=False)):
return {"strange_header": strange_header}

19
docs/tutorial/src/all/tutorial034.py

@ -1,19 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
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

18
docs/tutorial/src/all/tutorial035.py

@ -1,18 +0,0 @@
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.types import EmailStr
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

23
docs/tutorial/src/all/tutorial036.py

@ -1,23 +0,0 @@
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.types import EmailStr
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

42
docs/tutorial/src/all/tutorial037.py

@ -1,42 +0,0 @@
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.types import EmailStr
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
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

40
docs/tutorial/src/all/tutorial038.py

@ -1,40 +0,0 @@
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.types import EmailStr
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

8
docs/tutorial/src/all/tutorial039.py

@ -1,8 +0,0 @@
from fastapi import FastAPI, Form
app = FastAPI()
@app.post("/login/")
async def login(*, username: str = Form(...), password: str = Form(...)):
return {"username": username}

8
docs/tutorial/src/all/tutorial040.py

@ -1,8 +0,0 @@
from fastapi import FastAPI, File
app = FastAPI()
@app.post("/files/")
async def create_file(*, file: bytes = File(...)):
return {"file_size": len(file)}

8
docs/tutorial/src/all/tutorial041.py

@ -1,8 +0,0 @@
from fastapi import FastAPI, File, Form
app = FastAPI()
@app.post("/files/")
async def create_file(*, file: bytes = File(...), token: str = Form(...)):
return {"file_size": len(file), "token": token}

20
docs/tutorial/src/all/tutorial042.py

@ -1,20 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
from starlette.status import HTTP_201_CREATED
app = FastAPI()
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

19
docs/tutorial/src/all/tutorial043.py

@ -1,19 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
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

24
docs/tutorial/src/all/tutorial044.py

@ -1,24 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
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",
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

28
docs/tutorial/src/all/tutorial045.py

@ -1,28 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
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

33
docs/tutorial/src/all/tutorial046.py

@ -1,33 +0,0 @@
from typing import Set
from fastapi import FastAPI
from pydantic import BaseModel
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

8
docs/tutorial/src/all/tutorial047.py

@ -1,8 +0,0 @@
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/", deprecated=True)
async def read_items():
return [{"item_id": "Foo"}]

8
docs/tutorial/src/all/tutorial048.py

@ -1,8 +0,0 @@
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/", operation_id="some_specific_id_you_define")
async def read_items():
return [{"item_id": "Foo"}]

8
docs/tutorial/src/all/tutorial049.py

@ -1,8 +0,0 @@
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/", include_in_schema=False)
async def read_items():
return [{"item_id": "Foo"}]

9
docs/tutorial/src/all/tutorial050.py

@ -1,9 +0,0 @@
from fastapi import FastAPI
from starlette.responses import UJSONResponse
app = FastAPI()
@app.get("/items/", content_type=UJSONResponse)
async def read_items():
return [{"item_id": "Foo"}]

18
docs/tutorial/src/all/tutorial051.py

@ -1,18 +0,0 @@
from fastapi import FastAPI
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>
"""

27
docs/tutorial/src/all/tutorial052.py

@ -1,27 +0,0 @@
from fastapi import Depends, FastAPI
from pydantic import BaseModel
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams(BaseModel):
q: str = None
skip: int = None
limit: int = None
async def common_parameters(q: str = None, skip: int = 0, limit: int = 100):
return CommonQueryParams(q=q, skip=skip, limit=limit)
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends(common_parameters)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.limit]
response.update({"items": items})
return response

34
docs/tutorial/src/all/tutorial053.py

@ -1,34 +0,0 @@
from typing import List
from fastapi import Cookie, Depends, FastAPI
from pydantic import BaseModel
app = FastAPI()
class InterestsTracker(BaseModel):
track_code: str
interests: List[str]
fake_tracked_users_db = {
"Foo": {"track_code": "Foo", "interests": ["sports", "movies"]},
"Bar": {"track_code": "Bar", "interests": ["food", "shows"]},
"Baz": {"track_code": "Baz", "interests": ["gaming", "virtual reality"]},
}
async def get_tracked_interests(track_code: str = Cookie(None)):
if track_code in fake_tracked_users_db:
track_dict = fake_tracked_users_db[track_code]
track = InterestsTracker(**track_dict)
return track
return None
@app.get("/interests/")
async def read_interests(
tracked_interests: InterestsTracker = Depends(get_tracked_interests)
):
response = {"interests": tracked_interests.interests}
return response

49
docs/tutorial/src/all/tutorial054.py

@ -1,49 +0,0 @@
from random import choice
from typing import List
from fastapi import Cookie, Depends, FastAPI
from pydantic import BaseModel
app = FastAPI()
class InterestsTracker(BaseModel):
track_code: str
interests: List[str]
fake_tracked_users_db = {
"Foo": {"track_code": "Foo", "interests": ["sports", "movies"]},
"Bar": {"track_code": "Bar", "interests": ["food", "shows"]},
"Baz": {"track_code": "Baz", "interests": ["gaming", "virtual reality"]},
}
async def get_tracked_interests(track_code: str = Cookie(None)):
if track_code in fake_tracked_users_db:
track_dict = fake_tracked_users_db[track_code]
track = InterestsTracker(**track_dict)
return track
return None
class ComplexTracker:
def __init__(self, tracker: InterestsTracker = Depends(get_tracked_interests)):
self.tracker = tracker
def random_interest(self):
"""
Get a random interest from the tracked ones for the current user.
If the user doesn't have tracked interests, return a random one from the ones available.
"""
if self.tracker.interests:
return choice(self.tracker.interests)
return choice(
["sports", "movies", "food", "shows", "gaming", "virtual reality"]
)
@app.get("/suggested-category")
async def read_suggested_category(tracker: ComplexTracker = Depends(None)):
response = {"category": tracker.random_interest()}
return response

52
docs/tutorial/src/all/tutorial055.py

@ -1,52 +0,0 @@
from fastapi import FastAPI
from sqlalchemy import Boolean, Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.orm import scoped_session, sessionmaker
# 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

57
docs/tutorial/src/all/tutorial056.py

@ -1,57 +0,0 @@
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
from app.models.config import USERPROFILE_DOC_TYPE
from couchbase import LOCKMODE_WAIT
from couchbase.bucket import Bucket
from couchbase.cluster import Cluster, PasswordAuthenticator
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

0
docs/tutorial/src/all/tutorial064.py → docs/tutorial/src/application-configuration/tutorial001.py

0
docs/tutorial/src/all/tutorial065.py → docs/tutorial/src/application-configuration/tutorial002.py

0
docs/tutorial/src/all/tutorial066.py → docs/tutorial/src/application-configuration/tutorial003.py

0
docs/tutorial/src/all/tutorial061.py → docs/tutorial/src/bigger-applications/tutorial001.py

0
docs/tutorial/src/all/tutorial062.py → docs/tutorial/src/bigger-applications/tutorial002.py

4
docs/tutorial/src/all/tutorial063.py → docs/tutorial/src/bigger-applications/tutorial003.py

@ -1,7 +1,7 @@
from fastapi import FastAPI
from .tutorial74 import router as users_router
from .tutorial75 import router as items_router
from .tutorial01 import router as users_router
from .tutorial02 import router as items_router
app = FastAPI()

0
docs/tutorial/src/all/tutorial057.py → docs/tutorial/src/security/tutorial001.py

0
docs/tutorial/src/all/tutorial058.py → docs/tutorial/src/security/tutorial002.py

0
docs/tutorial/src/all/tutorial059.py → docs/tutorial/src/security/tutorial003.py

0
docs/tutorial/src/all/tutorial060.py → docs/tutorial/src/security/tutorial004.py

12
mkdocs.yml

@ -16,8 +16,8 @@ edit_uri: ""
nav:
- Introduction: 'index.md'
- Features: 'features.md'
- Tutorial:
- Tutorial Intro: 'tutorial/intro.md'
- Tutorial - User Guide:
- Tutorial - User Guide - Intro: 'tutorial/intro.md'
- First Steps: 'tutorial/first-steps.md'
- Path Parameters: 'tutorial/path-params.md'
- Query Parameters: 'tutorial/query-params.md'
@ -43,6 +43,14 @@ nav:
- Second Steps: 'tutorial/dependencies/second-steps.md'
- SQL (Relational) Databases: 'tutorial/sql-databases.md'
- NoSQL (Distributed / Big Data) Databases: 'tutorial/nosql-databases.md'
- Security:
- Security Intro: 'tutorial/security/intro.md'
- First Steps: 'tutorial/security/first-steps.md'
- Simple OAuth2 with Password and Bearer: 'tutorial/security/simple-oauth2.md'
- OAuth2 with Password (and hashing), Bearer with JWT tokens: 'tutorial/security/oauth2-jwt.md'
- Bigger Applications - Multiple Files: 'tutorial/bigger-applications.md'
- Application Configuration: 'tutorial/application-configuration.md'
- Concurrency and async / await: 'async.md'
- Deployment: 'deployment.md'

Loading…
Cancel
Save