Automated Action 2d0237b831 Create Number Divisibility API
- Implemented FastAPI application with divisibility checking
- Added endpoints for checking divisibility via GET and POST
- Added health endpoint
- Created requirements.txt
- Updated README with API documentation

generated with BackendIM... (backend.im)
2025-05-13 16:03:00 +00:00

58 lines
1.6 KiB
Python

from fastapi import FastAPI, HTTPException, Path as PathParam
from pydantic import BaseModel
from pathlib import Path
# Create the FastAPI app
app = FastAPI(
title="Number Divisibility API",
description="A simple API to check if a number is divisible by 2",
version="1.0.0"
)
class NumberRequest(BaseModel):
number: int
class DivisibilityResponse(BaseModel):
number: int
is_divisible_by_2: bool
@app.get("/")
def read_root():
return {"message": "Welcome to the Number Divisibility API"}
@app.get("/divisibility/{number}", response_model=DivisibilityResponse)
def check_divisibility(number: int = PathParam(..., description="The number to check divisibility for")):
"""
Check if a number is divisible by 2.
Returns a JSON response with the number and a boolean indicating if it's divisible by 2.
"""
return {
"number": number,
"is_divisible_by_2": number % 2 == 0
}
@app.post("/divisibility", response_model=DivisibilityResponse)
def check_divisibility_post(request: NumberRequest):
"""
Check if a number is divisible by 2 using POST request.
Returns a JSON response with the number and a boolean indicating if it's divisible by 2.
"""
return {
"number": request.number,
"is_divisible_by_2": request.number % 2 == 0
}
@app.get("/health")
def health_check():
"""
Health check endpoint to verify the API is running.
Returns a JSON response with status information.
"""
return {
"status": "healthy",
"api_version": app.version,
"service": "Number Divisibility API"
}