Skip to content

Documentation for ToMeDa

tomeda.repository.http_handler

This module provides a wrapper for making HTTP requests using the requests library. It includes error handling for common HTTP error status codes.

logger module-attribute

logger: TraceLogger = getLogger(__name__)

TomedaHttpHandler

TomedaHttpHandler(
    timeout: float | tuple[float, float] = (10, 10)
)

A class used to make HTTP requests with error handling.

Attributes:

Name Type Description
REQUEST_ATTEMPTS int

The maximum number of attempts to make for a single HTTP request in case of a connection timeout.

timeout float | tuple[float, float]

The timeout duration in seconds for the HTTP requests. This can be a single float, in which case the same timeout applies to both the connection and the data transfer, or a tuple of two floats, specifying separate timeouts for the connection and data transfer.

Constructs a new TomedaHttpHandler object.

Parameters:

Name Type Description Default
timeout float | tuple[float, float]

The timeout duration in seconds for the HTTP requests. This can be a single float, in which case the same timeout applies to both the connection and the data transfer, or a tuple of two floats, specifying separate timeouts for the connection and data transfer.

(10, 10)
Source code in tomeda/repository/http_handler.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def __init__(self, timeout: float | tuple[float, float] = (10, 10)) -> None:
    """
    Constructs a new `TomedaHttpHandler` object.

    Parameters
    ----------
    timeout : float | tuple[float, float], optional
        The timeout duration in seconds for the HTTP requests. This can be
        a single float, in which case the same timeout applies to both the
        connection and the data transfer, or a tuple of two floats,
        specifying separate timeouts for the connection and data transfer.

    """
    self.timeout = timeout

REQUEST_ATTEMPTS class-attribute instance-attribute

REQUEST_ATTEMPTS = 3

timeout instance-attribute

timeout = timeout

get

get(
    url: str,
    parameter: dict[str, str] | None = None,
    header: dict[str, str] | None = None,
) -> requests.Response

Makes a GET request to a specified URL.

Parameters:

Name Type Description Default
url str

The URL to send the GET request to.

required
parameter dict

A dictionary of URL parameters to append to the URL.

None
header dict

A dictionary of HTTP headers to send with the request.

None

Returns:

Type Description
Response

The response from the server.

Raises:

Type Description
TomedaHttpUnauthorizedError

If the server returns a 401 Unauthorized status code.

TomedaHttpException

If the server returns a status code that indicates an error (>= 400).

TomedaHttpTimeoutError

If the request times out after REQUEST_ATTEMPTS attempts.

Source code in tomeda/repository/http_handler.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def get(
    self,
    url: str,
    parameter: dict[str, str] | None = None,
    header: dict[str, str] | None = None,
) -> requests.Response:
    """
    Makes a GET request to a specified URL.

    Parameters
    ----------
    url : str
        The URL to send the GET request to.
    parameter : dict, optional
        A dictionary of URL parameters to append to the URL.
    header : dict, optional
        A dictionary of HTTP headers to send with the request.

    Returns
    -------
    requests.Response
        The response from the server.

    Raises
    ------
    TomedaHttpUnauthorizedError
        If the server returns a 401 Unauthorized status code.
    TomedaHttpException
        If the server returns a status code that indicates an error
        (>= 400).
    TomedaHttpTimeoutError
        If the request times out after `REQUEST_ATTEMPTS` attempts.

    """
    logger.trace("GET with url '%s'", url)
    return self._request(
        "GET",
        url,
        parameter=parameter,
        header=header,
        data=None,
        json=None,
        files=None,
    )

post

post(
    url: str,
    parameter: dict[str, str] | None = None,
    header: dict[str, str] | None = None,
    data: Any = None,
    json: Any = None,
    files: Any = None,
) -> requests.Response

Makes a POST request to a specified URL.

Parameters:

Name Type Description Default
url str

The URL to send the GET request to.

required
parameter dict

A dictionary of URL parameters to append to the URL.

None
header dict

A dictionary of HTTP headers to send with the request.

None
data Any

The data to send in the body of the request. This may be a dictionary, a list of tuples, bytes, or a file-like object. If a dictionary or list of tuples is provided, it will be form-encoded.

None
json Any

The JSON-serializable Python object to send in the body of the request. If provided, it will be serialized to JSON and the Content-Type header of the request will be set to 'application/json'.

None
files Any

The files to send in the body of the request. This should be a dictionary, where each key-value pair represents a field name and a file-like object (including a file object or a BytesIO object).

None

Returns:

Type Description
Response

The response from the server.

Raises:

Type Description
TomedaHttpUnauthorizedError

If the server returns a 401 Unauthorized status code.

TomedaHttpException

If the server returns a status code that indicates an error (>= 400).

