|
| 1 | +# coding=utf-8 |
| 2 | +# |
| 3 | +# Copyright © 2011-2020 Splunk, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +import json |
| 18 | + |
| 19 | +from splunklib.client import Entity |
| 20 | + |
| 21 | +class JsonSink(Entity): |
| 22 | + """This class represents a JSON-based write-only sink of entities in the Splunk |
| 23 | + instance, notably telemetry-metric. |
| 24 | + """ |
| 25 | + JSON_HEADER = [('Content-Type', 'application/json')] |
| 26 | + |
| 27 | + def __init__(self, service, path, **kwargs): |
| 28 | + super(JsonSink, self).__init__(service, path, skip_refresh=True, **kwargs) |
| 29 | + |
| 30 | + def _post(self, url, **kwargs): |
| 31 | + owner, app, sharing = self._proper_namespace() |
| 32 | + |
| 33 | + return self.service.post(self.path + url, owner=owner, app=app, sharing=sharing, **kwargs) |
| 34 | + |
| 35 | + def submit(self, data): |
| 36 | + """ |
| 37 | + Submits an item to the sink. |
| 38 | +
|
| 39 | + :param data: data to submit |
| 40 | + :type data: ``dict`` |
| 41 | +
|
| 42 | + :return: return data |
| 43 | + :rtype: ``dict`` |
| 44 | + """ |
| 45 | + |
| 46 | + response = self._post('', headers=self.__class__.JSON_HEADER, body=json.dumps(data)) |
| 47 | + body = json.loads(response.body.read().decode('utf-8')) |
| 48 | + |
| 49 | + return response, body |
0 commit comments