0
0
Rest APIprogramming~10 mins

Per-user vs per-IP limits in Rest API - Interactive Practice

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

Complete the code to check the user's ID for rate limiting.

Rest API
if request.user.id == [1]:
    allow_request()
Drag options to blanks, or click blank then click option'
A12345
Buser_id
Crequest.ip
Drequest.user.id
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number instead of the dynamic user ID.
Confusing IP address with user ID.
2fill in blank
medium

Complete the code to extract the IP address from the request headers.

Rest API
ip_address = request.headers.get([1])
Drag options to blanks, or click blank then click option'
A'X-Forwarded-For'
B'User-Agent'
C'Authorization'
D'Content-Type'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated headers like 'User-Agent' or 'Authorization'.
Not using quotes around the header name.
3fill in blank
hard

Fix the error in the rate limit check comparing IP addresses.

Rest API
if request.ip == [1]:
    block_request()
Drag options to blanks, or click blank then click option'
Arequest.user.id
Brequest.ip
Crequest.headers.get('X-Forwarded-For')
Duser_ip
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing IP to user ID.
Using request.ip directly without checking headers.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps user IDs to their request counts if the count is greater than 5.

Rest API
user_limits = {user.id: requests[1] for user, requests in user_requests.items() if requests [2] 5}
Drag options to blanks, or click blank then click option'
A* 2
B>
C>=
D+ 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of addition for counts.
Using '>=' instead of '>' which changes the filter condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps IP addresses to user IDs only if the user is active and the IP is not empty.

Rest API
ip_user_map = {request.headers.get([1]): user.id for user, request in requests.items() if user.active == [2] and request.headers.get([3]) != ''}
Drag options to blanks, or click blank then click option'
A'X-Forwarded-For'
BTrue
C'Remote-Addr'
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up header names for IP addresses.
Using False instead of True for active user check.