Skip to content

Commit

Permalink
chg: Use REST search for the tags
Browse files Browse the repository at this point in the history
Related to comments on a1326f2
  • Loading branch information
Rafiot committed Nov 5, 2020
1 parent 56ff222 commit 70de680
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
22 changes: 10 additions & 12 deletions pymisp/api.py
Expand Up @@ -881,21 +881,19 @@ def delete_tag(self, tag: Union[MISPTag, int, str, UUID]) -> Dict:
response = self._prepare_request('POST', f'tags/delete/{tag_id}')
return self._check_json_response(response)

def search_tags(self, tag_name: str, pythonify: bool = False) -> Union[Dict, List[MISPTag]]:
"""Search for tags by name. Matches substrings (no '%' is required).
In the response, each tag has key 'count' with the number of tagged events
and key 'attribute_count' with the number of tagged attributes.
def search_tags(self, tagname: str, strict_tagname: bool = False, pythonify: bool = False) -> Union[Dict, List[MISPTag]]:
"""Search for tags by name.
: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}')
tag_r = self._check_json_response(r)
if 'errors' in tag_r:
return tag_r
if not (self.global_pythonify or pythonify):
return tag_r['Tag']
query = {'tagname': tagname, 'strict_tagname': strict_tagname}
response = self._prepare_request('POST', 'tags/search', data=query)
normalized_response = self._check_json_response(response)
if not (self.global_pythonify or pythonify) or 'errors' in normalized_response:
return normalized_response
to_return: List[MISPTag] = []
for tag in tag_r['Tag']:
for tag in normalized_response:
t = MISPTag()
t.from_dict(**tag)
to_return.append(t)
Expand Down
2 changes: 1 addition & 1 deletion tests/testlive_comprehensive.py
Expand Up @@ -1340,7 +1340,7 @@ def test_tags(self):

# Search tag
# 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')
# No tags found
tags = self.admin_misp_connector.search_tags('not a tag')
Expand Down

0 comments on commit 70de680

Please sign in to comment.