]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:truck: Re-order tutorial Python fiels
authorSebastián Ramírez <tiangolo@gmail.com>
Fri, 14 Dec 2018 06:07:07 +0000 (10:07 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Fri, 14 Dec 2018 06:07:07 +0000 (10:07 +0400)
35 files changed:
docs/tutorial/src/tutorial34.py [new file with mode: 0644]
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.py
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
docs/tutorial/src/tutorial59.py
docs/tutorial/src/tutorial60.py
docs/tutorial/src/tutorial61.py
docs/tutorial/src/tutorial62.py
docs/tutorial/src/tutorial63.py
docs/tutorial/src/tutorial64.py
docs/tutorial/src/tutorial65.py
docs/tutorial/src/tutorial66.py
docs/tutorial/src/tutorial67.py
docs/tutorial/src/tutorial68.py

diff --git a/docs/tutorial/src/tutorial34.py b/docs/tutorial/src/tutorial34.py
new file mode 100644 (file)
index 0000000..6c8b101
--- /dev/null
@@ -0,0 +1,17 @@
+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
index 9e0fa4494e8cbcef8e5ea695197769d4b2e3591f..3f4f53265dff6c2606e706224d9d8049285b3b51 100644 (file)
@@ -1,5 +1,5 @@
-from fastapi import FastAPI
-from pydantic import BaseModel
+from fastapi import Body, FastAPI
+from pydantic import BaseModel, Schema
 
 app = FastAPI()
 
@@ -9,10 +9,21 @@ class Item(BaseModel):
     description: str = None
     price: float
     tax: float = None
-    tags: list = []
 
 
 @app.put("/items/{item_id}")
-async def update_item(*, item_id: int, item: Item):
+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
index 8f769279b1fc49601e8581a18cc13a68bc71e120..9e0fa4494e8cbcef8e5ea695197769d4b2e3591f 100644 (file)
@@ -1,5 +1,3 @@
-from typing import List
-
 from fastapi import FastAPI
 from pydantic import BaseModel
 
@@ -11,7 +9,7 @@ class Item(BaseModel):
     description: str = None
     price: float
     tax: float = None
-    tags: List[str] = []
+    tags: list = []
 
 
 @app.put("/items/{item_id}")
index 291b3c64dc533abdd42609867bff95c94f38fb47..8f769279b1fc49601e8581a18cc13a68bc71e120 100644 (file)
@@ -1,4 +1,4 @@
-from typing import Set
+from typing import List
 
 from fastapi import FastAPI
 from pydantic import BaseModel
@@ -11,7 +11,7 @@ class Item(BaseModel):
     description: str = None
     price: float
     tax: float = None
-    tags: Set[str] = []
+    tags: List[str] = []
 
 
 @app.put("/items/{item_id}")
index 257928ef39b3783388ea591f6c93b1d12521ec06..291b3c64dc533abdd42609867bff95c94f38fb47 100644 (file)
@@ -6,18 +6,12 @@ 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}")
index f5f19b39046f4b32a0d10ecd09ffab858c96bbbb..257928ef39b3783388ea591f6c93b1d12521ec06 100644 (file)
@@ -2,13 +2,12 @@ from typing import Set
 
 from fastapi import FastAPI
 from pydantic import BaseModel
-from pydantic.types import UrlStr
 
 app = FastAPI()
 
 
 class Image(BaseModel):
-    url: UrlStr
+    url: str
     name: str
 
 
index 09d8be768ada6fcf7e7d89b7581edb478a7d5ca5..f5f19b39046f4b32a0d10ecd09ffab858c96bbbb 100644 (file)
@@ -1,4 +1,4 @@
-from typing import List, Set
+from typing import Set
 
 from fastapi import FastAPI
 from pydantic import BaseModel
@@ -18,7 +18,7 @@ class Item(BaseModel):
     price: float
     tax: float = None
     tags: Set[str] = []
-    image: List[Image] = None
+    image: Image = None
 
 
 @app.put("/items/{item_id}")
