File size: 1,435 Bytes
0926cd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e0fda9
0926cd3
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"
    }