114 lines
1.8 KiB
Markdown

# Number Divisibility API
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
- History endpoint to view all past checks
- Database integration with SQLAlchemy and SQLite
- Database migrations managed by Alembic
- 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 /history
Get the history of all divisibility checks performed.
**Response:**
```json
[
{
"number": 42,
"is_divisible_by_2": true,
"id": 1,
"created_at": "2025-05-14T12:34:56.789Z"
},
{
"number": 7,
"is_divisible_by_2": false,
"id": 2,
"created_at": "2025-05-14T12:35:01.234Z"
}
]
```
### 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
```
3. Run database migrations:
```
alembic upgrade head
```
## 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`