Exemple #1
0
    def _request(self, endpoint, method="POST", sql_string="", stream_properties=None, encoding="utf-8"):
        url = "{}/{}".format(self.url, endpoint)

        logging.debug("KSQL generated: {}".format(sql_string))

        sql_string = self._validate_sql_string(sql_string)
        body = {"ksql": sql_string}
        if stream_properties:
            body["streamsProperties"] = stream_properties
        else:
            body["streamsProperties"] = {}
        data = json.dumps(body).encode(encoding)

        headers = deepcopy(self.headers)
        if self.api_key and self.secret:
            base64string = base64.b64encode(bytes("{}:{}".format(self.api_key, self.secret), "utf-8")).decode("utf-8")
            headers["Authorization"] = "Basic %s" % base64string

        req = urllib.request.Request(url=url, data=data, headers=headers, method=method.upper())

        try:
            r = urllib.request.urlopen(req, timeout=self.timeout)
        except urllib.error.HTTPError as http_error:
            try:
                content = json.loads(http_error.read().decode(encoding))
            except Exception as e:
                raise http_error
            else:
                logging.debug("content: {}".format(content))
                raise KSQLError(e=content.get("message"), error_code=content.get("error_code"))
        else:
            return r
Exemple #2
0
 def _raise_for_status(r, response):
     r_json = json.loads(response)
     if r.getcode() != 200:
         # seems to be the new API behavior
         if r_json.get("@type") == "statement_error" or r_json.get("@type") == "generic_error":
             error_message = r_json["message"]
             error_code = r_json["error_code"]
             stackTrace = r_json["stack_trace"]
             raise KSQLError(error_message, error_code, stackTrace)
         else:
             raise KSQLError("Unknown Error: {}".format(r.content))
     else:
         # seems to be the old API behavior, so some errors have status 200, bug??
         if r_json[0]["@type"] == "currentStatus" and r_json[0]["commandStatus"]["status"] == "ERROR":
             error_message = r_json[0]["commandStatus"]["message"]
             error_code = None
             stackTrace = None
             raise KSQLError(error_message, error_code, stackTrace)
         return True
Exemple #3
0
 def _raise_for_status(r, response):
     r_json = json.loads(response)
     if r.getcode() != 200:
         # seems to be the new API behavior
         if r_json.get('@type') == 'statement_error' or r_json.get(
                 '@type') == 'generic_error':
             error_message = r_json['message']
             error_code = r_json['error_code']
             stackTrace = r_json['stackTrace']
             raise KSQLError(error_message, error_code, stackTrace)
         else:
             raise KSQLError("Unknown Error: {}".format(r.content))
     else:
         # seems to be the old API behavior, so some errors have status 200, bug??
         if r_json[0]['@type'] == 'currentStatus' \
                 and r_json[0]['commandStatus']['status'] == 'ERROR':
             error_message = r_json[0]['commandStatus']['message']
             error_code = None
             stackTrace = None
             raise KSQLError(error_message, error_code, stackTrace)
         return True