index cda802d3e35ddf1d265d7a7187672e9af5c61062..09d8be768ada6fcf7e7d89b7581edb478a7d5ca5 100644 (file)
@@ -21,13 +21,7 @@ class Item(BaseModel):
     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
+@app.put("/items/{item_id}")
+async def update_item(*, item_id: int, item: Item):
+    results = {"item_id": item_id, "item": item}
+    return results
index 34b868563bd904e5dc906849748389576cf69855..cda802d3e35ddf1d265d7a7187672e9af5c61062 100644 (file)
@@ -1,4 +1,4 @@
-from typing import List
+from typing import List, Set
 
 from fastapi import FastAPI
 from pydantic import BaseModel
@@ -12,6 +12,22 @@ 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: 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 5a6fd30fe60d266ac9feda44a5b469c1ba89ca1a..34b868563bd904e5dc906849748389576cf69855 100644 (file)
@@ -1,8 +1,17 @@
-from fastapi import Cookie, FastAPI
+from typing import List
+
+from fastapi import FastAPI
+from pydantic import BaseModel
+from pydantic.types import UrlStr
 
 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
+
+
+@app.post("/images/multiple/")
+async def create_multiple_images(*, images: List[Image]):
+    return images
index 24a59e530adfd2069c5959542e8ec3bfc18dc46d..5a6fd30fe60d266ac9feda44a5b469c1ba89ca1a 100644 (file)
@@ -1,8 +1,8 @@
-from fastapi import FastAPI, Header
+from fastapi import Cookie, FastAPI
 
 app = FastAPI()
 
 
 @app.get("/items/")
-async def read_items(*, accept_encoding: str = Header(None)):
-    return {"Accept-Encoding": accept_encoding}
+async def read_items(*, ads_id: str = Cookie(None)):
+    return {"ads_id": ads_id}
index 4edc4b6fda6433f2cc3733279838a2ad9b77a3da..24a59e530adfd2069c5959542e8ec3bfc18dc46d 100644 (file)
@@ -4,5 +4,5 @@ app = FastAPI()
 
 
 @app.get("/items/")
-async def read_items(*, strange_header: str = Header(None, convert_underscores=False)):
-    return {"strange_header": strange_header}
+async def read_items(*, accept_encoding: str = Header(None)):
+    return {"Accept-Encoding": accept_encoding}
index 86dadcbda4c67ee8f35ac981e1516ee27598442e..4edc4b6fda6433f2cc3733279838a2ad9b77a3da 100644 (file)
@@ -1,19 +1,8 @@
-from typing import Set
-
-from fastapi import FastAPI
-from pydantic import BaseModel
+from fastapi import FastAPI, Header
 
 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(*, strange_header: str = Header(None, convert_underscores=False)):
+    return {"strange_header": strange_header}
index 3fb475b9d9cc74862f2f14756156497aa6fd5662..86dadcbda4c67ee8f35ac981e1516ee27598442e 100644 (file)
@@ -1,18 +1,19 @@
+from typing import Set
+
 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 Item(BaseModel):
+    name: str
+    description: str = None
+    price: float
+    tax: float = None
+    tags: Set[str] = []
 
 
-# Don't do this in production!
-@app.post("/user/", response_model=UserIn)
-async def create_user(*, user: UserIn):
-    return user
+@app.post("/items/", response_model=Item)
+async def create_item(*, item: Item):
+    return item
index c8ea361d8d45bc197dbbce97acb6a9f32eb52f5d..3fb475b9d9cc74862f2f14756156497aa6fd5662 100644 (file)
@@ -12,12 +12,7 @@ class UserIn(BaseModel):
     full_name: str = None
 
 
-class UserOut(BaseModel):
-    username: str
-    email: EmailStr
-    full_name: str = None
-
-
-@app.post("/user/", response_model=UserOut)
+# Don't do this in production!
+@app.post("/user/", response_model=UserIn)
 async def create_user(*, user: UserIn):
     return user
index aa8e7dad456b1b7cf7ae944a955f647af6c551a7..c8ea361d8d45bc197dbbce97acb6a9f32eb52f5d 100644 (file)
@@ -18,25 +18,6 @@ class UserOut(BaseModel):
     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
