comic-grading / inout.py
umang-immersfy's picture
regeneratio added, WIP
6e0fda9
from typing import Any
from typing import Dict
from typing import List
from typing import Tuple
import parameters
import requests
def send_post_request(url: str, payload: dict) -> Tuple[Dict[str, Any], int]:
"""
Sends a POST request to the given URL with the provided payload.
Args:
url (str): The target URL.
payload (dict): The JSON payload to send.
Returns:
Tuple[Dict[str, Any], int]: The JSON response and status code from the request.
"""
response = requests.post(url, json=payload)
return response.json(), response.status_code
def get_valid_post_response(url: str, payload: dict) -> Dict[str, Any]:
"""
Assures a response by wrapping up the request sending utility in a for loop with maximum retries.
Args:
url (str): The target URL.
payload (dict): The JSON payload to send.
Returns:
Dict[str, Any]: The JSON response from the request.
"""
for _ in range(int(parameters.MAX_TRIES)):
try:
response, status_code = send_post_request(url, payload)
if status_code != 200:
continue
return response
except:
continue
print(
f"Max retries exceeded with POST request for url: {url} with payload:\n{payload}\n"
)
return {
"error": f"Max retries exceeded with POST request for url: {url} with payload:\n{payload}\n"
}