예제 #1
0
 def test_convert_request_exception(self):
     """
     Converts known request exceptions into Globus NetworkErrors,
     confirms expected values.
     """
     # NetworkError
     conv = convert_request_exception(self.exc)
     self.assertIsInstance(conv, NetworkError)
     self.assertEqual(conv.underlying_exception.args,
                      self.exc.args)
     # Timeout Error
     conv = convert_request_exception(self.timeout_exc)
     self.assertIsInstance(conv, GlobusTimeoutError)
     self.assertEqual(conv.underlying_exception.args,
                      self.timeout_exc.args)
     # Connection Error
     conv = convert_request_exception(self.connection_exc)
     self.assertIsInstance(conv, GlobusConnectionError)
     self.assertEqual(conv.underlying_exception.args,
                      self.connection_exc.args)
예제 #2
0
 def send_request():
     try:
         return self._session.request(method=method,
                                      url=url,
                                      headers=rheaders,
                                      params=params,
                                      data=text_body,
                                      verify=self._verify,
                                      timeout=self._http_timeout)
     except requests.RequestException as e:
         self.logger.error("NetworkError on request")
         raise exc.convert_request_exception(e)
예제 #3
0
 def send_request():
     try:
         return self._session.request(
             method=method,
             url=url,
             headers=rheaders,
             allow_redirects=allow_redirects,
             verify=self._verify,
             timeout=self._http_timeout,
             **kwargs
         )
     except requests.RequestException as e:
         self.logger.error("NetworkError on request")
         raise exc.convert_request_exception(e)
예제 #4
0
def test_convert_requests_exception(exc, conv_class):
    conv = convert_request_exception(exc)
    assert conv.underlying_exception == exc
    assert isinstance(conv, conv_class)
예제 #5
0
def test_convert_requests_exception(orig, conv_class):
    conv = exc.convert_request_exception(orig)
    assert conv.underlying_exception == orig
    assert isinstance(conv, conv_class)
예제 #6
0
    def request(
        self,
        method: str,
        url: str,
        query_params: Optional[Dict[str, Any]] = None,
        data: Union[Dict[str, Any], str, None] = None,
        headers: Optional[Dict[str, str]] = None,
        encoding: Optional[str] = None,
        authorizer: Optional[GlobusAuthorizer] = None,
        allow_redirects: bool = True,
        stream: bool = False,
    ) -> requests.Response:
        """
        Send an HTTP request

        :param url: URL for the request
        :type url: str
        :param method: HTTP request method, as an all caps string
        :type method: str
        :param query_params: Parameters to be encoded as a query string
        :type query_params: dict, optional
        :param headers: HTTP headers to add to the request
        :type headers: dict
        :param data: Data to send as the request body. May pass through encoding.
        :type data: dict or str
        :param encoding: A way to encode request data. "json", "form", and "text"
            are all valid values. Custom encodings can be used only if they are
            registered with the transport. By default, strings get "text" behavior and
            all other objects get "json".
        :type encoding: str
        :param authorizer: The authorizer which is used to get or update authorization
            information for the request
        :type authorizer: GlobusAuthorizer, optional
        :param allow_redirects: Follow Location headers on redirect response
            automatically. Defaults to ``True``
        :type allow_redirects: bool
        :param stream: Do not immediately download the response content. Defaults to
            ``False``
        :type stream: bool

        :return: ``requests.Response`` object
        """
        log.debug("starting request for %s", url)
        resp: Optional[requests.Response] = None
        req = self._encode(method, url, query_params, data, headers, encoding)
        checker = RetryCheckRunner(self.retry_checks)
        log.debug("transport request state initialized")
        for attempt in range(self.max_retries + 1):
            log.debug("transport request retry cycle. attempt=%d", attempt)
            # add Authorization header, or (if it's a NullAuthorizer) possibly
            # explicitly remove the Authorization header
            # done fresh for each request, to handle potential for refreshed credentials
            self._set_authz_header(authorizer, req)

            ctx = RetryContext(attempt, authorizer=authorizer)
            try:
                log.debug("request about to send")
                resp = ctx.response = self.session.send(
                    req.prepare(),
                    timeout=self.http_timeout,
                    verify=self.verify_ssl,
                    allow_redirects=allow_redirects,
                    stream=stream,
                )
            except requests.RequestException as err:
                log.debug("request hit error (RequestException)")
                ctx.exception = err
                if attempt >= self.max_retries or not checker.should_retry(
                        ctx):
                    log.warning("request done (fail, error)")
                    raise exc.convert_request_exception(err)
                log.debug("request may retry (should-retry=true)")
            else:
                log.debug("request success, still check should-retry")
                if not checker.should_retry(ctx):
                    log.info("request done (success)")
                    return resp
                log.debug("request may retry, will check attempts")

            # the request will be retried, so sleep...
            if attempt < self.max_retries:
                log.debug("under attempt limit, will sleep")
                self._retry_sleep(ctx)
        if resp is None:
            raise ValueError("Somehow, retries ended without a response")
        log.warning("request reached max retries, done (fail, response)")
        return resp
예제 #7
0
def test_convert_requests_exception(exc, conv_class):
    conv = convert_request_exception(exc)
    assert conv.underlying_exception == exc
    assert isinstance(conv, conv_class)