6.6 KiB
🗄 ⏲
👆 💪 ✍ 🛠️ ⏮️ ➡ 🛠️ 👈 💪 ⏲ 📨 🔢 🛠️ ✍ 👱 🙆 (🎲 🎏 👩💻 👈 🔜 ⚙️ 👆 🛠️).
🛠️ 👈 🔨 🕐❔ 👆 🛠️ 📱 🤙 🔢 🛠️ 📛 "⏲". ↩️ 🖥 👈 🔢 👩💻 ✍ 📨 📨 👆 🛠️ & ⤴️ 👆 🛠️ 🤙 🔙, 📨 📨 🔢 🛠️ (👈 🎲 ✍ 🎏 👩💻).
👉 💼, 👆 💪 💚 📄 ❔ 👈 🔢 🛠️ 🔜 👀 💖. ⚫️❔ ➡ 🛠️ ⚫️ 🔜 ✔️, ⚫️❔ 💪 ⚫️ 🔜 ⌛, ⚫️❔ 📨 ⚫️ 🔜 📨, ♒️.
📱 ⏮️ ⏲
➡️ 👀 🌐 👉 ⏮️ 🖼.
🌈 👆 🛠️ 📱 👈 ✔ 🏗 🧾.
👉 🧾 🔜 ✔️ id
, title
(📦), customer
, & total
.
👩💻 👆 🛠️ (🔢 👩💻) 🔜 ✍ 🧾 👆 🛠️ ⏮️ 🏤 📨.
⤴️ 👆 🛠️ 🔜 (➡️ 🌈):
- 📨 🧾 🕴 🔢 👩💻.
- 📈 💸.
- 📨 📨 🔙 🛠️ 👩💻 (🔢 👩💻).
- 👉 🔜 🔨 📨 🏤 📨 (⚪️➡️ 👆 🛠️) 🔢 🛠️ 🚚 👈 🔢 👩💻 (👉 "⏲").
😐 FastAPI 📱
➡️ 🥇 👀 ❔ 😐 🛠️ 📱 🔜 👀 💖 ⏭ ❎ ⏲.
⚫️ 🔜 ✔️ ➡ 🛠️ 👈 🔜 📨 Invoice
💪, & 🔢 🔢 callback_url
👈 🔜 🔌 📛 ⏲.
👉 🍕 📶 😐, 🌅 📟 🎲 ⏪ 😰 👆:
{!../../../docs_src/openapi_callbacks/tutorial001.py!}
!!! tip
callback_url
🔢 🔢 ⚙️ Pydantic 📛 🆎.
🕴 🆕 👜 callbacks=messages_callback_router.routes
❌ ➡ 🛠️ 👨🎨. 👥 🔜 👀 ⚫️❔ 👈 ⏭.
🔬 ⏲
☑ ⏲ 📟 🔜 🪀 🙇 🔛 👆 👍 🛠️ 📱.
& ⚫️ 🔜 🎲 🪀 📚 ⚪️➡️ 1️⃣ 📱 ⏭.
⚫️ 💪 1️⃣ ⚖️ 2️⃣ ⏸ 📟, 💖:
callback_url = "https://example.com/api/v1/invoices/events/"
httpx.post(callback_url, json={"description": "Invoice paid", "paid": True})
✋️ 🎲 🏆 ⚠ 🍕 ⏲ ⚒ 💭 👈 👆 🛠️ 👩💻 (🔢 👩💻) 🛠️ 🔢 🛠️ ☑, 🛄 💽 👈 👆 🛠️ 🔜 📨 📨 💪 ⏲, ♒️.
, ⚫️❔ 👥 🔜 ⏭ 🚮 📟 📄 ❔ 👈 🔢 🛠️ 🔜 👀 💖 📨 ⏲ ⚪️➡️ 👆 🛠️.
👈 🧾 🔜 🎦 🆙 🦁 🎚 /docs
👆 🛠️, & ⚫️ 🔜 ➡️ 🔢 👩💻 💭 ❔ 🏗 🔢 🛠️.
👉 🖼 🚫 🛠️ ⏲ ⚫️ (👈 💪 ⏸ 📟), 🕴 🧾 🍕.
!!! tip ☑ ⏲ 🇺🇸🔍 📨.
🕐❔ 🛠️ ⏲ 👆, 👆 💪 ⚙️ 🕳 💖 <a href="https://www.python-httpx.org" class="external-link" target="_blank">🇸🇲</a> ⚖️ <a href="https://requests.readthedocs.io/" class="external-link" target="_blank">📨</a>.
✍ ⏲ 🧾 📟
👉 📟 🏆 🚫 🛠️ 👆 📱, 👥 🕴 💪 ⚫️ 📄 ❔ 👈 🔢 🛠️ 🔜 👀 💖.
✋️, 👆 ⏪ 💭 ❔ 💪 ✍ 🏧 🧾 🛠️ ⏮️ FastAPI.
👥 🔜 ⚙️ 👈 🎏 💡 📄 ❔ 🔢 🛠️ 🔜 👀 💖... 🏗 ➡ 🛠️(Ⓜ) 👈 🔢 🛠️ 🔜 🛠️ (🕐 👆 🛠️ 🔜 🤙).
!!! tip 🕐❔ ✍ 📟 📄 ⏲, ⚫️ 💪 ⚠ 🌈 👈 👆 👈 🔢 👩💻. & 👈 👆 ⏳ 🛠️ 🔢 🛠️, 🚫 👆 🛠️.
🍕 🛠️ 👉 ☝ 🎑 ( *🔢 👩💻*) 💪 ℹ 👆 💭 💖 ⚫️ 🌅 ⭐ 🌐❔ 🚮 🔢, Pydantic 🏷 💪, 📨, ♒️. 👈 *🔢 🛠️*.
✍ ⏲ APIRouter
🥇 ✍ 🆕 APIRouter
👈 🔜 🔌 1️⃣ ⚖️ 🌅 ⏲.
{!../../../docs_src/openapi_callbacks/tutorial001.py!}
✍ ⏲ ➡ 🛠️
✍ ⏲ ➡ 🛠️ ⚙️ 🎏 APIRouter
👆 ✍ 🔛.
⚫️ 🔜 👀 💖 😐 FastAPI ➡ 🛠️:
- ⚫️ 🔜 🎲 ✔️ 📄 💪 ⚫️ 🔜 📨, ✅
body: InvoiceEvent
. - & ⚫️ 💪 ✔️ 📄 📨 ⚫️ 🔜 📨, ✅
response_model=InvoiceEventReceived
.
{!../../../docs_src/openapi_callbacks/tutorial001.py!}
📤 2️⃣ 👑 🔺 ⚪️➡️ 😐 ➡ 🛠️:
- ⚫️ 🚫 💪 ✔️ 🙆 ☑ 📟, ↩️ 👆 📱 🔜 🙅 🤙 👉 📟. ⚫️ 🕴 ⚙️ 📄 🔢 🛠️. , 🔢 💪 ✔️
pass
. - ➡ 💪 🔌 🗄 3️⃣ 🧬 (👀 🌖 🔛) 🌐❔ ⚫️ 💪 ⚙️ 🔢 ⏮️ 🔢 & 🍕 ⏮️ 📨 📨 👆 🛠️.
⏲ ➡ 🧬
⏲ ➡ 💪 ✔️ 🗄 3️⃣ 🧬 👈 💪 🔌 🍕 ⏮️ 📨 📨 👆 🛠️.
👉 💼, ⚫️ str
:
"{$callback_url}/invoices/{$request.body.id}"
, 🚥 👆 🛠️ 👩💻 (🔢 👩💻) 📨 📨 👆 🛠️ :
https://yourapi.com/invoices/?callback_url=https://www.external.org/events
⏮️ 🎻 💪:
{
"id": "2expen51ve",
"customer": "Mr. Richie Rich",
"total": "9999"
}
⤴️ 👆 🛠️ 🔜 🛠️ 🧾, & ☝ ⏪, 📨 ⏲ 📨 callback_url
( 🔢 🛠️):
https://www.external.org/events/invoices/2expen51ve
⏮️ 🎻 💪 ⚗ 🕳 💖:
{
"description": "Payment celebration",
"paid": true
}
& ⚫️ 🔜 ⌛ 📨 ⚪️➡️ 👈 🔢 🛠️ ⏮️ 🎻 💪 💖:
{
"ok": true
}
!!! tip
👀 ❔ ⏲ 📛 ⚙️ 🔌 📛 📨 🔢 🔢 callback_url
(https://www.external.org/events
) & 🧾 id
⚪️➡️ 🔘 🎻 💪 (2expen51ve
).
🚮 ⏲ 📻
👉 ☝ 👆 ✔️ ⏲ ➡ 🛠️(Ⓜ) 💪 (1️⃣(Ⓜ) 👈 🔢 👩💻 🔜 🛠️ 🔢 🛠️) ⏲ 📻 👆 ✍ 🔛.
🔜 ⚙️ 🔢 callbacks
👆 🛠️ ➡ 🛠️ 👨🎨 🚶♀️ 🔢 .routes
(👈 🤙 list
🛣/➡ 🛠️) ⚪️➡️ 👈 ⏲ 📻:
{!../../../docs_src/openapi_callbacks/tutorial001.py!}
!!! tip
👀 👈 👆 🚫 🚶♀️ 📻 ⚫️ (invoices_callback_router
) callback=
, ✋️ 🔢 .routes
, invoices_callback_router.routes
.
✅ 🩺
🔜 👆 💪 ▶️ 👆 📱 ⏮️ Uvicorn & 🚶 http://127.0.0.1:8000/docs.
👆 🔜 👀 👆 🩺 ✅ "⏲" 📄 👆 ➡ 🛠️ 👈 🎦 ❔ 🔢 🛠️ 🔜 👀 💖:
