Skip to content

Latest commit

Β 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Œ Number Classification API

πŸ“– Overview

The Number Classification API is a serverless API deployed on AWS Lambda and API Gateway. It accepts a number as input and returns its mathematical properties along with a fun fact from the Numbers API.

✨ Features

🟒 Checks if a number is Prime, Perfect, or Armstrong

πŸ”’ Identifies whether a number is odd or even

πŸ” Calculates the sum of digits of the number

πŸŽ‰ Fetches a fun fact about the number using Numbers API

🌍 CORS-enabled for cross-origin requests

⚑ Fast response time (<500ms)

πŸš€ Serverless architecture using AWS Lambda & API Gateway

πŸ“‚ Version-controlled on GitHub with structured documentation

πŸ› οΈ Tech Stack

AWS Lambda (Backend Execution)

API Gateway (Public API Hosting)

Python 3.x (Programming Language)

Numbers API (External Fun Fact API)

GitHub (Version Control)

πŸ“Œ API Specification

πŸ”Ή Endpoint

GET /api/classify-number?number=

πŸ”Ή Example Request

curl -X GET "/api/classify-number?number=371"

πŸ”Ή Successful Response (200 OK)

{ "number": 371, "is_prime": false, "is_perfect": false, "properties": ["armstrong", "odd"], "digit_sum": 11, "fun_fact": "371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371" }

πŸ”Ή Error Response (400 Bad Request)

{ "number": "invalid_input", "error": true }

πŸ—οΈ How It Works

User sends a GET request with a number as a query parameter.

API validates the number, ensuring it's a valid integer or float.

Calculates properties (prime, perfect, Armstrong, odd/even, digit sum).

Fetches a fun fact from the Numbers API.

Returns a structured JSON response.

πŸš€ Deployment on AWS Lambda

Step 1: Create an AWS Lambda Function

Go to AWS Lambda and click Create function.

Choose Author from scratch.

Runtime: Python 3.x.

Upload the lambda_function.py file.

Set the Handler to lambda_function.lambda_handler.

Increase Timeout to 5 seconds.

Step 2: Configure API Gateway

Go to API Gateway and create a REST API.

Add a new GET method linked to your Lambda function.

Enable CORS (Cross-Origin Resource Sharing).

Deploy the API and copy the public endpoint URL.

Step 3: Test API

Run:

curl -X GET "/api/classify-number?number=371"

You should receive a JSON response with number properties and a fun fact.

πŸ› οΈ Local Development & Testing

1️⃣ Clone the Repository

git clone https://github.com/Aneeta007/Number-Classification-API.git

2️⃣ Install Dependencies

pip install requests

3️⃣ Run Locally

python lambda_function.py

4️⃣ Test Locally

import lambda_function

test_event = { "queryStringParameters": {"number": "371"} } print(lambda_function.lambda_handler(test_event, None))

🎯 Challenges Faced & Fixes

βœ… 500 Internal Server Error for Negative Numbers β†’ Now returns 200 OK for all valid numbers (negative & floating-point supported). βœ… Syntax Errors in Lambda Function β†’ Fixed improper JSON formatting in response handling. βœ… CORS Issues β†’ Implemented correct CORS headers in the response. βœ… Slow Response Time β†’ Optimized API request handling and logic execution.

About

This is a FastAPI-based web service that classifies numbers based on various mathematical properties. The API determines whether a number is prime, perfect, Armstrong, odd, or even, and provides a fun fact sourced from the Numbers API.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors