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,13 +3710,15 @@ 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
d = data if data is not None:
elif data: if isinstance(data, bytes):
if isinstance(data, dict): # Else, we can directly json encode. d = data
# Remove None values. else:
data = {k: v for k, v in data.items() if v is not None} if isinstance(data, dict):
d = json.dumps(data, default=pymisp_json_default) # Remove None values.
data = {k: v for k, v in data.items() if v is not None}
d = json.dumps(data, default=pymisp_json_default)
logger.debug(f'{request_type} - {url}') logger.debug(f'{request_type} - {url}')
if d is not None: if d is not None: