Skip to content

Instantly share code, notes, and snippets.

@hobbes3
Created June 16, 2016 20:48
Show Gist options
  • Save hobbes3/2ca54602213b1815f1a1837db5285aae to your computer and use it in GitHub Desktop.
Save hobbes3/2ca54602213b1815f1a1837db5285aae to your computer and use it in GitHub Desktop.
import cherrypy
import json
import pprint
import logging
import splunk.appserver.mrsparkle.controllers as controllers
from splunk.appserver.mrsparkle.lib.decorators import expose_page
from splunk.appserver.mrsparkle.lib.routes import route
from splunk.appserver.mrsparkle.lib.util import make_url
def setup_logger(modulename):
logger = logging.getLogger(modulename)
logger.propagate = False # Prevent the log messages from being duplicated in the python.log file
logger.setLevel(logging.INFO)
logger.addHandler(rotating_log_file)
return logger
def get_username(sessionKey):
try:
uri = 'authentication/current-context?output_mode=json'
serverResponse, serverContent = splunk.rest.simpleRequest(
make_url(uri, translate=False, relative=True, encode=False),
sessionKey,
postargs=None,
method='GET',
raiseAllErrors=True,
proxyMode=False,
rawResult=None,
jsonargs=None,
timeout=splunk.rest.SPLUNKD_CONNECTION_TIMEOUT
)
jsonResp = json.loads(serverContent)
user_name = jsonResp["entry"][0]["content"]["username"]
#logger.debug("User Name :: %s" % user_name)
return user_name
except Exception as e:
#logger.exception(e)
return None
#logger = setup_logger('comment')
class Comment(controllers.BaseController):
@route('/')
@expose_page(must_login=False, methods=['POST'])
def song(self, **kwargs):
try:
session_key = cherrypy.session.get('sessionKey')
user = get_username(session_key)
result = {}
result['song'] = 'la, la, la'
result['kwargs'] = kwargs
result['session_key'] = session_key
result['user'] = user
return self.render_json(result)
except Exception as e:
#logger.exception(e)
cherrypy.response.status = 500
return repr(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment