0
0
Rest APIprogramming~10 mins

API gateway patterns in Rest API - Interactive Code Practice

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

Complete the code to define the main function of an API Gateway that routes requests.

Rest API
def api_gateway(request):
    if request.path == '/users':
        return [1](request)
    else:
        return '404 Not Found'
Drag options to blanks, or click blank then click option'
Aauthenticate_user
Bhandle_users_service
Cprocess_payment_service
Dlog_request
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a service unrelated to the '/users' path.
Using a function that only logs or authenticates instead of routing.
2fill in blank
medium

Complete the code to add authentication middleware in the API Gateway.

Rest API
def api_gateway(request):
    if not [1](request):
        return '401 Unauthorized'
    # Continue routing
Drag options to blanks, or click blank then click option'
Avalidate_token
Blog_request
Chandle_users_service
Dcache_response
Attempts:
3 left
💡 Hint
Common Mistakes
Using logging or caching functions instead of authentication.
Skipping authentication checks.
3fill in blank
hard

Fix the error in the code that implements rate limiting in the API Gateway.

Rest API
def api_gateway(request):
    if not [1](request.user_id):
        return '429 Too Many Requests'
    # Continue processing
Drag options to blanks, or click blank then click option'
Acheck_rate_limit
Blog_request
Cvalidate_token
Dhandle_users_service
Attempts:
3 left
💡 Hint
Common Mistakes
Using authentication or logging functions instead of rate limiting.
Not passing the correct user identifier.
4fill in blank
hard

Fill both blanks to implement caching and logging in the API Gateway.

Rest API
def api_gateway(request):
    response = [1](request)
    [2](request, response)
    return response
Drag options to blanks, or click blank then click option'
Aget_cached_response
Blog_request_response
Cvalidate_token
Dhandle_users_service
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up caching and authentication functions.
Not logging after getting the response.
5fill in blank
hard

Fill all three blanks to implement request validation, routing, and error handling in the API Gateway.

Rest API
def api_gateway(request):
    if not [1](request):
        return '400 Bad Request'
    try:
        response = [2](request)
    except Exception:
        response = [3]()
    return response
Drag options to blanks, or click blank then click option'
Avalidate_request_schema
Broute_to_service
Chandle_error_response
Dlog_request
Attempts:
3 left
💡 Hint
Common Mistakes
Skipping request validation.
Not catching exceptions properly.
Using logging instead of error handling for exceptions.