new: Added async method for search_index

pull/80/head
Hannah Ward 2017-02-16 15:39:36 +00:00
parent bb00201aaf
commit 9e59a81b4c
No known key found for this signature in database
GPG Key ID: 6F3BAD60DE190290
1 changed files with 26 additions and 13 deletions

View File

@ -25,6 +25,13 @@ try:
except ImportError:
HAVE_REQUESTS = False
try:
from requests_futures.sessions import FuturesSession
ASYNC_OK = True
except ImportError:
ASYNC_OK = False
from . import __version__
from .exceptions import PyMISPError, SearchError, MissingDependency, NoURL, NoKey
from .mispevent import MISPEvent, MISPAttribute, EncodeUpdate
@ -94,12 +101,8 @@ class PyMISP(object):
self.proxies = proxies
self.cert = cert
self.asynch = asynch
if self.asynch:
try:
from requests_futures.sessions import FuturesSession
except ImportError:
print("You set Async, but you haven't got requests_futures installed")
print("Reverting to synchronous")
if asynch and not ASYNC_OK:
warnings.warn("You turned on Async, but don't have requests_futures installed")
self.asynch = False
self.ressources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
@ -146,11 +149,14 @@ class PyMISP(object):
self.category_type_mapping = self.describe_types['category_type_mappings']
self.sane_default = self.describe_types['sane_defaults']
def __prepare_session(self, output='json'):
def __prepare_session(self, output='json', async_implemented=False):
"""Prepare the headers of the session"""
if not HAVE_REQUESTS:
raise MissingDependency('Missing dependency, install requests (`pip install requests`)')
if self.asynch and async_implemented:
session = FuturesSession()
else:
session = requests.Session()
session.verify = self.ssl
session.proxies = self.proxies
@ -772,7 +778,7 @@ class PyMISP(object):
def search_index(self, published=None, eventid=None, tag=None, datefrom=None,
dateuntil=None, eventinfo=None, threatlevel=None, distribution=None,
analysis=None, attribute=None, org=None):
analysis=None, attribute=None, org=None, async_callback=None):
"""Search only at the index level. Use ! infront of value as NOT, default OR
:param published: Published (0,1)
@ -808,8 +814,15 @@ class PyMISP(object):
buildup_url += '/search{}:{}'.format(rule, joined)
else:
buildup_url += '/search{}:{}'.format(rule, allowed[rule])
session = self.__prepare_session()
session = self.__prepare_session(async_implemented=True)
url = urljoin(self.root_url, buildup_url)
if self.asynch:
if not async_callback:
warnings.warn("You haven't provided a callback!")
response = session.get(url, background_callback=async_callback)
else:
response = session.get(url)
return self._check_response(response)