|
| 1 | +from typing import Optional, Union, List, Dict |
| 2 | + |
| 3 | +from fastapi import FastAPI |
| 4 | +from pydantic import BaseModel, EmailStr |
| 5 | + |
| 6 | + |
| 7 | +app = FastAPI() |
| 8 | + |
| 9 | + |
| 10 | +# class UserIn(BaseModel): |
| 11 | +# username: str |
| 12 | +# password: str |
| 13 | +# email: EmailStr |
| 14 | +# full_name: Optional[str] = None |
| 15 | +# |
| 16 | +# |
| 17 | +# class UserOut(BaseModel): |
| 18 | +# username: str |
| 19 | +# email: EmailStr |
| 20 | +# full_name: Optional[str] = None |
| 21 | +# |
| 22 | +# |
| 23 | +# class UserInDB(BaseModel): |
| 24 | +# username: str |
| 25 | +# hashed_password: str |
| 26 | +# email: EmailStr |
| 27 | +# full_name: Optional[str] = None |
| 28 | + |
| 29 | + |
| 30 | +class UserBase(BaseModel): |
| 31 | + username: str |
| 32 | + email: EmailStr |
| 33 | + full_name: Optional[str] = None |
| 34 | + |
| 35 | + |
| 36 | +class UserIn(UserBase): |
| 37 | + password: str |
| 38 | + |
| 39 | + |
| 40 | +class UserOut(UserBase): |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +class UserInDB(UserBase): |
| 45 | + hashed_password: str |
| 46 | + |
| 47 | + |
| 48 | +class BaseItem(BaseModel): |
| 49 | + description: str |
| 50 | + type: str |
| 51 | + |
| 52 | + |
| 53 | +class CarItem(BaseItem): |
| 54 | + type = 'car' |
| 55 | + |
| 56 | + |
| 57 | +class PlaneItem(BaseItem): |
| 58 | + type = 'plane' |
| 59 | + size: int |
| 60 | + |
| 61 | + |
| 62 | +items = { |
| 63 | + "item1": {"description": "All my friends drive a low rider", "type": "car"}, |
| 64 | + "item2": { |
| 65 | + "description": "Music is my aeroplane, it's my aeroplane", |
| 66 | + "type": "plane", |
| 67 | + "size": 5, |
| 68 | + }, |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +class Item(BaseModel): |
| 73 | + name: str |
| 74 | + description: str |
| 75 | + |
| 76 | + |
| 77 | +items2 = [ |
| 78 | + {"name": "Foo", "description": "There comes my hero"}, |
| 79 | + {"name": "Red", "description": "It's my aeroplane"}, |
| 80 | +] |
| 81 | + |
| 82 | + |
| 83 | +def fake_password_hasher(raw_password: str): |
| 84 | + return 'supersecret' + raw_password |
| 85 | + |
| 86 | + |
| 87 | +def fake_save_user(user_in: UserIn): |
| 88 | + hashed_password = fake_password_hasher(user_in.password) |
| 89 | + user_in_db = UserInDB(**user_in.dict(), hashed_password=hashed_password) |
| 90 | + print('User saved! ..not really') |
| 91 | + return user_in_db |
| 92 | + |
| 93 | + |
| 94 | +@app.post('/user/', response_model=UserOut) |
| 95 | +async def create_user(user_in: UserIn): |
| 96 | + user_saved = fake_save_user(user_in) |
| 97 | + return user_saved |
| 98 | + |
| 99 | + |
| 100 | +@app.get('/items/{item_id}', response_model=Union[PlaneItem, CarItem]) |
| 101 | +async def read_item(item_id: str): |
| 102 | + return items[item_id] |
| 103 | + |
| 104 | + |
| 105 | +@app.get('/items/', response_model=List[Item]) |
| 106 | +async def read_items(): |
| 107 | + return items2 |
| 108 | + |
| 109 | + |
| 110 | +@app.get('/keyword-weights/', response_model=Dict[str, float]) |
| 111 | +async def read_keyword_weights(): |
| 112 | + return {'foo': 2.3, 'bar': 4.5} |
| 113 | + |
| 114 | + |
| 115 | +# PYTHONPATH=module_fastapi uvicorn fastapi_extra_models:app --reload |
| 116 | + |
0 commit comments