Skip to content

Call endpoints

call_endpoint(api_route, parameters)

Makes a POST request to a specified API endpoint.

This function sends a POST request to the given RAG endpoint with the provided parameters and returns the JSON response.

Parameters:

Name Type Description Default
api_route str

The API route to which the POST request is sent.

required
parameters dict

The parameters to be included in the POST request body.

required

Returns:

Name Type Description
dict dict

The JSON response from the API.

Raises:

Type Description
RequestException

If an error occurs during the HTTP request,

Source code in view/streamlit_app/call_endpoints.py
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
def call_endpoint(api_route: str, parameters: dict) -> dict:
    """
    Makes a POST request to a specified API endpoint.

    This function sends a POST request to the given RAG endpoint with the provided parameters
    and returns the JSON response.

    Args:
        api_route (str): The API route to which the POST request is sent.
        parameters (dict): The parameters to be included in the POST request body.

    Returns:
        dict: The JSON response from the API.

    Raises:
        requests.exceptions.RequestException: If an error occurs during the HTTP request,
        the exception is logged and re-raised.
    """
    url = f'http://{API_HOST}:{API_PORT}{api_route}'
    logger.info(f'Sending POST request to {url}')
    try:
        response = requests.post(url, json=parameters)
        response.raise_for_status()
        response_json = response.json()
        logger.debug(f'Received response: {response_json}')
        return response_json
    except requests.exceptions.RequestException as e:
        logger.error(f'Request error to {url}: {e}')
        raise

upload_endpoint(headers, files, params)

Makes a POST request to upload PDF files to a specified API endpoint.

This function sends a file upload request to the /upload endpoint with the provided headers, files, and parameters. It returns the JSON response from the API.

Parameters:

Name Type Description Default
headers dict

Headers to include in the POST request, such as authentication tokens.

required
files dict

Files to upload, with keys representing the field names and values as file objects.

required
params dict

Additional parameters to include in the request query string.

required

Returns:

Name Type Description
dict dict

The JSON response from the API.

Raises:

Type Description
RequestException

If an error occurs during the HTTP request,

Source code in view/streamlit_app/call_endpoints.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def upload_endpoint(headers: dict, files: dict, params: dict) -> dict:
    """
    Makes a POST request to upload PDF files to a specified API endpoint.

    This function sends a file upload request to the `/upload` endpoint with the provided headers,
    files, and parameters. It returns the JSON response from the API.

    Args:
        headers (dict): Headers to include in the POST request, such as authentication tokens.
        files (dict): Files to upload, with keys representing the field names and values as file objects.
        params (dict): Additional parameters to include in the request query string.

    Returns:
        dict: The JSON response from the API.

    Raises:
        requests.exceptions.RequestException: If an error occurs during the HTTP request,
        the exception is logged and re-raised.
    """
    url = f'http://{API_HOST}:{API_PORT}/upload'
    logger.info(f'Sending POST request to {url}')
    try:
        response = requests.post(url, headers=headers, files=files, params=params)
        response.raise_for_status()
        response_json = response.json()
        logger.debug(f'Received response: {response_json}')
        return response_json
    except requests.exceptions.RequestException as e:
        logger.error(f'Request error to {url}: {e}')
        raise