|
6 | 6 | from RequestsLibrary import log
|
7 | 7 | from RequestsLibrary.compat import urljoin
|
8 | 8 | from RequestsLibrary.utils import (
|
| 9 | + is_list_or_tuple, |
9 | 10 | is_file_descriptor,
|
10 | 11 | warn_if_equal_symbol_in_url_session_less,
|
11 | 12 | )
|
@@ -48,14 +49,29 @@ def _common_request(self, method, session, uri, **kwargs):
|
48 | 49 |
|
49 | 50 | files = kwargs.get("files", {}) or {}
|
50 | 51 | data = kwargs.get("data", []) or []
|
51 |
| - files_descriptor_to_close = filter( |
52 |
| - is_file_descriptor, list(files.values()) + [data] |
53 |
| - ) |
54 |
| - for file_descriptor in files_descriptor_to_close: |
55 |
| - file_descriptor.close() |
| 52 | + |
| 53 | + self._close_file_descriptors(files, data) |
56 | 54 |
|
57 | 55 | return resp
|
58 | 56 |
|
| 57 | + @staticmethod |
| 58 | + def _close_file_descriptors(files, data): |
| 59 | + """ |
| 60 | + Helper method that closes any open file descriptors. |
| 61 | + """ |
| 62 | + |
| 63 | + if is_list_or_tuple(files): |
| 64 | + files_descriptor_to_close = filter( |
| 65 | + is_file_descriptor, [file[1][1] for file in files] + [data] |
| 66 | + ) |
| 67 | + else: |
| 68 | + files_descriptor_to_close = filter( |
| 69 | + is_file_descriptor, list(files.values()) + [data] |
| 70 | + ) |
| 71 | + |
| 72 | + for file_descriptor in files_descriptor_to_close: |
| 73 | + file_descriptor.close() |
| 74 | + |
59 | 75 | @staticmethod
|
60 | 76 | def _merge_url(session, uri):
|
61 | 77 | """
|
@@ -176,7 +192,7 @@ def session_less_get(
|
176 | 192 | | ``json`` | A JSON serializable Python object to send in the body of the request. |
|
177 | 193 | | ``headers`` | Dictionary of HTTP Headers to send with the request. |
|
178 | 194 | | ``cookies`` | Dict or CookieJar object to send with the request. |
|
179 |
| - | ``files`` | Dictionary of file-like-objects (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. | |
| 195 | + | ``files`` | Dictionary of file-like-objects (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. List or tuple of ``('key': file-tuple)`` allows uploading multiple files with the same key, resulting in a list of files on the receiving end. | |
180 | 196 | | ``auth`` | Auth tuple to enable Basic/Digest/Custom HTTP Auth. |
|
181 | 197 | | ``timeout`` | How many seconds to wait for the server to send data before giving up, as a float, or a ``(connect timeout, read timeout)`` tuple. |
|
182 | 198 | | ``allow_redirects`` | Boolean. Enable/disable (values ``${True}`` or ``${False}``). Only for HEAD method keywords allow_redirection defaults to ``${False}``, all others ``${True}``. |
|
|
0 commit comments