Skip to content

🛠️

👆 💪 🚮 🛠️ FastAPI 🈸.

"🛠️" 🔢 👈 👷 ⏮️ 🔠 📨 ⏭ ⚫️ 🛠️ 🙆 🎯 ➡ 🛠️. & ⏮️ 🔠 📨 ⏭ 🛬 ⚫️.

  • ⚫️ ✊ 🔠 📨 👈 👟 👆 🈸.
  • ⚫️ 💪 ⤴️ 🕳 👈 📨 ⚖️ 🏃 🙆 💪 📟.
  • ⤴️ ⚫️ 🚶‍♀️ 📨 🛠️ 🎂 🈸 ( ➡ 🛠️).
  • ⚫️ ⤴️ ✊ 📨 🏗 🈸 ( ➡ 🛠️).
  • ⚫️ 💪 🕳 👈 📨 ⚖️ 🏃 🙆 💪 📟.
  • ⤴️ ⚫️ 📨 📨.

📡 ℹ

🚥 👆 ✔️ 🔗 ⏮️ yield, 🚪 📟 🔜 🏃 ⏮️ 🛠️.

🚥 📤 🙆 🖥 📋 (📄 ⏪), 👫 🔜 🏃 ⏮️ 🌐 🛠️.

✍ 🛠️

✍ 🛠️ 👆 ⚙️ 👨‍🎨 @app.middleware("http") 🔛 🔝 🔢.

🛠️ 🔢 📨:

  • request.
  • 🔢 call_next 👈 🔜 📨 request 🔢.
    • 👉 🔢 🔜 🚶‍♀️ request 🔗 ➡ 🛠️.
    • ⤴️ ⚫️ 📨 response 🏗 🔗 ➡ 🛠️.
  • 👆 💪 ⤴️ 🔀 🌅 response ⏭ 🛬 ⚫️.
import time

from fastapi import FastAPI, Request

app = FastAPI()


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response

Tip

✔️ 🤯 👈 🛃 © 🎚 💪 🚮 ⚙️ '✖-' 🔡.

✋️ 🚥 👆 ✔️ 🛃 🎚 👈 👆 💚 👩‍💻 🖥 💪 👀, 👆 💪 🚮 👫 👆 ⚜ 📳 (⚜ (✖️-🇨🇳 ℹ 🤝)) ⚙️ 🔢 expose_headers 📄 💃 ⚜ 🩺.

📡 ℹ

👆 💪 ⚙️ from starlette.requests import Request.

FastAPI 🚚 ⚫️ 🏪 👆, 👩‍💻. ✋️ ⚫️ 👟 🔗 ⚪️➡️ 💃.

⏭ & ⏮️ response

👆 💪 🚮 📟 🏃 ⏮️ request, ⏭ 🙆 ➡ 🛠️ 📨 ⚫️.

& ⏮️ response 🏗, ⏭ 🛬 ⚫️.

🖼, 👆 💪 🚮 🛃 🎚 X-Process-Time ⚗ 🕰 🥈 👈 ⚫️ ✊ 🛠️ 📨 & 🏗 📨:

import time

from fastapi import FastAPI, Request

app = FastAPI()


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response

🎏 🛠️

👆 💪 ⏪ ✍ 🌖 🔃 🎏 🛠️ 🏧 👩‍💻 🦮: 🏧 🛠️.

👆 🔜 ✍ 🔃 ❔ 🍵 ⚜ ⏮️ 🛠️ ⏭ 📄.