TomedaHttpTimeoutError

If the request times out after REQUEST_ATTEMPTS attempts.

Source code in tomeda/repository/http_handler.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def post(
    self,
    url: str,
    parameter: dict[str, str] | None = None,
    header: dict[str, str] | None = None,
    data: Any = None,
    json: Any = None,
    files: Any = None,
) -> requests.Response:
    """
    Makes a POST request to a specified URL.

    Parameters
    ----------
    url : str
        The URL to send the GET request to.
    parameter : dict, optional
        A dictionary of URL parameters to append to the URL.
    header : dict, optional
        A dictionary of HTTP headers to send with the request.
    data : Any, optional
        The data to send in the body of the request. This may be a
        dictionary, a list of tuples, bytes, or a file-like object.
        If a dictionary or list of tuples is provided, it will be
        form-encoded.
    json : Any, optional
        The JSON-serializable Python object to send in the body of the
        request. If provided, it will be serialized to JSON and the
        Content-Type header of the request will be set to
        'application/json'.
    files : Any, optional
        The files to send in the body of the request. This should be
        a dictionary, where each key-value pair represents a field
        name and a file-like object (including a file object or a
        BytesIO object).

    Returns
    -------
    requests.Response
        The response from the server.

    Raises
    ------
    TomedaHttpUnauthorizedError
        If the server returns a 401 Unauthorized status code.
    TomedaHttpException
        If the server returns a status code that indicates an error
        (>= 400).
    TomedaHttpTimeoutError
        If the request times out after `REQUEST_ATTEMPTS` attempts.

    """
    logger.trace("POST with url '%s'", url)
    return self._request(
        "POST",
        url,
        parameter=parameter,
        header=header,
        data=data,
        json=json,
        files=files,
    )

put

put(
    url: str,
    parameter: dict[str, str] | None = None,
    header: dict[str, str] | None = None,
    data: Any = None,
    json: Any = None,
    files: Any = None,
) -> requests.Response

Makes a PUT request to a specified URL.

Parameters:

Name Type Description Default
url str

The URL to send the GET request to.

required
parameter dict

A dictionary of URL parameters to append to the URL.

None
header dict

A dictionary of HTTP headers to send with the request.

None
data Any

The data to send in the body of the request. This may be a dictionary, a list of tuples, bytes, or a file-like object. If a dictionary or list of tuples is provided, it will be form-encoded.

None
json Any

The JSON-serializable Python object to send in the body of the request. If provided, it will be serialized to JSON and the Content-Type header of the request will be set to 'application/json'.

None
files Any

The files to send in the body of the request. This should be a dictionary, where each key-value pair represents a field name and a file-like object (including a file object or a BytesIO object).

None

Returns:

Type Description
Response

The response from the server.

Raises:

Type Description
TomedaHttpUnauthorizedError

If the server returns a 401 Unauthorized status code.

TomedaHttpException

If the server returns a status code that indicates an error (>= 400).

TomedaHttpTimeoutError

If the request times out after REQUEST_ATTEMPTS attempts.

Source code in tomeda/repository/http_handler.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
def put(
    self,
    url: str,
    parameter: dict[str, str] | None = None,
    header: dict[str, str] | None = None,
    data: Any = None,
    json: Any = None,
    files: Any = None,
) -> requests.Response:
    """
    Makes a PUT request to a specified URL.

    Parameters
    ----------
    url : str
        The URL to send the GET request to.
    parameter : dict, optional
        A dictionary of URL parameters to append to the URL.
    header : dict, optional
        A dictionary of HTTP headers to send with the request.
    data : Any, optional
        The data to send in the body of the request. This may be a
        dictionary, a list of tuples, bytes, or a file-like object.
        If a dictionary or list of tuples is provided, it will be
        form-encoded.
    json : Any, optional
        The JSON-serializable Python object to send in the body of the
        request. If provided, it will be serialized to JSON and the
        Content-Type header of the request will be set to
        'application/json'.
    files : Any, optional
        The files to send in the body of the request. This should be
        a dictionary, where each key-value pair represents a field
        name and a file-like object (including a file object or a
        BytesIO object).

    Returns
    -------
    requests.Response
        The response from the server.

    Raises
    ------
    TomedaHttpUnauthorizedError
        If the server returns a 401 Unauthorized status code.
    TomedaHttpException
        If the server returns a status code that indicates an error
        (>= 400).
    TomedaHttpTimeoutError
        If the request times out after `REQUEST_ATTEMPTS` attempts.

    """
    logger.trace("PUT with url '%s'", url)
    return self._request(
        "PUT",
        url,
        parameter=parameter,
        header=header,
        data=data,
        json=json,
        files=files,
    )