1. from flask import Flask, request, Response
  2. app = Flask(__name__)
  3. def sandbox_handler():
  4. """Handles requests for sandbox testing with header validation."""
  5. try:
  6. # Sanity checks for required headers
  7. if 'X-Sandbox-Id' not in request.headers:
  8. raise ValueError("Missing X-Sandbox-Id header")
  9. if 'X-Sandbox-Version' not in request.headers:
  10. raise ValueError("Missing X-Sandbox-Version header")
  11. # Basic data validation (example)
  12. try:
  13. sandbox_id = request.headers['X-Sandbox-Id']
  14. version = request.headers['X-Sandbox-Version']
  15. int(sandbox_id) # Check if sandbox_id is an integer
  16. int(version) # Check if version is an integer
  17. except ValueError:
  18. raise ValueError("X-Sandbox-Id and X-Sandbox-Version must be integers.")
  19. # Simulate a response with the validated headers
  20. response_body = "Sandbox test successful!"
  21. return Response(response_body, status=200, headers={'Content-Type': 'text/plain'})
  22. except ValueError as e:
  23. # Return an error response if validation fails
  24. return Response(f"Error: {str(e)}", status=400, headers={'Content-Type': 'text/plain'})
  25. except Exception as e:
  26. # Handle other exceptions
  27. return Response(f"Internal Server Error: {str(e)}", status=500, headers={'Content-Type': 'text/plain'})
  28. @app.route('/sandbox', methods=['GET'])
  29. def sandbox():
  30. """Routes requests to the sandbox handler."""
  31. return sandbox_handler()
  32. if __name__ == '__main__':
  33. app.run(debug=True)

Add your comment