chg: Use REST search for the tags

Related to comments on a1326f2cf2
pull/660/head
Raphaël Vinot 2020-11-05 16:51:41 +01:00
parent 56ff22228c
commit 70de680912
2 changed files with 11 additions and 13 deletions

View File

@ -881,21 +881,19 @@ class PyMISP:
response = self._prepare_request('POST', f'tags/delete/{tag_id}') response = self._prepare_request('POST', f'tags/delete/{tag_id}')
return self._check_json_response(response) return self._check_json_response(response)
def search_tags(self, tag_name: str, pythonify: bool = False) -> Union[Dict, List[MISPTag]]: def search_tags(self, tagname: str, strict_tagname: bool = False, pythonify: bool = False) -> Union[Dict, List[MISPTag]]:
"""Search for tags by name. Matches substrings (no '%' is required). """Search for tags by name.
In the response, each tag has key 'count' with the number of tagged events
and key 'attribute_count' with the number of tagged attributes.
:param tag_name: Name (can be a part of it) to search :param tag_name: Name to search, use % for substrings matches.
:param strict_tagname: only return tags matching exactly the tag name (so skipping synonyms and cluster's value)
""" """
r = self._prepare_request('GET', f'tags/index/searchall:{tag_name}') query = {'tagname': tagname, 'strict_tagname': strict_tagname}
tag_r = self._check_json_response(r) response = self._prepare_request('POST', 'tags/search', data=query)
if 'errors' in tag_r: normalized_response = self._check_json_response(response)
return tag_r if not (self.global_pythonify or pythonify) or 'errors' in normalized_response:
if not (self.global_pythonify or pythonify): return normalized_response
return tag_r['Tag']
to_return: List[MISPTag] = [] to_return: List[MISPTag] = []
for tag in tag_r['Tag']: for tag in normalized_response:
t = MISPTag() t = MISPTag()
t.from_dict(**tag) t.from_dict(**tag)
to_return.append(t) to_return.append(t)

View File

@ -1340,7 +1340,7 @@ class TestComprehensive(unittest.TestCase):
# Search tag # Search tag
# Partial search # Partial search
tags = self.admin_misp_connector.search_tags(new_tag.name[:5], pythonify=True) tags = self.admin_misp_connector.search_tags(f'{new_tag.name[:5]}%', pythonify=True)
self.assertEqual(tags[0].name, 'this is a test tag') self.assertEqual(tags[0].name, 'this is a test tag')
# No tags found # No tags found
tags = self.admin_misp_connector.search_tags('not a tag') tags = self.admin_misp_connector.search_tags('not a tag')