0
0
Microservicessystem_design~10 mins

API key management in Microservices - Interactive Code Practice

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

Complete the code to generate a unique API key using UUID.

Microservices
import uuid

def generate_api_key():
    return str(uuid.[1]())
Drag options to blanks, or click blank then click option'
Auuid3
Buuid1
Cuuid4
Duuid5
Attempts:
3 left
💡 Hint
Common Mistakes
Using uuid1 which is time-based and less secure.
2fill in blank
medium

Complete the code to store the API key with an expiration time in a key-value store.

Microservices
def store_api_key(store, key, user_id):
    expiration = 3600  # 1 hour in seconds
    store.set(key, user_id, [1]=expiration)
Drag options to blanks, or click blank then click option'
Aexpire
Bttl
Ctimeout
Dduration
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'expire' which is a method, not a parameter.
3fill in blank
hard

Fix the error in the code that validates the API key from the request headers.

Microservices
def validate_api_key(request, store):
    api_key = request.headers.get('[1]')
    if not api_key:
        return False
    return store.exists(api_key)
Drag options to blanks, or click blank then click option'
AX-API-Key
BAuthorization
CApi-Key
DToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Authorization' which is for bearer tokens, not API keys.
4fill in blank
hard

Fill both blanks to implement rate limiting using API keys and a Redis counter.

Microservices
def is_rate_limited(store, api_key):
    count = store.get(api_key) or 0
    if count [1] 100:
        return True
    store.incr(api_key)
    store.expire(api_key, [2])
    return False
Drag options to blanks, or click blank then click option'
A>=
B<=
C60
D3600
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' which would allow too many requests.
Setting expiration to 60 seconds which is too short.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps API keys to user IDs only if the keys are active.

Microservices
active_keys = {key: store.get(key) for key in keys if store.[1](key) and store.[2](key) > 0 and store.[3](key) == 'active'}
Drag options to blanks, or click blank then click option'
Aexists
Bttl
Chget
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'hget' for hash fields.
Not checking if the key exists before other calls.