(self, method, url, body=None, headers=None, **kwargs)
return response
Implementation of urllib3's urlopen.
Implementation of urllib3's urlopen.
Implementation of urllib3 s urlopen .
def urlopen(self, method, url, body=None, headers=None, **kwargs):
        """Implementation of urllib3's urlopen."""
        # pylint: disable=arguments-differ
        # We use kwargs to collect additional args that we don't need to
        # introspect here. However, we do explicitly collect the two
        # positional arguments.

        # Use a kwarg for this instead of an attribute to maintain
        # thread-safety.
        _credential_refresh_attempt = kwargs.pop("_credential_refresh_attempt", 0)

        if headers is None:
            headers = self.headers

        # Make a copy of the headers. They will be modified by the credentials
        # and we want to pass the original headers if we recurse.
        request_headers = headers.copy()

        self.credentials.before_request(self._request, method, url, request_headers)

        response = self.http.urlopen(
            method, url, body=body, headers=request_headers, **kwargs
        )

        # If the response indicated that the credentials needed to be
        # refreshed, then refresh the credentials and re-attempt the
        # request.
        # A stored token may expire between the time it is retrieved and
        # the time the request is made, so we may need to try twice.
        # The reason urllib3's retries aren't used is because they
        # don't allow you to modify the request headers. :/
        if (
            response.status in self._refresh_status_codes
            and _credential_refresh_attempt < self._max_refresh_attempts
        ):

            _LOGGER.info(
                "Refreshing credentials due to a %s response. Attempt %s/%s.",
                response.status,
                _credential_refresh_attempt + 1,
                self._max_refresh_attempts,
            )

            self.credentials.refresh(self._request)

            # Recurse. Pass in the original headers, not our modified set.
            return self.urlopen(
                method,
                url,
                body=body,
                headers=headers,
                _credential_refresh_attempt=_credential_refresh_attempt + 1,
                **kwargs
            )

        return response
def urlopen ( self , method , url , body = None , headers = None , * * kwargs ) : # pylint: disable=arguments-differ # We use kwargs to collect additional args that we don't need to # introspect here. However, we do explicitly collect the two # positional arguments. # Use a kwarg for this instead of an attribute to maintain # thread-safety. _credential_refresh_attempt = kwargs . pop ( "_credential_refresh_attempt" , 0 ) if headers is None : headers = self . headers # Make a copy of the headers. They will be modified by the credentials # and we want to pass the original headers if we recurse. request_headers = headers . copy ( ) self . credentials . before_request ( self . _request , method , url , request_headers ) response = self . http . urlopen ( method , url , body = body , headers = request_headers , * * kwargs ) # If the response indicated that the credentials needed to be # refreshed, then refresh the credentials and re-attempt the # request. # A stored token may expire between the time it is retrieved and # the time the request is made, so we may need to try twice. # The reason urllib3's retries aren't used is because they # don't allow you to modify the request headers. :/ if ( response . status in self . _refresh_status_codes and _credential_refresh_attempt < self . _max_refresh_attempts ) : _LOGGER . info ( "Refreshing credentials due to a %s response. Attempt %s/%s." , response . status , _credential_refresh_attempt + 1 , self . _max_refresh_attempts , ) self . credentials . refresh ( self . _request ) # Recurse. Pass in the original headers, not our modified set. return self . urlopen ( method , url , body = body , headers = headers , _credential_refresh_attempt = _credential_refresh_attempt + 1 , * * kwargs ) return response