Merge pull request #1134 from JakubOnderka/orjson

orjson and small fixes and optimizations
pull/1135/head
Raphaël Vinot 2024-01-17 13:43:21 +01:00 committed by GitHub
commit d8850c59ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 12 deletions

View File

@ -250,8 +250,9 @@ class AbstractMISP(MutableMapping, MISPFileCache, metaclass=ABCMeta):
option |= orjson.OPT_SORT_KEYS
if indent:
option |= orjson.OPT_INDENT_2
return dumps(self, default=pymisp_json_default, option=option).decode("utf-8")
# orjson dumps method returns bytes instead of bytes, to keep compatibility with json
# we have to convert output to str
return str(dumps(self, default=pymisp_json_default, option=option))
return dumps(self, default=pymisp_json_default, sort_keys=sort_keys, indent=indent)

View File

@ -71,7 +71,7 @@ def get_uuid_or_id_from_abstract_misp(obj: AbstractMISP | int | str | UUID | dic
if isinstance(obj, (int, str)):
return obj
if isinstance(obj, dict) and len(obj.keys()) == 1:
if isinstance(obj, dict) and len(obj) == 1:
# We have an object in that format: {'Event': {'id': 2, ...}}
# We need to get the content of that dictionary
obj = obj[list(obj.keys())[0]]
@ -188,6 +188,7 @@ class PyMISP:
self.__session.headers['Accept-Encoding'] = ', '.join(('br', 'gzip', 'deflate'))
if http_headers:
self.__session.headers.update(http_headers)
self._user_agent = f'PyMISP {__version__} - Python {".".join(str(x) for x in sys.version_info[:2])}'
self.global_pythonify = False
@ -2624,10 +2625,10 @@ class PyMISP:
'''
return_formats = ['openioc', 'json', 'xml', 'suricata', 'snort', 'text', 'rpz', 'csv', 'cache', 'stix-xml',
'stix', 'stix2', 'yara', 'yara-json', 'attack', 'attack-sightings', 'context', 'context-markdown']
return_formats = ('openioc', 'json', 'xml', 'suricata', 'snort', 'text', 'rpz', 'csv', 'cache', 'stix-xml',
'stix', 'stix2', 'yara', 'yara-json', 'attack', 'attack-sightings', 'context', 'context-markdown')
if controller not in ['events', 'attributes', 'objects']:
if controller not in ('events', 'attributes', 'objects'):
raise ValueError('controller has to be in {}'.format(', '.join(['events', 'attributes', 'objects'])))
# Deprecated stuff / synonyms
@ -2998,7 +2999,7 @@ class PyMISP:
query.pop('pythonify')
if log_id is not None:
query['id'] = query.pop('log_id')
if created is not None and isinstance(created, (datetime)):
if created is not None and isinstance(created, datetime):
query['created'] = query.pop('created').timestamp()
response = self._prepare_request('POST', 'admin/logs/index', data=query)
@ -3351,7 +3352,7 @@ class PyMISP:
"""
query: dict[str, Any] = {'setting': user_setting}
if isinstance(value, dict):
value = dumps(value).decode("utf-8") if HAS_ORJSON else dumps(value)
value = str(dumps(value)) if HAS_ORJSON else dumps(value)
query['value'] = value
if user:
query['user_id'] = get_uuid_or_id_from_abstract_misp(user)
@ -3745,7 +3746,7 @@ class PyMISP:
data = {k: v for k, v in data.items() if v is not None}
d = dumps(data, default=pymisp_json_default)
logger.debug(f'{request_type} - {url}')
logger.debug('%s - %s', request_type, url)
if d is not None:
logger.debug(d)
@ -3755,9 +3756,7 @@ class PyMISP:
url = f'{url}/{to_append_url}'
req = requests.Request(request_type, url, data=d, params=params)
user_agent = f'PyMISP {__version__} - Python {".".join(str(x) for x in sys.version_info[:2])}'
if self.tool:
user_agent = f'{user_agent} - {self.tool}'
user_agent = f'{self._user_agent} - {self.tool}' if self.tool else self._user_agent
req.auth = self.auth
prepped = self.__session.prepare_request(req)
prepped.headers.update(