+async def create_user(*, user: UserIn):
+    return user
index 605baf91f05e95c5763730b6a5983edd036847cf..aa8e7dad456b1b7cf7ae944a955f647af6c551a7 100644 (file)
@@ -5,22 +5,24 @@ from pydantic.types import EmailStr
 app = FastAPI()
 
 
-class UserBase(BaseModel):
+class UserIn(BaseModel):
     username: str
+    password: str
     email: EmailStr
     full_name: str = None
 
 
-class UserIn(UserBase):
-    password: str
-
-
-class UserOut(UserBase):
-    pass
+class UserOut(BaseModel):
+    username: str
+    email: EmailStr
+    full_name: str = None
 
 
-class UserInDB(UserBase):
+class UserInDB(BaseModel):
+    username: str
     hashed_password: str
+    email: EmailStr
+    full_name: str = None
 
 
 def fake_password_hasher(raw_password: str):
index 0290b644d374739bc668f7a1f24b99ffcc4fcfe2..605baf91f05e95c5763730b6a5983edd036847cf 100644 (file)
@@ -1,8 +1,40 @@
-from fastapi import FastAPI, Form
+from fastapi import FastAPI
+from pydantic import BaseModel
+from pydantic.types import EmailStr
 
 app = FastAPI()
 
 
-@app.post("/login/")
-async def login(*, username: str = Form(...), password: str = Form(...)):
-    return {"username": username}
+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
index 3e99fcdde799d25f2d35590ae02105ff87991bfb..0290b644d374739bc668f7a1f24b99ffcc4fcfe2 100644 (file)
@@ -1,8 +1,8 @@
-from fastapi import FastAPI, File
+from fastapi import FastAPI, Form
 
 app = FastAPI()
 
 
-@app.post("/files/")
-async def create_file(*, file: bytes = File(...)):
-    return {"file_size": len(file)}
+@app.post("/login/")
+async def login(*, username: str = Form(...), password: str = Form(...)):
+    return {"username": username}
index 1882a6397a9542ee181e31119b3c55f33de3237c..3e99fcdde799d25f2d35590ae02105ff87991bfb 100644 (file)
@@ -1,8 +1,8 @@
-from fastapi import FastAPI, File, Form
+from fastapi import FastAPI, File
 
 app = FastAPI()
 
 
 @app.post("/files/")
-async def create_file(*, file: bytes = File(...), token: str = Form(...)):
-    return {"file_size": len(file), "token": token}
+async def create_file(*, file: bytes = File(...)):
+    return {"file_size": len(file)}
index b486018672ada8c2c170f662c452bc7c23e64e0b..1882a6397a9542ee181e31119b3c55f33de3237c 100644 (file)
@@ -1,20 +1,8 @@
-from typing import Set
-
-from fastapi import FastAPI
-from pydantic import BaseModel
-from starlette.status import HTTP_201_CREATED
+from fastapi import FastAPI, File, Form
 
 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
+@app.post("/files/")
+async def create_file(*, file: bytes = File(...), token: str = Form(...)):
+    return {"file_size": len(file), "token": token}
index ccd5437da8a2e204e1bc9b6aa6a655f3c65da219..b486018672ada8c2c170f662c452bc7c23e64e0b 100644 (file)
@@ -2,6 +2,7 @@ from typing import Set
 
 from fastapi import FastAPI
 from pydantic import BaseModel
+from starlette.status import HTTP_201_CREATED
 
 app = FastAPI()
 
@@ -14,6 +15,6 @@ class Item(BaseModel):
     tags: Set[str] = []
 
 
-@app.post("/items/", response_model=Item, tags=["items"])
+@app.post("/items/", response_model=Item, status_code=HTTP_201_CREATED)
 async def create_item(*, item: Item):
     return item
index 106607fd2d3ace29c56fb0f493d90806684b62ff..ccd5437da8a2e204e1bc9b6aa6a655f3c65da219 100644 (file)
@@ -14,11 +14,6 @@ class Item(BaseModel):
     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",
-)
+@app.post("/items/", response_model=Item, tags=["items"])
 async def create_item(*, item: Item):
     return item
index a4151a8cd0422c322d472fd11c5b5b85945427aa..106607fd2d3ace29c56fb0f493d90806684b62ff 100644 (file)
@@ -14,15 +14,11 @@ class Item(BaseModel):
     tags: Set[str] = []
 
 
-@app.post("/items/", response_model=Item, summary="Create an item")
+@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):
-    """
-    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 f710e6c669d322132e2dcb4843a6941ea5ad37e6..a4151a8cd0422c322d472fd11c5b5b85945427aa 100644 (file)
@@ -14,12 +14,7 @@ class Item(BaseModel):
     tags: Set[str] = []
 
 
-@app.post(
-    "/items/",
-    response_model=Item,
-    summary="Create an item",
-    response_description="The created item",
-)
+@app.post("/items/", response_model=Item, summary="Create an item")
 async def create_item(*, item: Item):
     """
     Create an item with all the information:
index 11f3de6dbee7ed3f07e1f076d98b00a4e5e30868..f710e6c669d322132e2dcb4843a6941ea5ad37e6 100644 (file)
@@ -1,8 +1,33 @@
+from typing import Set
+
 from fastapi import FastAPI
+from pydantic import BaseModel
 
 app = FastAPI()
 
 
-@app.get("/items/", deprecated=True)
-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",
+    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 fafa8ffb8ed05cc867d641170349699d86711fca..11f3de6dbee7ed3f07e1f076d98b00a4e5e30868 100644 (file)
@@ -3,6 +3,6 @@ from fastapi import FastAPI
 app = FastAPI()
 
 
-@app.get("/items/", operation_id="some_specific_id_you_define")
+@app.get("/items/", deprecated=True)
 async def read_items():
     return [{"item_id": "Foo"}]
index dcc358e323d4da561d37ecaac25b7775ee87c28f..fafa8ffb8ed05cc867d641170349699d86711fca 100644 (file)
@@ -3,6 +3,6 @@ from fastapi import FastAPI
 app = FastAPI()
 
 
-@app.get("/items/", include_in_schema=False)
+@app.get("/items/", operation_id="some_specific_id_you_define")
 async def read_items():
     return [{"item_id": "Foo"}]
index bba3f342d6688e5206ad4620b8f9658e336863b5..dcc358e323d4da561d37ecaac25b7775ee87c28f 100644 (file)
@@ -1,9 +1,8 @@
 from fastapi import FastAPI
-from starlette.responses import UJSONResponse
 
 app = FastAPI()
 
 
-@app.get("/items/", content_type=UJSONResponse)
+@app.get("/items/", include_in_schema=False)
 async def read_items():
     return [{"item_id": "Foo"}]
index 214e6426365b3b4e0fc23b5267eee673028c17f0..bba3f342d6688e5206ad4620b8f9658e336863b5 100644 (file)
@@ -1,18 +1,9 @@
 from fastapi import FastAPI
-from starlette.responses import HTMLResponse
+from starlette.responses import UJSONResponse
 
 app = FastAPI()
 
 
-@app.get("/items/", content_type=HTMLResponse)
+@app.get("/items/", content_type=UJSONResponse)
 async def read_items():
-    return """
-    <html>
-        <head>
-            <title>Some HTML in here</title>
-        </head>
-        <body>
-            <h1>Look ma! HTML!</h1>
-        </body>
-    </html>
-    """
+    return [{"item_id": "Foo"}]
index 82a51634eb1e38a719fd16688921d1f374049497..214e6426365b3b4e0fc23b5267eee673028c17f0 100644 (file)
@@ -1,27 +1,18 @@
-from fastapi import Depends, FastAPI
-from pydantic import BaseModel
+from fastapi import FastAPI
+from starlette.responses import HTMLResponse
 
 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
