🏗 👩💻¶
FastAPI ⚓️ 🔛 🗄 🔧, 👆 🤚 🏧 🔗 ⏮️ 📚 🧰, 🔌 🏧 🛠️ 🩺 (🚚 🦁 🎚).
1️⃣ 🎯 📈 👈 🚫 🎯 ⭐ 👈 👆 💪 🏗 👩💻 (🕣 🤙 📱 ) 👆 🛠️, 📚 🎏 🛠️ 🇪🇸.
🗄 👩💻 🚂¶
📤 📚 🧰 🏗 👩💻 ⚪️➡️ 🗄.
⚠ 🧰 🗄 🚂.
🚥 👆 🏗 🕸, 📶 😌 🎛 🗄-📕-🇦🇪.
🏗 📕 🕸 👩💻¶
➡️ ▶️ ⏮️ 🙅 FastAPI 🈸:
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
class ResponseMessage(BaseModel):
message: str
@app.post("/items/", response_model=ResponseMessage)
async def create_item(item: Item):
return {"message": "item received"}
@app.get("/items/", response_model=List[Item])
async def get_items():
return [
{"name": "Plumbus", "price": 3},
{"name": "Portal Gun", "price": 9001},
]
🤓 Other versions and variants
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
class ResponseMessage(BaseModel):
message: str
@app.post("/items/", response_model=ResponseMessage)
async def create_item(item: Item):
return {"message": "item received"}
@app.get("/items/", response_model=list[Item])
async def get_items():
return [
{"name": "Plumbus", "price": 3},
{"name": "Portal Gun", "price": 9001},
]
👀 👈 ➡ 🛠️ 🔬 🏷 👫 ⚙️ 📨 🚀 & 📨 🚀, ⚙️ 🏷 Item
& ResponseMessage
.
🛠️ 🩺¶
🚥 👆 🚶 🛠️ 🩺, 👆 🔜 👀 👈 ⚫️ ✔️ 🔗 📊 📨 📨 & 📨 📨:
👆 💪 👀 👈 🔗 ↩️ 👫 📣 ⏮️ 🏷 📱.
👈 ℹ 💪 📱 🗄 🔗, & ⤴️ 🎦 🛠️ 🩺 (🦁 🎚).
& 👈 🎏 ℹ ⚪️➡️ 🏷 👈 🔌 🗄 ⚫️❔ 💪 ⚙️ 🏗 👩💻 📟.
🏗 📕 👩💻¶
🔜 👈 👥 ✔️ 📱 ⏮️ 🏷, 👥 💪 🏗 👩💻 📟 🕸.
❎ openapi-ts
¶
👆 💪 ❎ openapi-ts
👆 🕸 📟 ⏮️:
$ npm install @hey-api/openapi-ts --save-dev
---> 100%
🏗 👩💻 📟¶
🏗 👩💻 📟 👆 💪 ⚙️ 📋 ⏸ 🈸 openapi-ts
👈 🔜 🔜 ❎.
↩️ ⚫️ ❎ 🇧🇿 🏗, 👆 🎲 🚫🔜 💪 🤙 👈 📋 🔗, ✋️ 👆 🔜 🚮 ⚫️ 🔛 👆 package.json
📁.
⚫️ 💪 👀 💖 👉:
{
"name": "frontend-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"generate-client": "openapi-ts --input http://localhost:8000/openapi.json --output ./src/client --client axios"
},
"author": "",
"license": "",
"devDependencies": {
"@hey-api/openapi-ts": "^0.27.38",
"typescript": "^4.6.2"
}
}
⏮️ ✔️ 👈 ☕ generate-client
✍ 📤, 👆 💪 🏃 ⚫️ ⏮️:
$ npm run generate-client
frontend-app@1.0.0 generate-client /home/user/code/frontend-app
> openapi-ts --input http://localhost:8000/openapi.json --output ./src/client --client axios
👈 📋 🔜 🏗 📟 ./src/client
& 🔜 ⚙️ axios
(🕸 🇺🇸🔍 🗃) 🔘.
🔄 👅 👩💻 📟¶
🔜 👆 💪 🗄 & ⚙️ 👩💻 📟, ⚫️ 💪 👀 💖 👉, 👀 👈 👆 🤚 ✍ 👩🔬:
👆 🔜 🤚 ✍ 🚀 📨:
Tip
👀 ✍ name
& price
, 👈 🔬 FastAPI 🈸, Item
🏷.
👆 🔜 ✔️ ⏸ ❌ 📊 👈 👆 📨:
📨 🎚 🔜 ✔️ ✍:
FastAPI 📱 ⏮️ 🔖¶
📚 💼 👆 FastAPI 📱 🔜 🦏, & 👆 🔜 🎲 ⚙️ 🔖 🎏 🎏 👪 ➡ 🛠️.
🖼, 👆 💪 ✔️ 📄 🏬 & ➕1️⃣ 📄 👩💻, & 👫 💪 👽 🔖:
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
class ResponseMessage(BaseModel):
message: str
class User(BaseModel):
username: str
email: str
@app.post("/items/", response_model=ResponseMessage, tags=["items"])
async def create_item(item: Item):
return {"message": "Item received"}
@app.get("/items/", response_model=List[Item], tags=["items"])
async def get_items():
return [
{"name": "Plumbus", "price": 3},
{"name": "Portal Gun", "price": 9001},
]
@app.post("/users/", response_model=ResponseMessage, tags=["users"])
async def create_user(user: User):
return {"message": "User received"}
🤓 Other versions and variants
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
class ResponseMessage(BaseModel):
message: str
class User(BaseModel):
username: str
email: str
@app.post("/items/", response_model=ResponseMessage, tags=["items"])
async def create_item(item: Item):
return {"message": "Item received"}
@app.get("/items/", response_model=list[Item], tags=["items"])
async def get_items():
return [
{"name": "Plumbus", "price": 3},
{"name": "Portal Gun", "price": 9001},
]
@app.post("/users/", response_model=ResponseMessage, tags=["users"])
async def create_user(user: User):
return {"message": "User received"}
🏗 📕 👩💻 ⏮️ 🔖¶
🚥 👆 🏗 👩💻 FastAPI 📱 ⚙️ 🔖, ⚫️ 🔜 🛎 🎏 👩💻 📟 ⚓️ 🔛 🔖.
👉 🌌 👆 🔜 💪 ✔️ 👜 ✔ & 👪 ☑ 👩💻 📟:
👉 💼 👆 ✔️:
ItemsService
UsersService
👩💻 👩🔬 📛¶
▶️️ 🔜 🏗 👩🔬 📛 💖 createItemItemsPost
🚫 👀 📶 🧹:
ItemsService.createItemItemsPost({name: "Plumbus", price: 5})
...👈 ↩️ 👩💻 🚂 ⚙️ 🗄 🔗 🛠️ 🆔 🔠 ➡ 🛠️.
🗄 🚚 👈 🔠 🛠️ 🆔 😍 🤭 🌐 ➡ 🛠️, FastAPI ⚙️ 🔢 📛, ➡, & 🇺🇸🔍 👩🔬/🛠️ 🏗 👈 🛠️ 🆔, ↩️ 👈 🌌 ⚫️ 💪 ⚒ 💭 👈 🛠️ 🆔 😍.
✋️ 👤 🔜 🎦 👆 ❔ 📉 👈 ⏭. 👶
🛃 🛠️ 🆔 & 👍 👩🔬 📛¶
👆 💪 🔀 🌌 👫 🛠️ 🆔 🏗 ⚒ 👫 🙅 & ✔️ 🙅 👩🔬 📛 👩💻.
👉 💼 👆 🔜 ✔️ 🚚 👈 🔠 🛠️ 🆔 😍 🎏 🌌.
🖼, 👆 💪 ⚒ 💭 👈 🔠 ➡ 🛠️ ✔️ 🔖, & ⤴️ 🏗 🛠️ 🆔 ⚓️ 🔛 🔖 & ➡ 🛠️ 📛 (🔢 📛).
🛃 🏗 😍 🆔 🔢¶
FastAPI ⚙️ 😍 🆔 🔠 ➡ 🛠️, ⚫️ ⚙️ 🛠️ 🆔 & 📛 🙆 💪 🛃 🏷, 📨 ⚖️ 📨.
👆 💪 🛃 👈 🔢. ⚫️ ✊ APIRoute
& 🔢 🎻.
🖼, 📥 ⚫️ ⚙️ 🥇 🔖 (👆 🔜 🎲 ✔️ 🕴 1️⃣ 🔖) & ➡ 🛠️ 📛 (🔢 📛).
👆 💪 ⤴️ 🚶♀️ 👈 🛃 🔢 FastAPI generate_unique_id_function
🔢:
from typing import List
from fastapi import FastAPI
from fastapi.routing import APIRoute
from pydantic import BaseModel
def custom_generate_unique_id(route: APIRoute):
return f"{route.tags[0]}-{route.name}"
app = FastAPI(generate_unique_id_function=custom_generate_unique_id)
class Item(BaseModel):
name: str
price: float
class ResponseMessage(BaseModel):
message: str
class User(BaseModel):
username: str
email: str
@app.post("/items/", response_model=ResponseMessage, tags=["items"])
async def create_item(item: Item):
return {"message": "Item received"}
@app.get("/items/", response_model=List[Item], tags=["items"])
async def get_items():
return [
{"name": "Plumbus", "price": 3},
{"name": "Portal Gun", "price": 9001},
]
@app.post("/users/", response_model=ResponseMessage, tags=["users"])
async def create_user(user: User):
return {"message": "User received"}
🤓 Other versions and variants
from fastapi import FastAPI
from fastapi.routing import APIRoute
from pydantic import BaseModel
def custom_generate_unique_id(route: APIRoute):
return f"{route.tags[0]}-{route.name}"
app = FastAPI(generate_unique_id_function=custom_generate_unique_id)
class Item(BaseModel):
name: str
price: float
class ResponseMessage(BaseModel):
message: str
class User(BaseModel):
username: str
email: str
@app.post("/items/", response_model=ResponseMessage, tags=["items"])
async def create_item(item: Item):
return {"message": "Item received"}
@app.get("/items/", response_model=list[Item], tags=["items"])
async def get_items():
return [
{"name": "Plumbus", "price": 3},
{"name": "Portal Gun", "price": 9001},
]
@app.post("/users/", response_model=ResponseMessage, tags=["users"])
async def create_user(user: User):
return {"message": "User received"}
🏗 📕 👩💻 ⏮️ 🛃 🛠️ 🆔¶
🔜 🚥 👆 🏗 👩💻 🔄, 👆 🔜 👀 👈 ⚫️ ✔️ 📉 👩🔬 📛:
👆 👀, 👩🔬 📛 🔜 ✔️ 🔖 & ⤴️ 🔢 📛, 🔜 👫 🚫 🔌 ℹ ⚪️➡️ 📛 ➡ & 🇺🇸🔍 🛠️.
🗜 🗄 🔧 👩💻 🚂¶
🏗 📟 ✔️ ❎ ℹ.
👥 ⏪ 💭 👈 👉 👩🔬 🔗 🏬 ↩️ 👈 🔤 ItemsService
(✊ ⚪️➡️ 🔖), ✋️ 👥 ✔️ 📛 🔡 👩🔬 📛 💁♂️. 👶
👥 🔜 🎲 💚 🚧 ⚫️ 🗄 🏢, 👈 🔜 🚚 👈 🛠️ 🆔 😍.
✋️ 🏗 👩💻 👥 💪 🔀 🗄 🛠️ 🆔 ▶️️ ⏭ 🏭 👩💻, ⚒ 👈 👩🔬 📛 👌 & 🧹.
👥 💪 ⏬ 🗄 🎻 📁 openapi.json
& ⤴️ 👥 💪 ❎ 👈 🔡 🔖 ⏮️ ✍ 💖 👉:
import json
from pathlib import Path
file_path = Path("./openapi.json")
openapi_content = json.loads(file_path.read_text())
for path_data in openapi_content["paths"].values():
for operation in path_data.values():
tag = operation["tags"][0]
operation_id = operation["operationId"]
to_remove = f"{tag}-"
new_operation_id = operation_id[len(to_remove) :]
operation["operationId"] = new_operation_id
file_path.write_text(json.dumps(openapi_content))
⏮️ 👈, 🛠️ 🆔 🔜 📁 ⚪️➡️ 👜 💖 items-get_items
get_items
, 👈 🌌 👩💻 🚂 💪 🏗 🙅 👩🔬 📛.
🏗 📕 👩💻 ⏮️ 🗜 🗄¶
🔜 🔚 🏁 📁 openapi.json
, 👆 🔜 🔀 package.json
⚙️ 👈 🇧🇿 📁, 🖼:
{
"name": "frontend-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"generate-client": "openapi-ts --input ./openapi.json --output ./src/client --client axios"
},
"author": "",
"license": "",
"devDependencies": {
"@hey-api/openapi-ts": "^0.27.38",
"typescript": "^4.6.2"
}
}
⏮️ 🏭 🆕 👩💻, 👆 🔜 🔜 ✔️ 🧹 👩🔬 📛, ⏮️ 🌐 ✍, ⏸ ❌, ♒️:
💰¶
🕐❔ ⚙️ 🔁 🏗 👩💻 👆 🔜 ✍ :
- 👩🔬.
- 📨 🚀 💪, 🔢 🔢, ♒️.
- 📨 🚀.
👆 🔜 ✔️ ⏸ ❌ 🌐.
& 🕐❔ 👆 ℹ 👩💻 📟, & ♻ 🕸, ⚫️ 🔜 ✔️ 🙆 🆕 ➡ 🛠️ 💪 👩🔬, 🗝 🕐 ❎, & 🙆 🎏 🔀 🔜 🎨 🔛 🏗 📟. 👶
👉 ⛓ 👈 🚥 🕳 🔀 ⚫️ 🔜 🎨 🔛 👩💻 📟 🔁. & 🚥 👆 🏗 👩💻 ⚫️ 🔜 ❌ 👅 🚥 👆 ✔️ 🙆 🔖 📊 ⚙️.
, 👆 🔜 🔍 📚 ❌ 📶 ⏪ 🛠️ 🛵 ↩️ ✔️ ⌛ ❌ 🎦 🆙 👆 🏁 👩💻 🏭 & ⤴️ 🔄 ℹ 🌐❔ ⚠. 👶