File size: 12,107 Bytes
112cabd f4d0231 112cabd ccb76e6 1363452 ccb76e6 112cabd ccb76e6 f4d0231 112cabd f4d0231 112cabd f4d0231 112cabd f4d0231 112cabd f4d0231 112cabd f4d0231 112cabd 11f1e55 112cabd 11f1e55 112cabd 11f1e55 f4d0231 112cabd f4d0231 11f1e55 112cabd 11f1e55 112cabd 11f1e55 112cabd 11f1e55 112cabd 11f1e55 112cabd 11f1e55 f4d0231 112cabd 1eec6c9 112cabd 1eec6c9 112cabd 1eec6c9 112cabd 11f1e55 1eec6c9 112cabd 1eec6c9 112cabd 1eec6c9 112cabd 1eec6c9 112cabd 889e5a0 112cabd 889e5a0 112cabd 889e5a0 112cabd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
"""
API Client for RewardPilot Orchestrator Service
"""
import requests
import logging
from typing import Dict, Any, Optional, Tuple, List
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class RewardPilotClient:
"""Client for interacting with RewardPilot microservices"""
def __init__(self, orchestrator_url: str = "http://localhost:8000"):
"""
Initialize API client
Args:
orchestrator_url: Base URL for orchestrator service
"""
self.orchestrator_url = orchestrator_url.rstrip('/')
self.timeout = 10 # seconds
def get_recommendation(
self,
user_id: str,
merchant: str,
category: str,
amount: float,
mcc: Optional[str] = None
) -> Dict:
"""
Get card recommendation for a transaction
Args:
user_id: User identifier
merchant: Merchant name
category: Transaction category (e.g., "Groceries", "Dining")
amount: Transaction amount in USD
mcc: Optional Merchant Category Code
Returns:
Dictionary with recommendation data
"""
# Map category to MCC if not provided
if not mcc:
mcc = self._category_to_mcc(category)
payload = {
"user_id": user_id,
"merchant": merchant,
"category": category,
"mcc": mcc,
"amount_usd": amount
}
try:
logger.info(f"π Calling: {self.orchestrator_url}/recommend")
logger.info(f"π¦ Payload: {payload}")
response = requests.post(
f"{self.orchestrator_url}/recommend",
json=payload,
timeout=self.timeout
)
logger.info(f"π‘ Response status: {response.status_code}")
if response.status_code == 200:
data = response.json()
logger.info(f"β
Got recommendation for {merchant}")
return {
"success": True,
"data": data
}
elif response.status_code == 404:
logger.warning(f"β οΈ Endpoint not found (404) - using mock data")
return self._get_mock_recommendation(user_id, merchant, category, amount)
else:
logger.error(f"β API error: {response.status_code} - using mock data")
return self._get_mock_recommendation(user_id, merchant, category, amount)
except requests.exceptions.Timeout:
logger.error("β Request timeout - using mock data")
return self._get_mock_recommendation(user_id, merchant, category, amount)
except requests.exceptions.ConnectionError:
logger.error("β Connection error - using mock data")
return self._get_mock_recommendation(user_id, merchant, category, amount)
except Exception as e:
logger.error(f"β Unexpected error: {e} - using mock data")
return self._get_mock_recommendation(user_id, merchant, category, amount)
def get_user_analytics(self, user_id: str) -> Dict:
"""
Get analytics for a user
Args:
user_id: User identifier
Returns:
Dictionary with analytics data
"""
logger.info(f"π Getting analytics for {user_id} (using mock data)")
# Always use mock data since orchestrator doesn't have analytics endpoint
return self._get_mock_analytics(user_id)
def compare_cards(self, card_ids: List[str]) -> Dict:
"""
Compare multiple credit cards
Args:
card_ids: List of card identifiers
Returns:
Dictionary with comparison data
"""
payload = {
"card_ids": card_ids
}
try:
response = requests.post(
f"{self.orchestrator_url}/compare",
json=payload,
timeout=self.timeout
)
if response.status_code == 200:
return {
"success": True,
"data": response.json()
}
else:
return {
"success": False,
"error": f"API returned status {response.status_code}"
}
except Exception as e:
logger.error(f"β Error comparing cards: {e}")
return {
"success": False,
"error": str(e)
}
def _category_to_mcc(self, category: str) -> str:
"""Map category name to MCC code"""
category_map = {
"Groceries": "5411",
"Dining": "5812",
"Travel": "4511",
"Gas": "5541",
"Online Shopping": "5999",
"Entertainment": "7832",
"Pharmacy": "5912",
"Department Store": "5311",
"Home Improvement": "5211",
"Utilities": "4900"
}
return category_map.get(category, "0000")
def _get_mock_recommendation(
self,
user_id: str,
merchant: str,
category: str,
amount: float
) -> Dict:
"""
Generate mock recommendation for demo/development
This is used when the orchestrator service is unavailable
"""
# Simple rule-based mock logic
card_rules = {
"Groceries": {
"card": "Amex Gold",
"rate": "4x points",
"multiplier": 4.0
},
"Dining": {
"card": "Capital One Savor",
"rate": "4% cashback",
"multiplier": 4.0
},
"Travel": {
"card": "Chase Sapphire Reserve",
"rate": "3x points",
"multiplier": 3.0
},
"Gas": {
"card": "Costco Visa",
"rate": "4% cashback",
"multiplier": 4.0
},
"Online Shopping": {
"card": "Amazon Prime Card",
"rate": "5% cashback",
"multiplier": 5.0
}
}
rule = card_rules.get(category, {
"card": "Citi Double Cash",
"rate": "2% cashback",
"multiplier": 2.0
})
rewards_earned = amount * (rule["multiplier"] / 100)
annual_potential = rewards_earned * 12 # Rough estimate
logger.warning(f"β οΈ Using mock data for {merchant}")
return {
"success": True,
"data": {
"recommended_card": rule["card"],
"rewards_earned": round(rewards_earned, 2),
"rewards_rate": rule["rate"],
"merchant": merchant,
"category": category,
"amount": amount,
"annual_potential": round(annual_potential, 2),
"optimization_score": 85,
"reasoning": f"Best card for {category.lower()} purchases",
"warnings": [],
"alternatives": [
{
"card": "Citi Double Cash",
"rewards": round(amount * 0.02, 2),
"rate": "2% cashback"
}
],
"mock_data": True # Flag to indicate this is mock data
}
}
def _get_mock_analytics(self, user_id: str) -> Dict[str, Any]:
"""Generate user-specific mock analytics"""
# Different data for different users
user_profiles = {
"u_alice": {
"user_id": "u_alice",
"total_spending": 4250.75,
"total_rewards": 187.50,
"optimization_score": 92,
"optimized_count": 58,
"potential_savings": 125.00,
"spending_by_category": {
"Groceries": 1200.00,
"Restaurants": 850.50,
"Gas Stations": 420.25,
"Online Shopping": 1200.00,
"Entertainment": 580.00
},
"rewards_by_card": {
"Amex Gold": 95.50,
"Chase Sapphire Reserve": 62.00,
"Citi Double Cash": 30.00
},
"monthly_trends": [
{"month": "Aug", "spending": 1200, "rewards": 52},
{"month": "Sep", "spending": 1450, "rewards": 63},
{"month": "Oct", "spending": 1600, "rewards": 72}
]
},
"u_bob": {
"user_id": "u_bob",
"total_spending": 3150.25,
"total_rewards": 142.30,
"optimization_score": 78,
"optimized_count": 42,
"potential_savings": 285.50,
"spending_by_category": {
"Groceries": 800.00,
"Restaurants": 1200.00,
"Gas Stations": 350.00,
"Airlines": 600.00,
"Entertainment": 200.25
},
"rewards_by_card": {
"Chase Sapphire Reserve": 85.00,
"Amex Gold": 42.30,
"Capital One Venture": 15.00
},
"monthly_trends": [
{"month": "Aug", "spending": 950, "rewards": 38},
{"month": "Sep", "spending": 1100, "rewards": 52},
{"month": "Oct", "spending": 1100, "rewards": 52}
]
},
"u_charlie": {
"user_id": "u_charlie",
"total_spending": 5420.80,
"total_rewards": 245.60,
"optimization_score": 85,
"optimized_count": 67,
"potential_savings": 180.00,
"spending_by_category": {
"Online Shopping": 2000.00,
"Groceries": 1100.00,
"Restaurants": 950.00,
"Gas Stations": 520.80,
"Hotels": 850.00
},
"rewards_by_card": {
"Amex Gold": 125.00,
"Chase Sapphire Reserve": 95.60,
"Citi Double Cash": 25.00
},
"monthly_trends": [
{"month": "Aug", "spending": 1650, "rewards": 75},
{"month": "Sep", "spending": 1850, "rewards": 82},
{"month": "Oct", "spending": 1920, "rewards": 88}
]
}
}
# Get user-specific data or default to alice
user_data = user_profiles.get(user_id, user_profiles["u_alice"])
return {
"success": True,
"data": {
**user_data,
"mock_data": True
}
}
def health_check(self) -> bool:
"""
Check if orchestrator service is available
Returns:
True if service is healthy, False otherwise
"""
try:
response = requests.get(
f"{self.orchestrator_url}/health",
timeout=5
)
return response.status_code == 200
except:
return False
# Convenience function
def create_client(orchestrator_url: str = "http://localhost:8000") -> RewardPilotClient:
"""Create and return a RewardPilotClient instance"""
return RewardPilotClient(orchestrator_url) |