Automated Action 3efcd88ffa Create Calculator API with FastAPI and SQLAlchemy
- Setup FastAPI project structure
- Add database models for calculations
- Create API endpoints for basic math operations
- Add health endpoint
- Configure SQLite and Alembic for database management
- Update README with usage instructions

generated with BackendIM... (backend.im)
2025-05-13 22:57:26 +00:00

1.7 KiB

Calculator API

A simple calculator API built with FastAPI and SQLAlchemy that performs basic arithmetic operations and stores calculation history.

Features

  • Basic arithmetic operations (add, subtract, multiply, divide)
  • Square root calculation
  • Calculation history storage in SQLite database
  • Health check endpoint

API Endpoints

Calculator Operations

  • POST /api/v1/calculator/ - Perform a calculation
  • GET /api/v1/calculator/history - Get calculation history
  • GET /api/v1/calculator/{calculation_id} - Get a specific calculation

Health Check

  • GET /api/v1/health - API health status

Installation

# Clone the repository
git clone <repository-url>

# Install dependencies
pip install -r requirements.txt

# Run migrations
alembic upgrade head

# Start the API
uvicorn main:app --reload

Environment Variables

Create a .env file in the root directory with the following variables:

# No environment variables required for basic setup

API Documentation

Once the API is running, you can access the API documentation at:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

Example Usage

Perform Addition

POST /api/v1/calculator/
{
  "operation": "add",
  "first_number": 5,
  "second_number": 3
}

Response:

{
  "id": 1,
  "operation": "add",
  "first_number": 5,
  "second_number": 3,
  "result": 8,
  "created_at": "2025-05-13T12:00:00.000Z"
}

Perform Square Root

POST /api/v1/calculator/
{
  "operation": "square_root",
  "first_number": 16
}

Response:

{
  "id": 2,
  "operation": "square_root",
  "first_number": 16,
  "second_number": null,
  "result": 4,
  "created_at": "2025-05-13T12:01:00.000Z"
}