diff --git a/slop_with_models.py b/slop_with_models.py index 4895720..23560ef 100644 --- a/slop_with_models.py +++ b/slop_with_models.py @@ -123,7 +123,11 @@ tools = { "execute": lambda params: {"result": f"Hello, {params['name']}!"}, }, } -resources = {"hello": {"id": "hello", "content": "Hello, SLOP!"}} +resources = { + "hello": {"id": "hello", "content": "Hello, SLOP!"}, + "foo/bar": {"id": "foo/bar", "content": "Nested Foo Bar"}, + "foo/baz": {"id": "foo/baz", "content": "Nested Foo Baz"}, +} # Memory endpoints with lock and timeout LOCK_TIMEOUT = 2 # Seconds to wait for lock acquisition @@ -291,13 +295,38 @@ def list_resources(): @app.route("/resources/", methods=["GET"]) def get_resource(resource_id): - logger.info(f"Received /resources/{resource_id} request") - if resource_id not in resources: - logger.error(f"Resource not found: {resource_id}") + logger.info(f"Received /resources/{resource_id} GET request") + if resource_id in resources: + resource = resources[resource_id] + logger.debug(f"Exact match found for {resource_id}: {resource}") + return jsonify(resource), 200 + else: + # Prefix search for nested resources + prefix = f"{resource_id}/" + matching_resources = [ + res for key, res in resources.items() if key.startswith(prefix) + ] + if matching_resources: + logger.debug(f"Prefix search for {resource_id} found: {matching_resources}") + return jsonify({"resources": matching_resources}), 200 + logger.warning(f"No resource or nested resources found for {resource_id}") return jsonify({"error": "Resource not found"}), 404 - resource = resources[resource_id] - logger.debug(f"Returning resource {resource_id}: {resource}") - return jsonify(resource), 200 + +@app.route("/resources/", methods=["PUT"]) +def update_resource(resource_id): + logger.info(f"Received /resources/{resource_id} PUT request") + data = request.json + logger.debug(f"Update resource data: {data}") + if not data or "content" not in data: + logger.warning(f"Invalid resource update request: {data}") + return jsonify({"error": "Missing 'content'"}), 400 + # Update or create the resource + resources[resource_id] = { + "id": resource_id, + "content": data["content"] + } + logger.info(f"Updated/created resource {resource_id}: {resources[resource_id]}") + return jsonify({"status": "updated", "resource": resources[resource_id]}), 200 @app.route("/pay", methods=["POST"]) def pay():