cleanup style

pull/1/head
Raphaël Vinot 2014-04-14 10:55:20 +02:00
parent ac482e8f00
commit 93ddd4cf50
1 changed files with 22 additions and 22 deletions

View File

@ -1,20 +1,21 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Python API for MISP """
import requests
class PyMISP(object):
def __init__(self, url, key, out_type = 'json'):
class PyMISP(object):
""" Python API for MISP, you will need the URL
of the instnce you want to query, and the auth key of your user."""
def __init__(self, url, key, out_type='json'):
self.url = url + '/events'
self.key = key
self.out_type = out_type
self.rest = self.url + '/{}'
def __prepare_session(self, force_out=None):
"""
Prepare the headers of the session
@ -24,13 +25,13 @@ class PyMISP(object):
else:
out = self.out_type
session = requests.Session()
session.headers.update({'Authorization': self.key,
'Accept': 'application/' + out,
'content-type': 'text/' + out})
session.headers.update(
{'Authorization': self.key,
'Accept': 'application/' + out,
'content-type': 'text/' + out})
return session
################ REST API ################
# ############### REST API ################
def get_index(self):
"""
@ -61,7 +62,7 @@ class PyMISP(object):
"""
session = self.__prepare_session()
return session.post(self.rest.format(event_id), data=event,
verify=False)
verify=False)
def delete_event(self, event_id):
"""
@ -70,7 +71,7 @@ class PyMISP(object):
session = self.__prepare_session()
return session.delete(self.rest.format(event_id), verify=False)
######### REST Search #########
# ######## REST Search #########
def __prepare_rest_search(self, values, not_values):
"""
@ -78,23 +79,23 @@ class PyMISP(object):
"""
to_return = ''
if values is not None:
if type(values) != type([]):
if not isinstance(values, list):
to_return += values
else:
to_return += '&&'.join(values)
if not_values is not None:
if len(to_return) > 0 :
if len(to_return) > 0:
to_return += '&&!'
else:
to_return += '!'
if type(values) != type([]):
if not isinstance(values, list):
to_return += not_values
else:
to_return += '&&!'.join(not_values)
return to_return
def search(self, values=None, not_values=None, type_attribute=None,
category=None, org=None, tags=None, not_tags=None):
category=None, org=None, tags=None, not_tags=None):
"""
Search via the Rest API
"""
@ -114,7 +115,7 @@ class PyMISP(object):
session = self.__prepare_session()
return session.get(search.format(val, type_attribute,
category, org, tag), verify=False)
category, org, tag), verify=False)
def get_attachement(self, event_id):
"""
@ -124,8 +125,7 @@ class PyMISP(object):
session = self.__prepare_session()
return session.get(attach.format(event_id), verify=False)
############### Export ###############
# ############## Export ###############
def download_all(self):
"""
@ -139,12 +139,12 @@ class PyMISP(object):
"""
Download one event in XML
"""
template = self.url + '/events/xml/download/{}/{}'
template = self.url + '/events/xml/download/{}/{}'
if with_attachement:
a = 'true'
attach = 'true'
else:
a = 'false'
attach = 'false'
session = self.__prepare_session('xml')
return session.get(template.format(event_id, a), verify=False)
return session.get(template.format(event_id, attach), verify=False)
##########################################