+@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>
+    """
index e015f958528a93423739c73c0cb31dd1788bc393..82a51634eb1e38a719fd16688921d1f374049497 100644 (file)
@@ -1,34 +1,27 @@
-from typing import List
-
-from fastapi import Cookie, Depends, FastAPI
+from fastapi import Depends, FastAPI
 from pydantic import BaseModel
 
 app = FastAPI()
 
 
-class InterestsTracker(BaseModel):
-    track_code: str
-    interests: List[str]
+fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
 
 
-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"]},
-}
+class CommonQueryParams(BaseModel):
+    q: str = None
+    skip: int = None
+    limit: int = None
 
 
-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
+async def common_parameters(q: str = None, skip: int = 0, limit: int = 100):
+    return CommonQueryParams(q=q, skip=skip, limit=limit)
 
 
-@app.get("/interests/")
-async def read_interests(
-    tracked_interests: InterestsTracker = Depends(get_tracked_interests)
-):
-    response = {"interests": tracked_interests.interests}
+@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
index 3697b170ae23fe9ae242eb9f54d2dacf7fca79e8..e015f958528a93423739c73c0cb31dd1788bc393 100644 (file)
@@ -1,4 +1,3 @@
-from random import choice
 from typing import List
 
 from fastapi import Cookie, Depends, FastAPI
@@ -27,23 +26,9 @@ async def get_tracked_interests(track_code: str = Cookie(None)):
     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()}
+@app.get("/interests/")
+async def read_interests(
+    tracked_interests: InterestsTracker = Depends(get_tracked_interests)
+):
+    response = {"interests": tracked_interests.interests}
     return response
index 720604c0247f7a9d540f4499025205bee1d9b939..3697b170ae23fe9ae242eb9f54d2dacf7fca79e8 100644 (file)
@@ -1,52 +1,49 @@
-from fastapi import FastAPI
+from random import choice
+from typing import List
 
-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()
+from fastapi import Cookie, Depends, FastAPI
+from pydantic import BaseModel
 
+app = FastAPI()
 
-Base = declarative_base(cls=CustomBase)
 
+class InterestsTracker(BaseModel):
+    track_code: str
+    interests: List[str]
 
-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)
 
+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"]},
+}
 
-engine = create_engine(SQLALCHEMY_DATABASE_URI, convert_unicode=True)
-db_session = scoped_session(
-    sessionmaker(autocommit=False, autoflush=False, bind=engine)
-)
 
+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
 
-def get_user(username, db_session):
-    return db_session.query(User).filter(User.id == username).first()
 
+class ComplexTracker:
+    def __init__(self, tracker: InterestsTracker = Depends(get_tracked_interests)):
+        self.tracker = tracker
 
-# FastAPI specific code
-app = FastAPI()
+    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("/users/{username}")
-def read_user(username: str):
-    user = get_user(username, db_session)
-    return user
+@app.get("/suggested-category")
+async def read_suggested_category(tracker: ComplexTracker = Depends(None)):
+    response = {"category": tracker.random_interest()}
+    return response
index 26897e1c9c08bf6e1ff209c317a77c7fbf79687c..720604c0247f7a9d540f4499025205bee1d9b939 100644 (file)
@@ -1,49 +1,45 @@
-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
+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
 
-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
 
+SQLALCHEMY_DATABASE_URI = "postgresql://user:password@postgresserver/db"
 
-class User(BaseModel):
-    username: str
-    email: Optional[str] = None
-    full_name: Optional[str] = None
-    disabled: Optional[bool] = None
+# 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 UserInDB(User):
-    type: str = USERPROFILE_DOC_TYPE
-    hashed_password: str
+class CustomBase:
+    # Generate __tablename__ automatically
+    @declared_attr
+    def __tablename__(cls):
+        return cls.__name__.lower()
 
-    class Meta:
-        key: Optional[str] = None
 
+Base = declarative_base(cls=CustomBase)
 
-def get_user_doc_id(username):
-    return f"userprofile::{username}"
 
+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)
 
-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
+
+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
@@ -52,6 +48,5 @@ app = FastAPI()
 
 @app.get("/users/{username}")
 def read_user(username: str):
-    bucket = get_bucket()
-    user = get_user(bucket=bucket, username=username)
+    user = get_user(username, db_session)
     return user