Update utils/api_client.py
Browse files- utils/api_client.py +110 -1
utils/api_client.py
CHANGED
|
@@ -68,4 +68,113 @@ class RewardPilotClient:
|
|
| 68 |
self.get_recommendation(
|
| 69 |
user_id, merchant, mcc, amount_usd, transaction_date
|
| 70 |
)
|
| 71 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
self.get_recommendation(
|
| 69 |
user_id, merchant, mcc, amount_usd, transaction_date
|
| 70 |
)
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
def get_user_analytics(self, user_id: str) -> Dict[str, Any]:
|
| 74 |
+
"""
|
| 75 |
+
Fetch user analytics including spending patterns and optimization metrics
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
user_id: User identifier
|
| 79 |
+
|
| 80 |
+
Returns:
|
| 81 |
+
Dictionary containing analytics data
|
| 82 |
+
"""
|
| 83 |
+
try:
|
| 84 |
+
# Option 1: If you have a dedicated analytics endpoint
|
| 85 |
+
response = httpx.get(
|
| 86 |
+
f"{self.orchestrator_url}/analytics/{user_id}",
|
| 87 |
+
timeout=30.0
|
| 88 |
+
)
|
| 89 |
+
response.raise_for_status()
|
| 90 |
+
return response.json()
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
logger.error(f"Analytics fetch failed: {e}")
|
| 94 |
+
# Return mock data as fallback
|
| 95 |
+
return self._get_mock_analytics(user_id)
|
| 96 |
+
|
| 97 |
+
def _get_mock_analytics(self, user_id: str) -> Dict[str, Any]:
|
| 98 |
+
"""Generate mock analytics data for demo purposes"""
|
| 99 |
+
import random
|
| 100 |
+
|
| 101 |
+
# Different data per user for variety
|
| 102 |
+
user_multipliers = {
|
| 103 |
+
"u_alice": 1.2,
|
| 104 |
+
"u_bob": 0.9,
|
| 105 |
+
"u_charlie": 1.5,
|
| 106 |
+
}
|
| 107 |
+
multiplier = user_multipliers.get(user_id, 1.0)
|
| 108 |
+
|
| 109 |
+
return {
|
| 110 |
+
"user_id": user_id,
|
| 111 |
+
"annual_savings": round(342 * multiplier, 2),
|
| 112 |
+
"rate_increase": round(23 * multiplier, 1),
|
| 113 |
+
"optimized_transactions": int(156 * multiplier),
|
| 114 |
+
"optimization_score": min(100, int(87 * multiplier)),
|
| 115 |
+
"category_breakdown": [
|
| 116 |
+
{
|
| 117 |
+
"category": "π Groceries",
|
| 118 |
+
"monthly_spend": round(450 * multiplier, 2),
|
| 119 |
+
"best_card": "Amex Gold",
|
| 120 |
+
"rewards": round(27 * multiplier, 2),
|
| 121 |
+
"rate": "6%"
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"category": "π½οΈ Restaurants",
|
| 125 |
+
"monthly_spend": round(320 * multiplier, 2),
|
| 126 |
+
"best_card": "Amex Gold",
|
| 127 |
+
"rewards": round(12.8 * multiplier, 2),
|
| 128 |
+
"rate": "4%"
|
| 129 |
+
},
|
| 130 |
+
{
|
| 131 |
+
"category": "β½ Gas",
|
| 132 |
+
"monthly_spend": round(180 * multiplier, 2),
|
| 133 |
+
"best_card": "Costco Visa",
|
| 134 |
+
"rewards": round(7.2 * multiplier, 2),
|
| 135 |
+
"rate": "4%"
|
| 136 |
+
},
|
| 137 |
+
{
|
| 138 |
+
"category": "βοΈ Travel",
|
| 139 |
+
"monthly_spend": round(850 * multiplier, 2),
|
| 140 |
+
"best_card": "Sapphire Reserve",
|
| 141 |
+
"rewards": round(42.5 * multiplier, 2),
|
| 142 |
+
"rate": "5%"
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"category": "π¬ Entertainment",
|
| 146 |
+
"monthly_spend": round(125 * multiplier, 2),
|
| 147 |
+
"best_card": "Freedom Unlimited",
|
| 148 |
+
"rewards": round(1.88 * multiplier, 2),
|
| 149 |
+
"rate": "1.5%"
|
| 150 |
+
},
|
| 151 |
+
{
|
| 152 |
+
"category": "πͺ Online Shopping",
|
| 153 |
+
"monthly_spend": round(280 * multiplier, 2),
|
| 154 |
+
"best_card": "Amazon Prime",
|
| 155 |
+
"rewards": round(16.8 * multiplier, 2),
|
| 156 |
+
"rate": "6%"
|
| 157 |
+
}
|
| 158 |
+
],
|
| 159 |
+
"total_monthly_spend": round(2205 * multiplier, 2),
|
| 160 |
+
"total_monthly_rewards": round(108.18 * multiplier, 2),
|
| 161 |
+
"average_rate": round(4.9 * multiplier, 2),
|
| 162 |
+
"top_categories": [
|
| 163 |
+
{"name": "βοΈ Travel", "amount": round(850 * multiplier, 2), "change": "+45%"},
|
| 164 |
+
{"name": "π Groceries", "amount": round(450 * multiplier, 2), "change": "+12%"},
|
| 165 |
+
{"name": "π½οΈ Restaurants", "amount": round(320 * multiplier, 2), "change": "-5%"}
|
| 166 |
+
],
|
| 167 |
+
"ytd_rewards": round(1298.16 * multiplier, 2),
|
| 168 |
+
"ytd_potential": round(1640 * multiplier, 2),
|
| 169 |
+
"money_left": round(341.84 * multiplier, 2),
|
| 170 |
+
"forecast": {
|
| 171 |
+
"next_month_spend": round(2350 * multiplier, 2),
|
| 172 |
+
"next_month_rewards": round(115.25 * multiplier, 2),
|
| 173 |
+
"cards_to_watch": ["Amex Gold (dining cap)", "Freedom (quarterly bonus)"]
|
| 174 |
+
},
|
| 175 |
+
"recommendations": [
|
| 176 |
+
"π³ Use Chase Freedom for groceries in Q4 (5% back)",
|
| 177 |
+
"β οΈ Monitor Amex Gold dining spend (cap at $2,000)",
|
| 178 |
+
"π― Book holiday travel with Sapphire Reserve for 5x points"
|
| 179 |
+
]
|
| 180 |
+
}
|