QRadar Python helper library functions

The QRadar® Python helper library (qpylib) contains several useful functions that you can use to add logging, make REST API calls, and convert JSON objects to Python dictionaries.

All functions that you import into your app's views.py file can be called globally.

The following table describes functions that you can import into your app's views.py file.

Table 1. Functions that you can import into your app
Function Format Description
log()
def log(message, level='info'):

Here's an example:

from qpylib import qpylib

..

#in precedence order from lowest level to highest

log('debug message' ,'debug')

log('info message' ,'info')

log('warning message' ,'warning')

log('error message' ,'error')

log('critical message' ,'critical')
Import the qpylib helper library into your app's views.py to use the log() function. This function writes messages at your chosen log level to the /store/log/app.log file.

By default, logging is turned on and set to INFO level. Lower level logging messages are ignored. Use the POST /log_level endpoint to change

set_log_level( log_level )
def set_log_level(log_level='info'):
Set the current log level. Used by the POST /log_level endpoint but can also be called programmatically.
REST()
def REST( RESTtype, requestURL, headers={}, 
data=None, params=None, json=None, version=None ):

For example:

try:
  headers = {'content-type' : 'text/plain'}
  arielOptions = qpylib.REST( 'get',
                 '/api/ariel/databases', headers = headers )
except Exception as e:
  qpylib.log( "Error "  + str(e) )
  raise
Import the qpylib library to use this function to make calls to the QRadar REST API endpoints. The endpoint takes care of authentication and authorization by reusing the security tokens that are passed on the request from QRadar.
to_json_dict( JSON )
def to_json_dict(python_obj):
Converts a JSON object in to a Python dictionary.