|
|
""" |
|
|
Example usage of the Smart Budget Recommendation API |
|
|
|
|
|
This script demonstrates how to use the API endpoints. |
|
|
""" |
|
|
|
|
|
import requests |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
BASE_URL = "http://localhost:8000" |
|
|
|
|
|
|
|
|
USER_ID = "user123" |
|
|
|
|
|
def create_sample_expenses(): |
|
|
"""Create sample expenses for testing""" |
|
|
expenses = [ |
|
|
{ |
|
|
"user_id": USER_ID, |
|
|
"amount": 3500, |
|
|
"category": "Groceries", |
|
|
"description": "Monthly groceries", |
|
|
"date": "2024-01-15T00:00:00", |
|
|
"type": "expense" |
|
|
}, |
|
|
{ |
|
|
"user_id": USER_ID, |
|
|
"amount": 4000, |
|
|
"category": "Groceries", |
|
|
"description": "Monthly groceries", |
|
|
"date": "2024-02-15T00:00:00", |
|
|
"type": "expense" |
|
|
}, |
|
|
{ |
|
|
"user_id": USER_ID, |
|
|
"amount": 3800, |
|
|
"category": "Groceries", |
|
|
"description": "Monthly groceries", |
|
|
"date": "2024-03-15T00:00:00", |
|
|
"type": "expense" |
|
|
}, |
|
|
{ |
|
|
"user_id": USER_ID, |
|
|
"amount": 4200, |
|
|
"category": "Groceries", |
|
|
"description": "Monthly groceries", |
|
|
"date": "2024-04-15T00:00:00", |
|
|
"type": "expense" |
|
|
}, |
|
|
{ |
|
|
"user_id": USER_ID, |
|
|
"amount": 2000, |
|
|
"category": "Transport", |
|
|
"description": "Monthly transport", |
|
|
"date": "2024-01-20T00:00:00", |
|
|
"type": "expense" |
|
|
}, |
|
|
{ |
|
|
"user_id": USER_ID, |
|
|
"amount": 2200, |
|
|
"category": "Transport", |
|
|
"description": "Monthly transport", |
|
|
"date": "2024-02-20T00:00:00", |
|
|
"type": "expense" |
|
|
}, |
|
|
] |
|
|
|
|
|
print("Creating sample expenses...") |
|
|
for expense in expenses: |
|
|
response = requests.post(f"{BASE_URL}/expenses", json=expense) |
|
|
if response.status_code == 200: |
|
|
print(f"✓ Created expense: {expense['category']} - Rs.{expense['amount']}") |
|
|
else: |
|
|
print(f"✗ Failed to create expense: {response.text}") |
|
|
|
|
|
def get_recommendations(): |
|
|
"""Get budget recommendations""" |
|
|
print("\n" + "="*50) |
|
|
print("Getting Smart Budget Recommendations...") |
|
|
print("="*50) |
|
|
|
|
|
|
|
|
next_month = datetime.now().month + 1 |
|
|
next_year = datetime.now().year |
|
|
if next_month > 12: |
|
|
next_month = 1 |
|
|
next_year += 1 |
|
|
|
|
|
response = requests.get( |
|
|
f"{BASE_URL}/recommendations/{USER_ID}", |
|
|
params={"month": next_month, "year": next_year} |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
recommendations = response.json() |
|
|
print(f"\nFound {len(recommendations)} recommendations:\n") |
|
|
|
|
|
for rec in recommendations: |
|
|
print(f"Category: {rec['category']}") |
|
|
print(f" Average Expense: Rs.{rec['average_expense']:,.0f}") |
|
|
print(f" Recommended Budget: Rs.{rec['recommended_budget']:,.0f}") |
|
|
print(f" Confidence: {rec['confidence']*100:.0f}%") |
|
|
print(f" Reason: {rec['reason']}") |
|
|
print() |
|
|
else: |
|
|
print(f"Error: {response.status_code} - {response.text}") |
|
|
|
|
|
def get_category_expenses(): |
|
|
"""Get category expense averages""" |
|
|
print("\n" + "="*50) |
|
|
print("Getting Category Expense Averages...") |
|
|
print("="*50) |
|
|
|
|
|
response = requests.get( |
|
|
f"{BASE_URL}/category-expenses/{USER_ID}", |
|
|
params={"months": 3} |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
categories = response.json() |
|
|
print(f"\nFound {len(categories)} categories:\n") |
|
|
|
|
|
for cat in categories: |
|
|
print(f"{cat['category']}:") |
|
|
print(f" Average Monthly: Rs.{cat['average_monthly_expense']:,.0f}") |
|
|
print(f" Total Transactions: {cat['total_expenses']}") |
|
|
print(f" Months Analyzed: {cat['months_analyzed']}") |
|
|
print() |
|
|
else: |
|
|
print(f"Error: {response.status_code} - {response.text}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("Smart Budget Recommendation API - Example Usage") |
|
|
print("="*50) |
|
|
|
|
|
|
|
|
print("\nChecking API health...") |
|
|
response = requests.get(f"{BASE_URL}/health") |
|
|
if response.status_code == 200: |
|
|
print(f"✓ API is healthy: {response.json()}") |
|
|
else: |
|
|
print(f"✗ API health check failed: {response.text}") |
|
|
exit(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get_recommendations() |
|
|
|
|
|
|
|
|
get_category_expenses() |
|
|
|
|
|
|