def __decode_response(self,
                          response,
                          query_params={'output_format': 'json'}):
        if response.code != 200:
            raise CheckMkWebApiResponseException(response)

        body = response.read().decode()

        if body.startswith('Authentication error:'):
            raise CheckMkWebApiAuthenticationException(body)

        body_dict = self.__parse_response_body(body, query_params)

        # Views return json lists and not dicts of information.
        # Validate the result is a list, return result

        if isinstance(body_dict, list):
            return body_dict

        result = body_dict['result']

        if body_dict['result_code'] == 0:
            return result

        raise CheckMkWebApiException(result)
Example #2
0
    def make_request(self, action, query_params=None, data=None):
        """
        Make arbitrary request to Check_Mk Web API

        # Arguments
        action (str): Action request, e.g. add_host
        query_params (dict): dict of path parameters
        data (dict): dict that will be sent as request body
        # Basic Auth
        password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(None, self.web_api_base, self.username, self.secret)
        handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
        opener = urllib.request.build_opener(handler)
        urllib.request.install_opener(opener)
        # Raises
        CheckMkWebApiResponseException: Raised when the HTTP status code != 200
        CheckMkWebApiException: Raised when the action's result_code != 0
        """
        if not query_params:
            query_params = {}
        else:
            query_params = dict(query_params)  # work on copy

        query_params.update({'action': action})

        request_format = query_params.get('request_format', 'json')

        response = urllib.request.urlopen(
            self.__build_request_path(query_params),
            WebApi.__build_request_data(data, request_format))

        if response.code != 200:
            raise CheckMkWebApiResponseException(response)

        body = response.read().decode()

        if body.startswith('Authentication error:'):
            raise CheckMkWebApiAuthenticationException(body)

        if 'output_format' in query_params and query_params[
                'output_format'] == 'python':
            body_dict = ast.literal_eval(body)
        else:
            body_dict = json.loads(body)

        result = body_dict['result']
        if body_dict['result_code'] == 0:
            return result

        raise CheckMkWebApiException(result)
Example #3
0
    def make_request(self, action, query_params=None, data=None):
        """
        Makes arbitrary request to Check_Mk Web API

        # Arguments
        action (str): Action request, e.g. add_host
        query_params (dict): dict of path parameters
        data (dict): dict that will be sent as request body

        # Raises
        CheckMkWebApiResponseException: Raised when the HTTP status code != 200
        CheckMkWebApiException: Raised when the action's result_code != 0
        """
        if not query_params:
            query_params = {}
        else:
            query_params = dict(query_params)  # work on copy

        query_params.update({'action': action})

        response = urllib.request.urlopen(
            self.__build_request_path(query_params),
            WebApi.__build_request_data(data))

        if response.code != 200:
            raise CheckMkWebApiResponseException(response)

        body = response.read().decode()

        if body.startswith('Authentication error:'):
            raise CheckMkWebApiAuthenticationException(body)

        body_json = json.loads(body)
        result = body_json['result']

        if body_json['result_code'] == 0:
            return result

        raise CheckMkWebApiException(result)