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)
This commit is contained in:
parent
c0048fb1ec
commit
2d0237b831
86
README.md
86
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`
|
58
main.py
Normal file
58
main.py
Normal file
@ -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"
|
||||||
|
}
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fastapi==0.104.1
|
||||||
|
uvicorn==0.23.2
|
||||||
|
pydantic==2.4.2
|
Loading…
x
Reference in New Issue
Block a user