This project implements a web service for processing receipts and calculating points based on a specific ruleset from the Fetch Rewards Receipt Processor Challenge.
The service is built using FastAPI and includes endpoints for submitting receipts and retrieving points awarded for a given receipt ID. The project is designed to fulfill the requirements of a coding challenge, with a focus on in-memory data storage, efficient request handling, and comprehensive testing.
- Efficient Request Handling: Utilizes FastAPI's async capabilities for efficient request handling.
- In-Memory Storage: Stores receipt data in memory, ensuring no data persistence across application restarts.
- Comprehensive Testing: Includes both unit and integration tests to ensure the correctness of the implementation.
- Dockerized Setup: Provides a Dockerfile for easy containerization and deployment.
- Exception Handlers: Provides exception handler examples for
ValidationError
andHTTPException
to return meaningful error responses.
- Endpoint:
/receipts/process
- Method:
POST
- Description: Submits a receipt for processing and returns a JSON object with an ID generated by the application.
- Request Body: JSON object representing the receipt.
- Response: JSON object containing the receipt ID.
{
"retailer": "Target",
"purchaseDate": "2022-01-01",
"purchaseTime": "13:01",
"items": [
{"shortDescription": "Mountain Dew 12PK", "price": "6.49"},
{"shortDescription": "Emils Cheese Pizza", "price": "12.25"},
{"shortDescription": "Knorr Creamy Chicken", "price": "1.26"},
{"shortDescription": "Doritos Nacho Cheese", "price": "3.35"},
{"shortDescription": "Klarbrunn 12-PK 12 FL OZ", "price": "12.00"}
],
"total": "35.35"
}
{
"id": "7fb1377b-b223-49d9-a31a-5a02701dd310"
}
- Endpoint:
/receipts/{id}/points
- Method:
GET
- Description: Retrieves the points awarded for the receipt with the given ID.
- Response: JSON object containing the number of points awarded.
GET /receipts/7fb1377b-b223-49d9-a31a-5a02701dd310/points
{
"points": 28
}
- One point for every alphanumeric character in the retailer name.
- 50 points if the total is a round dollar amount with no cents.
- 25 points if the total is a multiple of 0.25.
- 5 points for every two items on the receipt.
- If the trimmed length of the item description is a multiple of 3, multiply the price by 0.2 and round up to the nearest integer.
- 6 points if the day in the purchase date is odd.
- 10 points if the time of purchase is after 2:00pm and before 4:00pm.
git clone https://github.com/ryanlevee/receipt-processor.git
Make sure you are in the root directory for the project (receipt-processor
).
docker build -t receipt-processor-image:1.0 .
docker images
docker run -d --name receipt-processor -p 8000:8000 receipt-processor-image:1.0
docker ps
docker logs receipt-processor
API documentation and UI can now be accessed at http://localhost:8000/docs
.
-
Run the tests using pytest:
pytest
receipt-processor/
├── main.py # Main application code
├── tests/
│ ├── __init__.py # Test package initializer
│ ├── test_integration.py# Integration tests
│ ├── test_unit.py # Unit tests
├── requirements.txt # Project dependencies
├── Dockerfile # Dockerfile for containerization
└── README.md # Project documentation
-
Clone the repository:
git clone https://github.com/ryanlevee/receipt-processor.git cd receipt-processor
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
uvicorn main:app --host 0.0.0.0 --port 8000
-
Access the API documentation and UI at
http://localhost:8000/docs
.