from flask import Flask, request, Response
app = Flask(__name__)
def sandbox_handler():
"""Handles requests for sandbox testing with header validation."""
try:
# Sanity checks for required headers
if 'X-Sandbox-Id' not in request.headers:
raise ValueError("Missing X-Sandbox-Id header")
if 'X-Sandbox-Version' not in request.headers:
raise ValueError("Missing X-Sandbox-Version header")
# Basic data validation (example)
try:
sandbox_id = request.headers['X-Sandbox-Id']
version = request.headers['X-Sandbox-Version']
int(sandbox_id) # Check if sandbox_id is an integer
int(version) # Check if version is an integer
except ValueError:
raise ValueError("X-Sandbox-Id and X-Sandbox-Version must be integers.")
# Simulate a response with the validated headers
response_body = "Sandbox test successful!"
return Response(response_body, status=200, headers={'Content-Type': 'text/plain'})
except ValueError as e:
# Return an error response if validation fails
return Response(f"Error: {str(e)}", status=400, headers={'Content-Type': 'text/plain'})
except Exception as e:
# Handle other exceptions
return Response(f"Internal Server Error: {str(e)}", status=500, headers={'Content-Type': 'text/plain'})
@app.route('/sandbox', methods=['GET'])
def sandbox():
"""Routes requests to the sandbox handler."""
return sandbox_handler()
if __name__ == '__main__':
app.run(debug=True)
Add your comment