new: Save one REST call when initialize PyMISP class

pull/779/head
Jakub Onderka 2021-06-28 12:57:07 +02:00 committed by Raphaël Vinot
parent 7f53761403
commit 76ce8d8c38
1 changed files with 16 additions and 2 deletions

View File

@ -28,6 +28,15 @@ from .mispevent import MISPEvent, MISPAttribute, MISPSighting, MISPLog, MISPObje
MISPGalaxyCluster, MISPGalaxyClusterRelation, MISPCorrelationExclusion MISPGalaxyCluster, MISPGalaxyClusterRelation, MISPCorrelationExclusion
from .abstract import pymisp_json_default, MISPTag, AbstractMISP, describe_types from .abstract import pymisp_json_default, MISPTag, AbstractMISP, describe_types
try:
# cached_property exists since Python 3.8
from functools import cached_property # type: ignore
except ImportError:
from functools import lru_cache
def cached_property(func): # type: ignore
return property(lru_cache()(func))
SearchType = TypeVar('SearchType', str, int) SearchType = TypeVar('SearchType', str, int)
# str: string to search / list: values to search (OR) / dict: {'OR': [list], 'NOT': [list], 'AND': [list]} # str: string to search / list: values to search (OR) / dict: {'OR': [list], 'NOT': [list], 'AND': [list]}
SearchParameterTypes = TypeVar('SearchParameterTypes', str, List[Union[str, int]], Dict[str, Union[str, int]]) SearchParameterTypes = TypeVar('SearchParameterTypes', str, List[Union[str, int]], Dict[str, Union[str, int]])
@ -213,12 +222,17 @@ class PyMISP:
@property @property
def recommended_pymisp_version(self) -> Dict: def recommended_pymisp_version(self) -> Dict:
"""Returns the recommended API version from the server""" """Returns the recommended API version from the server"""
# Sine MISP 2.4.146 is recommended PyMISP version included in getVersion call
misp_version = self.misp_instance_version
if "pymisp_recommended_version" in misp_version:
return {"version": misp_version["recommended_pymisp_version"]} # Returns dict to keep BC
response = self._prepare_request('GET', 'servers/getPyMISPVersion.json') response = self._prepare_request('GET', 'servers/getPyMISPVersion.json')
return self._check_json_response(response) return self._check_json_response(response)
@property @property
def version(self) -> Dict: def version(self) -> Dict:
"""Returns the version of PyMISP you're curently using""" """Returns the version of PyMISP you're currently using"""
return {'version': __version__} return {'version': __version__}
@property @property
@ -235,7 +249,7 @@ class PyMISP:
return {'version': version[0]} return {'version': version[0]}
return {'error': 'Impossible to retrieve the version of the main branch.'} return {'error': 'Impossible to retrieve the version of the main branch.'}
@property @cached_property
def misp_instance_version(self) -> Dict: def misp_instance_version(self) -> Dict:
"""Returns the version of the instance.""" """Returns the version of the instance."""
response = self._prepare_request('GET', 'servers/getVersion') response = self._prepare_request('GET', 'servers/getVersion')