Skip to content

Requests

api_key_safe_raise_for_status(response)

Raise an exception if the request is not successful.

Parameters:

Name Type Description Default
response Response

The response of the request.

required
Source code in inference_sdk/http/utils/requests.py
11
12
13
14
15
16
17
18
19
20
21
def api_key_safe_raise_for_status(response: Response) -> None:
    """Raise an exception if the request is not successful.

    Args:
        response: The response of the request.
    """
    request_is_successful = response.status_code < 400
    if request_is_successful:
        return None
    response.url = deduct_api_key_from_string(value=response.url)
    response.raise_for_status()

deduct_api_key(match)

Deduct the API key from the string.

Parameters:

Name Type Description Default
match Match

The match of the API key.

required

Returns:

Type Description
str

The string with the API key deducted.

Source code in inference_sdk/http/utils/requests.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def deduct_api_key(match: re.Match) -> str:
    """Deduct the API key from the string.

    Args:
        match: The match of the API key.

    Returns:
        The string with the API key deducted.
    """
    key_value = match.group(KEY_VALUE_GROUP)
    if len(key_value) < MIN_KEY_LENGTH_TO_REVEAL_PREFIX:
        return f"api_key=***"
    key_prefix = key_value[:2]
    key_postfix = key_value[-2:]
    return f"api_key={key_prefix}***{key_postfix}"

deduct_api_key_from_string(value)

Deduct the API key from the string.

Parameters:

Name Type Description Default
value str

The string to deduct the API key from.

required

Returns:

Type Description
str

The string with the API key deducted.

Source code in inference_sdk/http/utils/requests.py
24
25
26
27
28
29
30
31
32
33
def deduct_api_key_from_string(value: str) -> str:
    """Deduct the API key from the string.

    Args:
        value: The string to deduct the API key from.

    Returns:
        The string with the API key deducted.
    """
    return API_KEY_PATTERN.sub(deduct_api_key, value)

inject_images_into_payload(payload, encoded_images, key='image')

Inject images into the payload.

Parameters:

Name Type Description Default
payload dict

The payload to inject the images into.

required
encoded_images List[Tuple[str, Optional[float]]]

The encoded images.

required
key str

The key of the images.

'image'

Returns:

Type Description
dict

The payload with the images injected.

Source code in inference_sdk/http/utils/requests.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def inject_images_into_payload(
    payload: dict,
    encoded_images: List[Tuple[str, Optional[float]]],
    key: str = "image",
) -> dict:
    """Inject images into the payload.

    Args:
        payload: The payload to inject the images into.
        encoded_images: The encoded images.
        key: The key of the images.

    Returns:
        The payload with the images injected.
    """
    if len(encoded_images) == 0:
        return payload
    if len(encoded_images) > 1:
        images_payload = [
            {"type": "base64", "value": image} for image, _ in encoded_images
        ]
        payload[key] = images_payload
    else:
        payload[key] = {"type": "base64", "value": encoded_images[0][0]}
    return payload

inject_nested_batches_of_images_into_payload(payload, encoded_images, key='image')

Inject nested batches of images into the payload.

Parameters:

Name Type Description Default
payload dict

The payload to inject the images into.

required
encoded_images Union[list, Tuple[str, Optional[float]]]

The encoded images.

required
key str

The key of the images.

'image'

Returns:

Type Description
dict

The payload with the images injected.

Source code in inference_sdk/http/utils/requests.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def inject_nested_batches_of_images_into_payload(
    payload: dict,
    encoded_images: Union[list, Tuple[str, Optional[float]]],
    key: str = "image",
) -> dict:
    """Inject nested batches of images into the payload.

    Args:
        payload: The payload to inject the images into.
        encoded_images: The encoded images.
        key: The key of the images.

    Returns:
        The payload with the images injected.
    """
    payload_value = _batch_of_images_into_inference_format(
        encoded_images=encoded_images,
    )
    payload[key] = payload_value
    return payload