Python urllib3.HTTPResponse() Examples

The following are 9 code examples of urllib3.HTTPResponse(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib3 , or try the search function .
Example #1
Source File: utils.py    From drf-reverse-proxy with Mozilla Public License 2.0 7 votes vote down vote up
def get_urlopen_mock(body=DEFAULT_BODY_CONTENT, headers=None, status=200):
    mockHttpResponse = Mock(name='httplib.HTTPResponse')

    headers = urllib3.response.HTTPHeaderDict(headers)

    if not hasattr(body, 'read'):
        body = BytesIO(body)

    else:
        body.seek(0)

    urllib3_response = urllib3.HTTPResponse(body,
                                            headers,
                                            status,
                                            preload_content=False,
                                            original_response=mockHttpResponse)

    return MagicMock(return_value=urllib3_response) 
Example #2
Source File: https.py    From OpenDoor with GNU General Public License v3.0 5 votes vote down vote up
def _provide_ssl_auth_required(self):
        """
        Provide ssl auth response
        :return: urllib3.HTTPResponse
        """
        
        response = HTTPResponse()
        response.status = self.DEFAULT_SSL_CERT_REQUIRED_STATUSES
        response.__setattr__('_body', ' ')
        return response 
Example #3
Source File: response.py    From kubetest with GNU General Public License v3.0 5 votes vote down vote up
def json(self) -> Dict[str, Any]:
        """Convert the response data to JSON.

        If the response data is not valid JSON, an error will be raised.

        Returns:
            The JSON data loaded into a Python dictionary.
        """
        # This should generally be the case, since the primary source of Response
        # instances are the HTTP methods on Pods (e.g. http_proxy_get), where
        # `_preload_content=False`. Should that be set to True, it will not return
        # the HTTPResponse but will instead return the response data formatted as
        # the type specified in the `response_type` param. By default, kubetest sets
        # the _preload_content field to True, so this should generally not be hit.
        if isinstance(self.data, urllib3.HTTPResponse):
            return json.loads(self.data.data)

        # The response data comes back as a string. This could be a JSON string,
        # or something else (text body, error string, etc). Since we've preloaded
        # the content as a string (see comment above), we can not simply load the
        # string as JSON as the preloading essentially serializes the Python dict
        # out to a string, so various values will not parse into JSON (None vs null,
        # " vs ', etc). To remedy this, we attempt to load the response as an ast
        # literal - if it fails for any reason, fall back to trying to load JSON.
        # At the very least the JSON loading will fail with a familiar error which
        # one would expect from a .json() function.
        try:
            data = ast.literal_eval(self.data)
        except Exception as e:
            log.debug(f'failed literal eval of data {self.data} ({e})')
            data = json.loads(self.data)

        return data 
Example #4
Source File: flux_csv_parser.py    From influxdb-client-python with MIT License 5 votes vote down vote up
def __init__(self, response: HTTPResponse, serialization_mode: FluxSerializationMode,
                 data_frame_index: List[str] = None) -> None:
        self._response = response
        self.tables = []
        self._serialization_mode = serialization_mode
        self._data_frame_index = data_frame_index
        self._data_frame_values = []
        pass 
Example #5
Source File: test_FluxCSVParser.py    From influxdb-client-python with MIT License 5 votes vote down vote up
def _parse_to_tables(data: str):
        fp = BytesIO(str.encode(data))
        _parser = FluxCsvParser(response=HTTPResponse(fp, preload_content=False),
                                serialization_mode=FluxSerializationMode.tables)
        list(_parser.generator())
        tables = _parser.tables
        return tables 
Example #6
Source File: rest.py    From alfred-dropbox with MIT License 5 votes vote down vote up
def __init__(self, resp):
        # arg: A urllib3.HTTPResponse object
        self.urllib3_response = resp
        self.status = resp.status
        self.version = resp.version
        self.reason = resp.reason
        self.strict = resp.strict
        self.is_closed = False 
Example #7
Source File: rest.py    From alfred-dropbox with MIT License 5 votes vote down vote up
def closed(self):
        return self.is_closed


    # ---------------------------------
    # Backwards compat for HTTPResponse
    # --------------------------------- 
Example #8
Source File: test_kubernetes_executor.py    From airflow with Apache License 2.0 4 votes vote down vote up
def test_run_next_exception(self, mock_get_kube_client, mock_kubernetes_job_watcher):

        # When a quota is exceeded this is the ApiException we get
        response = HTTPResponse(
            body='{"kind": "Status", "apiVersion": "v1", "metadata": {}, "status": "Failure", '
                 '"message": "pods \\"podname\\" is forbidden: exceeded quota: compute-resources, '
                 'requested: limits.memory=4Gi, used: limits.memory=6508Mi, limited: limits.memory=10Gi", '
                 '"reason": "Forbidden", "details": {"name": "podname", "kind": "pods"}, "code": 403}')
        response.status = 403
        response.reason = "Forbidden"

        # A mock kube_client that throws errors when making a pod
        mock_kube_client = mock.patch('kubernetes.client.CoreV1Api', autospec=True)
        mock_kube_client.create_namespaced_pod = mock.MagicMock(
            side_effect=ApiException(http_resp=response))
        mock_get_kube_client.return_value = mock_kube_client
        mock_api_client = mock.MagicMock()
        mock_api_client.sanitize_for_serialization.return_value = {}
        mock_kube_client.api_client = mock_api_client

        kubernetes_executor = KubernetesExecutor()
        kubernetes_executor.start()

        # Execute a task while the Api Throws errors
        try_number = 1
        kubernetes_executor.execute_async(key=('dag', 'task', datetime.utcnow(), try_number),
                                          queue=None,
                                          command=['airflow', 'tasks', 'run', 'true', 'some_parameter'],
                                          executor_config={})
        kubernetes_executor.sync()
        kubernetes_executor.sync()

        assert mock_kube_client.create_namespaced_pod.called
        self.assertFalse(kubernetes_executor.task_queue.empty())

        # Disable the ApiException
        mock_kube_client.create_namespaced_pod.side_effect = None

        # Execute the task without errors should empty the queue
        kubernetes_executor.sync()
        assert mock_kube_client.create_namespaced_pod.called
        self.assertTrue(kubernetes_executor.task_queue.empty()) 
Example #9
Source File: https.py    From OpenDoor with GNU General Public License v3.0 4 votes vote down vote up
def request(self, url):
        """
        Client request SSL
        :param str url: request uri
        :return: urllib3.HTTPResponse
        """

        if self._HTTP_DBG_LEVEL <= self.__debug.level:
            self.__debug.debug_request(self._headers, url, self.__cfg.method)

        try:

            disable_warnings(InsecureRequestWarning)

            if self.__cfg.DEFAULT_SCAN == self.__cfg.scan:  # directories requests
                response = self.__pool.request(self.__cfg.method,
                                               helper.parse_url(url).path,
                                               headers=self._headers,
                                               retries=self.__cfg.retries,
                                               assert_same_host=False,
                                               redirect=False)
                self.cookies_middleware(is_accept=self.__cfg.accept_cookies, response=response)
            else:  # subdomains

                response = PoolManager().request(self.__cfg.method, url,
                                                 headers=self._headers,
                                                 retries=self.__cfg.retries,
                                                 assert_same_host=False,
                                                 redirect=False)
            return response

        except MaxRetryError:
            if self.__cfg.DEFAULT_SCAN == self.__cfg.scan:
                self.__tpl.warning(key='max_retry_error', url=helper.parse_url(url).path)

        except HostChangedError as error:
            self.__tpl.warning(key='host_changed_error', details=error)
            pass

        except ReadTimeoutError:
            self.__tpl.warning(key='read_timeout_error', url=url)
            pass

        except ConnectTimeoutError:
            self.__tpl.warning(key='connection_timeout_error', url=url)
            pass

        except SSLError:
            if self.__cfg.DEFAULT_SCAN != self.__cfg.scan:
                return self._provide_ssl_auth_required()