
- Add is_divisible_by_3 field to database model and schema - Create migration script for the new field - Update existing endpoints to check divisibility by 3 - Add dedicated endpoints for divisibility by 3 - Update README with new endpoint documentation - Fix linting issues with ruff
162 lines
2.7 KiB
Markdown
162 lines
2.7 KiB
Markdown
# Number Divisibility API
|
|
|
|
A simple REST API built with FastAPI that checks if a number is divisible by 2 and by 3.
|
|
|
|
## Features
|
|
|
|
- Check if a number is divisible by 2 and by 3 via GET or POST requests
|
|
- Dedicated endpoints for divisibility by 2 and by 3
|
|
- 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 and by 3 using a path parameter.
|
|
|
|
**Parameters:**
|
|
- `number` (integer): The number to check
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"number": 42,
|
|
"is_divisible_by_2": true,
|
|
"is_divisible_by_3": false
|
|
}
|
|
```
|
|
|
|
### POST /divisibility
|
|
|
|
Check if a number is divisible by 2 and by 3 using a JSON request body.
|
|
|
|
**Request Body:**
|
|
```json
|
|
{
|
|
"number": 42
|
|
}
|
|
```
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"number": 42,
|
|
"is_divisible_by_2": true,
|
|
"is_divisible_by_3": false
|
|
}
|
|
```
|
|
|
|
### GET /divisibility/by3/{number}
|
|
|
|
Check if a number is divisible by 3 using a path parameter.
|
|
|
|
**Parameters:**
|
|
- `number` (integer): The number to check
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"number": 9,
|
|
"is_divisible_by_2": false,
|
|
"is_divisible_by_3": true
|
|
}
|
|
```
|
|
|
|
### POST /divisibility/by3
|
|
|
|
Check if a number is divisible by 3 using a JSON request body.
|
|
|
|
**Request Body:**
|
|
```json
|
|
{
|
|
"number": 9
|
|
}
|
|
```
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"number": 9,
|
|
"is_divisible_by_2": false,
|
|
"is_divisible_by_3": true
|
|
}
|
|
```
|
|
|
|
### GET /history
|
|
|
|
Get the history of all divisibility checks performed.
|
|
|
|
**Response:**
|
|
```json
|
|
[
|
|
{
|
|
"number": 42,
|
|
"is_divisible_by_2": true,
|
|
"is_divisible_by_3": false,
|
|
"id": 1,
|
|
"created_at": "2025-05-14T12:34:56.789Z"
|
|
},
|
|
{
|
|
"number": 7,
|
|
"is_divisible_by_2": false,
|
|
"is_divisible_by_3": false,
|
|
"id": 2,
|
|
"created_at": "2025-05-14T12:35:01.234Z"
|
|
},
|
|
{
|
|
"number": 9,
|
|
"is_divisible_by_2": false,
|
|
"is_divisible_by_3": true,
|
|
"id": 3,
|
|
"created_at": "2025-05-14T12:36:05.678Z"
|
|
}
|
|
]
|
|
```
|
|
|
|
### 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` |