예제 #1
0
    def _send_request_helper(self, url, accepted_status_codes, function, data,
                             retry_count):
        while True:
            try:
                if self._endpoint.auth == constants.NO_AUTH:
                    if data is None:
                        r = function(url,
                                     headers=self._headers,
                                     verify=self.verify_ssl)
                    else:
                        r = function(url,
                                     headers=self._headers,
                                     data=json.dumps(data),
                                     verify=self.verify_ssl)
                else:
                    if data is None:
                        r = function(url,
                                     headers=self._headers,
                                     auth=self._auth,
                                     verify=self.verify_ssl)
                    else:
                        r = function(url,
                                     headers=self._headers,
                                     auth=self._auth,
                                     data=json.dumps(data),
                                     verify=self.verify_ssl)
            except requests.exceptions.RequestException as e:
                error = True
                r = None
                status = None
                text = None

                self.logger.error(u"Request to '{}' failed with '{}'".format(
                    url, e))
            else:
                error = False
                status = r.status_code
                text = r.text

            if error or status not in accepted_status_codes:
                if self._retry_policy.should_retry(status, error, retry_count):
                    sleep(self._retry_policy.seconds_to_sleep(retry_count))
                    retry_count += 1
                    continue

                if error:
                    raise HttpClientException(
                        u"Error sending http request and maximum retry encountered."
                    )
                else:
                    if status == 400:
                        raise SparkCommandFailedException(
                            'Spark command failed! This is often the result of code using more memory on the Spark Driver than what was configured. Please try increasing the Spark Driver memory in the configuration.'
                        )
                    else:
                        raise HttpClientException(
                            u"Invalid status code '{}' from {} with error payload: {}"
                            .format(status, url, text))
            return r
예제 #2
0
    def _send_request_helper(self, url, accepted_status_codes, function, data,
                             retry_count):
        while True:
            try:
                if not self._endpoint.authenticate:
                    if data is None:
                        r = function(url,
                                     headers=self._headers,
                                     verify=self.verify_ssl)
                    else:
                        r = function(url,
                                     headers=self._headers,
                                     data=json.dumps(data),
                                     verify=self.verify_ssl)
                else:
                    if data is None:
                        r = function(url,
                                     headers=self._headers,
                                     auth=(self._endpoint.username,
                                           self._endpoint.password),
                                     verify=self.verify_ssl)
                    else:
                        r = function(url,
                                     headers=self._headers,
                                     auth=(self._endpoint.username,
                                           self._endpoint.password),
                                     data=json.dumps(data),
                                     verify=self.verify_ssl)
            except requests.exceptions.RequestException as e:
                error = True
                r = None
                status = None
                text = None

                self.logger.error(u"Request to '{}' failed with '{}'".format(
                    url, e))
            else:
                error = False
                status = r.status_code
                text = r.text

            if error or status not in accepted_status_codes:
                if self._retry_policy.should_retry(status, error, retry_count):
                    sleep(self._retry_policy.seconds_to_sleep(retry_count))
                    retry_count += 1
                    continue

                if error:
                    raise HttpClientException(
                        u"Error sending http request and maximum retry encountered."
                    )
                else:
                    raise HttpClientException(
                        u"Invalid status code '{}' from {} with error payload: {}"
                        .format(status, url, text))
            return r
예제 #3
0
def test_sql_expected_exception():
    line = "-o my_var"
    cell = "some spark code"
    magic.execute_sqlquery = MagicMock(side_effect=HttpClientException('HAHAHAHAH'))

    magic.sql(line, cell)
    magic.execute_sqlquery.assert_called_once_with(cell, None, None, None, None, "my_var", False, None)
    ipython_display.send_error.assert_called_once_with(constants.EXPECTED_ERROR_MSG
                                                       .format(magic.execute_sqlquery.side_effect))