A KV store isn't very useful if only one Python process can talk to it. Wrap it with Python's stdlib http.server so clients can PUT, GET, and DELETE via HTTP.
This step doesn't actually boot a server during the tests β it just checks that your Handler class defines the three verbs and integrates cleanly with the store.
from http.server import BaseHTTPRequestHandler
import json
import time
class KeyValueStore:
def __init__(self):
self._store = {}
def put(self, key, value, ttl_seconds=None):
expiry = time.time() + ttl_seconds if ttl_seconds is not None else None
self._store[key] = {"value": value, "expiry": expiry}
def get(self, key):
entry = self._store.get(key)
if entry is None:
return None
if entry["expiry"] and time.time() > entry["expiry"]:
del self._store[key]
return None
return entry["value"]
def delete(self, key):
if key in self._store:
del self._store[key]
return True
return False
store = KeyValueStore()
class Handler(BaseHTTPRequestHandler):
def do_PUT(self):
"""Handle PUT /store/<key> with JSON body {"value": "...", "ttl": N}"""
# TODO: Parse the key from self.path (e.g. /store/<key>)
# TODO: Read and parse the JSON body
# TODO: Call store.put(key, value, ttl)
# TODO: Respond 200 with JSON {"status": "ok"}
pass
def do_GET(self):
"""Handle GET /store/<key>"""
# TODO: Parse the key
# TODO: Look it up in the store
# TODO: 200 with value, or 404 if missing
pass
def do_DELETE(self):
"""Handle DELETE /store/<key>"""
# TODO: Parse the key
# TODO: Call store.delete(key)
# TODO: 200 or 404
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
A KV store isn't very useful if only one Python process can talk to it. Wrap it with Python's stdlib http.server so clients can PUT, GET, and DELETE via HTTP.
This step doesn't actually boot a server during the tests β it just checks that your Handler class defines the three verbs and integrates cleanly with the store.
Press Run Tests or ββ© to check your solution.