A simple Python HTTP server that provides a REST API for dividing two numbers.
- POST /divide - Divide two numbers with comprehensive error handling
- GET /health - Health check endpoint
- GET / - API documentation and help
- Input validation and error handling
- JSON request/response format
- Logging for operations
- Clone the repository:
git clone <repository-url>
cd PyApiToDevide- Install dependencies:
pip install -r requirements.txtRun the Flask application:
python app.pyThe server will start on http://localhost:5000
Divides the first number (numerator) by the second number (denominator).
Request:
{
"numerator": 10,
"denominator": 2
}Response:
{
"result": 5.0,
"numerator": 10.0,
"denominator": 2.0
}Health check endpoint to verify the server is running.
Response:
{
"status": "healthy",
"message": "Division API is running"
}API documentation and help.
Response:
{
"message": "Division API Server",
"endpoints": {
"POST /divide": "Divide two numbers",
"GET /health": "Health check",
"GET /": "This help message"
},
"usage": {
"POST /divide": {
"description": "Divide numerator by denominator",
"request_body": {
"numerator": "number (required)",
"denominator": "number (required)"
},
"example": {
"numerator": 10,
"denominator": 2
}
}
}
}The API handles various error scenarios:
- 400 Bad Request: Missing fields, invalid input types, division by zero
- 500 Internal Server Error: Unexpected server errors
Example error response:
{
"error": "Division by zero",
"message": "Denominator cannot be zero"
}Run the test suite to verify the API functionality:
python test_api.pyThe test suite includes:
- Basic division operations
- Decimal number handling
- Negative number handling
- Error case testing (division by zero, invalid input, etc.)
- Health check verification
# Basic division
curl -X POST http://localhost:5000/divide \
-H "Content-Type: application/json" \
-d '{"numerator": 15, "denominator": 3}'
# Health check
curl http://localhost:5000/health
# Get API documentation
curl http://localhost:5000/import requests
# Divide two numbers
response = requests.post(
'http://localhost:5000/divide',
json={'numerator': 20, 'denominator': 4}
)
if response.status_code == 200:
result = response.json()
print(f"Result: {result['result']}")
else:
print(f"Error: {response.json()}")PyApiToDevide/
├── app.py # Main Flask application
├── test_api.py # Test suite for the API
├── requirements.txt # Python dependencies
└── README.md # This documentation
- Python 3.6+
- Flask 3.0.0
- requests (for testing)
This project is open source and available under the MIT License.