MakPr016 commited on
Commit
da3515a
·
1 Parent(s): 81931f5

Updated API

Browse files
Files changed (1) hide show
  1. main.py +5 -18
main.py CHANGED
@@ -23,7 +23,6 @@ app.add_middleware(
23
  allow_headers=["*"],
24
  )
25
 
26
- # Fallback rates in case API fails
27
  EXCHANGE_RATES = {
28
  "USD": 1.0,
29
  "EUR": 1.08,
@@ -40,16 +39,13 @@ EXCHANGE_RATES = {
40
  def fetch_live_rates():
41
  global EXCHANGE_RATES
42
  try:
43
- # Using a free standard API for USD base rates
44
- response = requests.get("https://api.exchangerate-api.com/v4/latest/USD")
45
  data = response.json()
46
- if "rates" in data:
47
- EXCHANGE_RATES = data["rates"]
48
- # Ensure USD is 1.0
49
  EXCHANGE_RATES["USD"] = 1.0
50
- print("Successfully fetched live exchange rates.")
51
- except Exception as e:
52
- print(f"Failed to fetch live rates, using fallback. Error: {e}")
53
 
54
  @app.on_event("startup")
55
  async def startup_event():
@@ -59,15 +55,9 @@ def get_exchange_rate(currency_code: str) -> float:
59
  return EXCHANGE_RATES.get(currency_code.upper(), 1.0)
60
 
61
  def convert_price(price: float, from_curr: str, to_curr: str) -> float:
62
- # If currencies are the same, return original price
63
  if from_curr.upper() == to_curr.upper():
64
  return price
65
 
66
- # Logic: Convert 'from' to USD, then USD to 'to'
67
- # API returns rates where 1 USD = X Currency
68
- # Price in USD = Price / Rate(from)
69
- # Price in Target = Price in USD * Rate(to)
70
-
71
  rate_from = get_exchange_rate(from_curr)
72
  rate_to = get_exchange_rate(to_curr)
73
 
@@ -110,7 +100,6 @@ async def analyze_rfq(rfq_id: str, target_currency: str = Query("USD")):
110
 
111
  for q in quotes_res.data:
112
  v_id = q['vendor_id']
113
-
114
  meta = q.get('metadata') or {}
115
  logistics = meta.get('logistics') or {}
116
  currency = logistics.get('currency', 'USD')
@@ -132,7 +121,6 @@ async def analyze_rfq(rfq_id: str, target_currency: str = Query("USD")):
132
  q_id = qi['quotation_id']
133
  if q_id in q_to_v:
134
  v_id = q_to_v[q_id]
135
-
136
  source_curr = quote_currency_map.get(q_id, "USD")
137
  original_price = qi['unit_price']
138
  converted_price = convert_price(original_price, source_curr, target_currency)
@@ -146,7 +134,6 @@ async def analyze_rfq(rfq_id: str, target_currency: str = Query("USD")):
146
  ))
147
 
148
  vendor_offers = list(vendor_map.values())
149
-
150
  optimizer = VendorOptimizer(rfq_items, vendor_offers)
151
  results = optimizer.run_all()
152
 
 
23
  allow_headers=["*"],
24
  )
25
 
 
26
  EXCHANGE_RATES = {
27
  "USD": 1.0,
28
  "EUR": 1.08,
 
39
  def fetch_live_rates():
40
  global EXCHANGE_RATES
41
  try:
42
+ response = requests.get("https://v6.exchangerate-api.com/v6/13d7597c5aae8f8a1c96e25d/latest/USD")
 
43
  data = response.json()
44
+ if data.get("result") == "success":
45
+ EXCHANGE_RATES = data["conversion_rates"]
 
46
  EXCHANGE_RATES["USD"] = 1.0
47
+ except Exception:
48
+ pass
 
49
 
50
  @app.on_event("startup")
51
  async def startup_event():
 
55
  return EXCHANGE_RATES.get(currency_code.upper(), 1.0)
56
 
57
  def convert_price(price: float, from_curr: str, to_curr: str) -> float:
 
58
  if from_curr.upper() == to_curr.upper():
59
  return price
60
 
 
 
 
 
 
61
  rate_from = get_exchange_rate(from_curr)
62
  rate_to = get_exchange_rate(to_curr)
63
 
 
100
 
101
  for q in quotes_res.data:
102
  v_id = q['vendor_id']
 
103
  meta = q.get('metadata') or {}
104
  logistics = meta.get('logistics') or {}
105
  currency = logistics.get('currency', 'USD')
 
121
  q_id = qi['quotation_id']
122
  if q_id in q_to_v:
123
  v_id = q_to_v[q_id]
 
124
  source_curr = quote_currency_map.get(q_id, "USD")
125
  original_price = qi['unit_price']
126
  converted_price = convert_price(original_price, source_curr, target_currency)
 
134
  ))
135
 
136
  vendor_offers = list(vendor_map.values())
 
137
  optimizer = VendorOptimizer(rfq_items, vendor_offers)
138
  results = optimizer.run_all()
139