SamiKoen commited on
Commit
a05cc02
·
1 Parent(s): 8ba1576

API timeout için retry mekanizması - 3 deneme (10s, 15s, 20s)

Browse files
Files changed (2) hide show
  1. app.py +14 -4
  2. smart_warehouse.py +18 -9
app.py CHANGED
@@ -53,10 +53,20 @@ def get_warehouse_stock_old(product_name):
53
  import re
54
  import requests
55
 
56
- # Get XML
57
- url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
58
- response = requests.get(url, verify=False, timeout=7)
59
- xml_text = response.text
 
 
 
 
 
 
 
 
 
 
60
 
61
  # Turkish normalize
62
  def normalize(text):
 
53
  import re
54
  import requests
55
 
56
+ # Get XML with retry
57
+ xml_text = None
58
+ for attempt in range(3):
59
+ try:
60
+ url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
61
+ timeout_val = 10 + (attempt * 5) # 10, 15, 20 seconds
62
+ response = requests.get(url, verify=False, timeout=timeout_val)
63
+ xml_text = response.text
64
+ break
65
+ except requests.exceptions.Timeout:
66
+ if attempt == 2:
67
+ return None
68
+ except Exception:
69
+ return None
70
 
71
  # Turkish normalize
72
  def normalize(text):
smart_warehouse.py CHANGED
@@ -10,15 +10,24 @@ def get_warehouse_stock_smart(user_message):
10
 
11
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
 
13
- # Get XML data
14
- try:
15
- url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
16
- response = requests.get(url, verify=False, timeout=7)
17
- xml_text = response.text
18
- print(f"DEBUG - XML fetched: {len(xml_text)} characters")
19
- except Exception as e:
20
- print(f"XML fetch error: {e}")
21
- return None
 
 
 
 
 
 
 
 
 
22
 
23
  # Extract just product blocks to reduce token usage
24
  product_pattern = r'<Product>(.*?)</Product>'
 
10
 
11
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
 
13
+ # Get XML data with retry
14
+ xml_text = None
15
+ for attempt in range(3): # Try 3 times
16
+ try:
17
+ url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
18
+ timeout_val = 10 + (attempt * 5) # Increase timeout on each retry: 10, 15, 20
19
+ response = requests.get(url, verify=False, timeout=timeout_val)
20
+ xml_text = response.text
21
+ print(f"DEBUG - XML fetched: {len(xml_text)} characters (attempt {attempt+1})")
22
+ break
23
+ except requests.exceptions.Timeout:
24
+ print(f"XML fetch timeout (attempt {attempt+1}/3, timeout={timeout_val}s)")
25
+ if attempt == 2:
26
+ print("All attempts failed - timeout")
27
+ return None
28
+ except Exception as e:
29
+ print(f"XML fetch error: {e}")
30
+ return None
31
 
32
  # Extract just product blocks to reduce token usage
33
  product_pattern = r'<Product>(.*?)</Product>'