0
0
FastAPIframework~10 mins

Rate limiting 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 rate limiting dependency from fastapi-limiter.

FastAPI
from fastapi_limiter import [1]
Drag options to blanks, or click blank then click option'
ARateLimiter
BLimiterDependency
CLimiter
DRateLimit
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a non-existent class like 'Limiter' or 'RateLimit'.
Confusing the import with other unrelated classes.
2fill in blank
medium

Complete the code to apply a rate limit of 5 requests per minute to the endpoint.

FastAPI
@app.get("/items", dependencies=[Depends([1]("5/minute"))])
def read_items():
    return {"message": "Hello"}
Drag options to blanks, or click blank then click option'
ALimiterDependency
BLimiter
CRateLimiter
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Depends' directly instead of wrapping the rate limiter.
Using incorrect class names like 'Limiter'.
3fill in blank
hard

Fix the error in the initialization of the Redis backend for rate limiting.

FastAPI
import aioredis

redis = aioredis.from_url([1])

@app.on_event("startup")
async def startup():
    await FastAPILimiter.init(redis)
Drag options to blanks, or click blank then click option'
A"redis://localhost:6379"
B"http://localhost:6379"
C"localhost:6379"
D"redis://127.0.0.1"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http://' instead of 'redis://'.
Omitting the port number.
Using just the hostname without protocol.
4fill in blank
hard

Fill both blanks to create a rate limit that allows 10 requests per minute and 100 requests per hour.

FastAPI
@app.get("/data", dependencies=[Depends(RateLimiter([1])), Depends(RateLimiter([2]))])
async def get_data():
    return {"data": "sample"}
Drag options to blanks, or click blank then click option'
A"10/minute"
B"100/hour"
C"10/hour"
D"100/minute"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'minute' and 'hour' in the strings.
Using incorrect numbers or units.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each user to their request count if it is greater than 10.

FastAPI
user_counts = {user: count for user, count in counts.items() if count [1] [2] and user.is_active == [3]
Drag options to blanks, or click blank then click option'
A>
B10
CTrue
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for count comparison.
Checking user.is_active against a string instead of boolean.