fix: Avoid exception when data is an empty iterator

Fix #1053
pull/1054/head
Raphaël Vinot 2023-09-04 17:33:25 +02:00
parent 69e660ef03
commit 0a63ab8cc8
1 changed files with 12 additions and 9 deletions

View File

@ -3699,8 +3699,9 @@ class PyMISP:
def __repr__(self): def __repr__(self):
return f'<{self.__class__.__name__}(url={self.root_url})' return f'<{self.__class__.__name__}(url={self.root_url})'
def _prepare_request(self, request_type: str, url: str, data: Union[Iterable, Mapping, AbstractMISP, bytes] = {}, params: Mapping = {}, def _prepare_request(self, request_type: str, url: str, data: Optional[Union[Iterable, Mapping, AbstractMISP, bytes]] = None,
kw_params: Mapping = {}, output_type: str = 'json', content_type: str = 'json') -> requests.Response: params: Mapping = {}, kw_params: Mapping = {},
output_type: str = 'json', content_type: str = 'json') -> requests.Response:
'''Prepare a request for python-requests''' '''Prepare a request for python-requests'''
if url[0] == '/': if url[0] == '/':
# strip it: it will fail if MISP is in a sub directory # strip it: it will fail if MISP is in a sub directory
@ -3709,10 +3710,12 @@ class PyMISP:
# so we need to make it a + instead and hope for the best # so we need to make it a + instead and hope for the best
url = url.replace(' ', '+') url = url.replace(' ', '+')
url = urljoin(self.root_url, url) url = urljoin(self.root_url, url)
if data == {} or isinstance(data, bytes): d: Optional[Union[bytes, str]] = None
if data is not None:
if isinstance(data, bytes):
d = data d = data
elif data: else:
if isinstance(data, dict): # Else, we can directly json encode. if isinstance(data, dict):
# Remove None values. # Remove None values.
data = {k: v for k, v in data.items() if v is not None} data = {k: v for k, v in data.items() if v is not None}
d = json.dumps(data, default=pymisp_json_default) d = json.dumps(data, default=pymisp_json_default)