Getting Data In

REST API Modular input - creating class in responsehandler.py

DavidCaputo
Path Finder

Hello,

I'm trying to get data in Splunk using REST API (data are in json format).
I understand I have to create my own class in responsehandler.py, but I don't know anything about python...

I tried a lot of things based on exemples but unsuccessfully...

Below a sample of my data...

{"infos":
    [
    {"component":
        {"measures":
        [
            {"metric":"ncloc","periods":[{"index":1,"value":"245"}],"value":"4082"},
            {"metric":"sqale_index","periods":[{"index":1,"value":"0"}],"value":"3564"},
            .....
            {"metric":"blocker_violations","periods":[{"index":1,"value":"0"}],"value":"0"}
        ]
        }
    }
    ]
}

Any help would be very appreciated !
Thanks,
David

0 Karma

ansif
Motivator

Try this:

class infosJSONArrayHandler:

            def __init__(self,**args):
                    pass

            def __call__(self, response_object,raw_response_output,response_type,req_args,endpoint):
                    if response_type == "json":
                            output = json.loads(raw_response_output)
                            for info in output["infos"]:
                                    print_xml_stream(json.dumps(info))
                    else:
                            print_xml_stream(raw_response_output)

NOTE: Python doesn't like inconsitently mixed tabs and spaces for indentation.

0 Karma

DavidCaputo
Path Finder

Thanks ansif,
I tried to use this class (I used only tabs for indentation) but it doesn't work eather...

I looked to the internal index and found this :
"08-27-2018 11:00:06.975 +0200 ERROR ExecProcessor - message from "python /data/splunk/etc/apps/rest_ta/bin/rest.py" HTTP Request error: 401 Client Error: Not Authorized"

I guess the python code you provide is correct but the problem is somewhere else...
David

0 Karma
Get Updates on the Splunk Community!

Updated Team Landing Page in Splunk Observability

We’re making some changes to the team landing page in Splunk Observability, based on your feedback. The ...

New! Splunk Observability Search Enhancements for Splunk APM Services/Traces and ...

Regardless of where you are in Splunk Observability, you can search for relevant APM targets including service ...

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...