Ejemplo n.º 1
0
    def query(self, analysis_type, params):
        """
        Performs a query using the Keen IO analysis API.  A read key must be set first.

        """
        if not self.read_key:
            raise exceptions.InvalidEnvironmentError(
                "The Keen IO API requires a read key to perform queries. "
                "Please set a 'read_key' when initializing the "
                "KeenApi object.")

        url = "{0}/{1}/projects/{2}/queries/{3}".format(
            self.base_url, self.api_version, self.project_id, analysis_type)

        headers = {"Authorization": self.read_key}
        payload = params
        response = fulfill(HTTPMethods.GET,
                           url,
                           params=payload,
                           headers=headers)
        if response.status_code != 200:
            error = response.json()
            raise exceptions.KeenApiError(error)

        return response.json()["result"]
Ejemplo n.º 2
0
    def post_event(self, event):
        """
        Posts a single event to the Keen IO API. The write key must be set first.

        :param event: an Event to upload
        """
        if not self.write_key:
            raise exceptions.InvalidEnvironmentError(
                "The Keen IO API requires a write key to send events. "
                "Please set a 'write_key' when initializing the "
                "KeenApi object.")

        url = "{0}/{1}/projects/{2}/events/{3}".format(self.base_url,
                                                       self.api_version,
                                                       self.project_id,
                                                       event.event_collection)
        headers = {
            "Content-Type": "application/json",
            "Authorization": self.write_key
        }
        payload = event.to_json()
        response = fulfill(HTTPMethods.POST,
                           url,
                           data=payload,
                           headers=headers)
        if response.status_code != 201:
            error = response.json()
            raise exceptions.KeenApiError(error)
Ejemplo n.º 3
0
    def _error_handling(self, res):
        """
        Helper function to do the error handling

        :params res: the response from a request
        """
        # making the error handling generic so if an status_code starting with 2 doesn't exist, we raise the error
        if res.status_code // 100 != 2:
            error = self._get_response_json(res)
            raise exceptions.KeenApiError(error)
Ejemplo n.º 4
0
    def error_handling(self, res):
        """
        Helper function to do the error handling

        :params res: the response from a request
        """
        # making the error handling generic so if an status_code starting with 2 doesn't exist, we raise the error
        if res.status_code // 100 != 2:
            try:
                error = res.json()
            except json.JSONDecodeError:
                error = {
                    'message':
                    'The API did not respond with JSON, but: "{0}"'.format(
                        res.text[:1000]),
                    "error_code":
                    "InvalidResponseFormat"
                }
            raise exceptions.KeenApiError(error)