import logging
import json
+import uuid
from typing import Optional
import redis
-from flask import Flask, Response
+from flask import Flask, Response, request
redis_client = redis.Redis(host="redis", port=6379, decode_responses=True)
),
status=400,
)
- # TODO check that the subpath contains no more than 3 unique subpath strings
+ # ASSUMPTION: The test runner will provide the api context to retrieve valid path components from the database
+ test_id = request.args.get("test")
+ if test_id:
+ app.logger.debug("Under test %s", test_id)
+ if test_parts := redis_client.smembers(f"test:{test_id}"):
+ if any([part not in test_parts for part in path_parts]):
+ return Response(
+ response=json.dumps(
+ {
+ "error": f"Invalid API path ({subpath}). All segments in test {test_id} must be in {test_parts}"
+ }
+ ),
+ status=400,
+ )
+ else:
+ app.logger.debug("Test '%s' not found", test_id)
hits = increment_hits(subpath)
return {"hits": hits}
@app.route("/test/<int:num_requests>/", methods=["POST"])
def test(num_requests: int):
increment_hits("test")
- return {}
+ # ASSUMPTION: The requirements don't specify if the test runner or the api path under test specify
+ # which 3 path parts are valid, so I will have the test runner generate and store them.
+ test_id = str(uuid.uuid4())
+ paths = ["abc", "def", "ghi"]
+ redis_client.sadd(f"test:{test_id}", *paths)
+ return {"test_id": test_id}