0
0
FastAPIframework~10 mins

Request timing middleware in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct module for timing.

FastAPI
from fastapi import FastAPI
import [1]

app = FastAPI()
Drag options to blanks, or click blank then click option'
Atime
Bdatetime
Cos
Dsys
Attempts:
3 left
💡 Hint
Common Mistakes
Importing datetime instead of time
Using os or sys which are unrelated to timing
2fill in blank
medium

Complete the code to define a middleware function that records the start time.

FastAPI
@app.middleware("http")
async def add_process_time_header(request, call_next):
    start_time = [1]()
    response = await call_next(request)
    return response
Drag options to blanks, or click blank then click option'
Atime.sleep
Btime.time
Cdatetime.now
Dtime.clock
Attempts:
3 left
💡 Hint
Common Mistakes
Using datetime.now which returns a datetime object
Using time.sleep which pauses execution
Using deprecated time.clock
3fill in blank
hard

Fix the error in calculating the process time by completing the blank.

FastAPI
@app.middleware("http")
async def add_process_time_header(request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() [1] start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response
Drag options to blanks, or click blank then click option'
A/
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Adding times instead of subtracting
Multiplying or dividing times which is incorrect
4fill in blank
hard

Fill both blanks to convert the process time to milliseconds and add it to the response headers.

FastAPI
@app.middleware("http")
async def add_process_time_header(request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = (time.time() - start_time) [1] 1000
    response.headers["X-Process-Time-ms"] = str(round(process_time, [2]))
    return response
Drag options to blanks, or click blank then click option'
A*
B//
C4
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer division instead of multiplication
Using addition instead of multiplication
Using wrong number of decimals like 5 or 3
5fill in blank
hard

Fill all three blanks to create a complete middleware that measures request time and adds it as a header rounded to 3 decimals.

FastAPI
from fastapi import FastAPI
import time

app = FastAPI()

@app.middleware("http")
async def add_process_time_header(request, call_next):
    start = [1]()
    response = await call_next(request)
    duration = [3]() - start
    response.headers["X-Process-Time"] = str(round(duration, [2]))
    return response

@app.get("/")
async def root():
    return {"message": "Hello World"}
Drag options to blanks, or click blank then click option'
Atime.time
Btime.sleep
C3
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using sleep instead of time for start
Not rounding or rounding to wrong decimal places