From 2d0237b831442ed04379fa31b6aa32ab3f6d656f Mon Sep 17 00:00:00 2001 From: Automated Action Date: Tue, 13 May 2025 16:03:00 +0000 Subject: [PATCH] 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) --- README.md | 86 ++++++++++++++++++++++++++++++++++++++++++++++-- main.py | 58 ++++++++++++++++++++++++++++++++ requirements.txt | 3 ++ 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 main.py create mode 100644 requirements.txt diff --git a/README.md b/README.md index e8acfba..dad4e11 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,85 @@ -# FastAPI Application +# Number Divisibility API -This is a FastAPI application bootstrapped by BackendIM, the AI-powered backend generation platform. +A simple REST API built with FastAPI that checks if a number is divisible by 2. + +## Features + +- Check if a number is divisible by 2 via GET or POST requests +- Health endpoint for monitoring +- OpenAPI documentation built-in + +## API Endpoints + +### GET / + +Welcome message for the API. + +### GET /divisibility/{number} + +Check if a number is divisible by 2 using a path parameter. + +**Parameters:** +- `number` (integer): The number to check + +**Response:** +```json +{ + "number": 42, + "is_divisible_by_2": true +} +``` + +### POST /divisibility + +Check if a number is divisible by 2 using a JSON request body. + +**Request Body:** +```json +{ + "number": 42 +} +``` + +**Response:** +```json +{ + "number": 42, + "is_divisible_by_2": true +} +``` + +### GET /health + +Health check endpoint to verify the API is running. + +**Response:** +```json +{ + "status": "healthy", + "api_version": "1.0.0", + "service": "Number Divisibility API" +} +``` + +## Installation + +1. Clone the repository +2. Install dependencies: + ``` + pip install -r requirements.txt + ``` + +## Running the API + +``` +uvicorn main:app --reload +``` + +The API will be available at `http://localhost:8000` + +## API Documentation + +FastAPI automatically generates interactive API documentation: + +- Swagger UI: `http://localhost:8000/docs` +- ReDoc: `http://localhost:8000/redoc` \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..8829689 --- /dev/null +++ b/main.py @@ -0,0 +1,58 @@ +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" + } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..723c3ee --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +fastapi==0.104.1 +uvicorn==0.23.2 +pydantic==2.4.2 \ No newline at end of file