Dash-inc commited on
Commit
0d39048
·
verified ·
1 Parent(s): a8df78e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +26 -1
main.py CHANGED
@@ -113,10 +113,35 @@ def save_image_locally(image, filename):
113
  image.save(filepath, format="PNG")
114
  return filepath
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def fetch_image(url):
117
  try:
118
  with requests.Session() as session:
119
- response = session.get(url, timeout=10)
120
  response.raise_for_status()
121
  return Image.open(BytesIO(response.content))
122
  except Exception as e:
 
113
  image.save(filepath, format="PNG")
114
  return filepath
115
 
116
+
117
+ def make_request_with_retries(url, headers, payload, retries=3, delay=2):
118
+ """
119
+ Makes an HTTP POST request with retries in case of failure.
120
+ :param url: The URL for the request.
121
+ :param headers: Headers to include in the request.
122
+ :param payload: Payload to include in the request.
123
+ :param retries: Number of retries on failure.
124
+ :param delay: Delay between retries.
125
+ :return: Response JSON from the server.
126
+ """
127
+ for attempt in range(retries):
128
+ try:
129
+ with requests.Session() as session:
130
+ response = session.post(url, headers=headers, json=payload, timeout=30)
131
+ response.raise_for_status()
132
+ return response.json()
133
+ except requests.exceptions.RequestException as e:
134
+ if attempt < retries - 1:
135
+ time.sleep(delay)
136
+ continue
137
+ else:
138
+ raise HTTPException(status_code=500, detail=f"Request failed after {retries} attempts: {str(e)}")
139
+
140
+
141
  def fetch_image(url):
142
  try:
143
  with requests.Session() as session:
144
+ response = session.get(url, timeout=30)
145
  response.raise_for_status()
146
  return Image.open(BytesIO(response.content))
147
  except Exception as e: