-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add decorator for json rest endpoints
- Loading branch information
1 parent
418a174
commit 1a0f7d9
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"""The following imports allow these classes to be imported via | ||
the splunklib.customrest package like so: | ||
from splunklib.customrest import * | ||
""" | ||
from .json import json_handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import logging | ||
import traceback | ||
import json | ||
|
||
from functools import wraps | ||
|
||
def json_handler(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
decorated = json_exception_handler(json_payload_extractor(func)) | ||
return decorated(*args, **kwargs) | ||
return wrapper | ||
|
||
|
||
def json_payload_extractor(func): | ||
@wraps(func) | ||
def wrapper(self, in_string): | ||
try: | ||
request = json.loads(in_string) | ||
kwargs = {'request': request, 'in_string': in_string} | ||
if 'payload' in request: | ||
# if request contains payload, parse it and add it as payload parameter | ||
kwargs['payload'] = json.loads(request['payload']) | ||
if 'query' in request: | ||
# if request contains query, parse it and add it as query parameter | ||
kwargs['query'] = _convert_tuples_to_dict(request['query']) | ||
return func(self, **kwargs) | ||
except ValueError as e: | ||
return {'payload': {'success': 'false', 'result': f'Error parsing JSON: {e}'}, | ||
'status': 400 | ||
} | ||
return wrapper | ||
|
||
|
||
def json_exception_handler(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception as e: | ||
logging.error( | ||
f'error={repr(e)} traceback={traceback.format_exc()}') | ||
return {'payload': {'success': 'false', 'message': f'Error: {repr(e)}'}, | ||
'status': 500 | ||
} | ||
return wrapper | ||
|
||
|
||
def _convert_tuples_to_dict(tuples): | ||
return {t[0]: t[1] for t in tuples} | ||
|