def __init__( self, host, # type: Text port, # type: int user, # type: Text source=None, # type: Text catalog=None, # type: Text schema=None, # type: Text session_properties=None, # type: Optional[Dict[Text, Any]] http_session=None, # type: Any http_headers=None, # type: Optional[Dict[Text, Text]] transaction_id=NO_TRANSACTION, # type: Optional[Text] http_scheme=constants.HTTP, # type: Text auth=constants.DEFAULT_AUTH, # type: Optional[Any] redirect_handler=None, max_attempts=MAX_ATTEMPTS, # type: int request_timeout=constants. DEFAULT_REQUEST_TIMEOUT, # type: Union[float, Tuple[float, float]] handle_retry=exceptions.RetryWithExponentialBackoff(), verify=True # type: Any ): # type: (...) -> None self._client_session = ClientSession( catalog, schema, source, user, session_properties, http_headers, transaction_id, ) self._host = host self._port = port self._next_uri = None # type: Optional[Text] if http_session is not None: self._http_session = http_session else: # mypy cannot follow module import self._http_session = self.http.Session() # type: ignore self._http_session.verify = verify self._http_session.headers.update(self.http_headers) self._exceptions = self.HTTP_EXCEPTIONS self._auth = auth if self._auth: if http_scheme == constants.HTTP: raise ValueError("cannot use authentication with HTTP") self._auth.set_http_session(self._http_session) self._exceptions += self._auth.get_exceptions() self._redirect_handler = redirect_handler self._request_timeout = request_timeout self._handle_retry = handle_retry self.max_attempts = max_attempts self._http_scheme = http_scheme
def __init__( self, host: str, port: int, user: str, source: str = None, catalog: str = None, schema: str = None, session_properties: Optional[Dict[str, Any]] = None, http_session: Any = None, http_headers: Optional[Dict[str, str]] = None, transaction_id: Optional[str] = NO_TRANSACTION, http_scheme: str = constants.HTTP, auth: Optional[Any] = constants.DEFAULT_AUTH, redirect_handler: Any = None, max_attempts: int = MAX_ATTEMPTS, request_timeout: Union[float, Tuple[float, float]] = constants.DEFAULT_REQUEST_TIMEOUT, handle_retry=exceptions.RetryWithExponentialBackoff(), verify: bool = True ) -> None: self._client_session = ClientSession( catalog, schema, source, user, session_properties, http_headers, transaction_id, ) self._host = host self._port = port self._next_uri: Optional[str] = None if http_session is not None: self._http_session = http_session else: self._http_session = self.http.Session() self._http_session.verify = verify self._http_session.headers.update(self.http_headers) self._exceptions = self.HTTP_EXCEPTIONS self._auth = auth if self._auth: if http_scheme == constants.HTTP: raise ValueError("cannot use authentication with HTTP") self._auth.set_http_session(self._http_session) self._exceptions += self._auth.get_exceptions() self._redirect_handler = redirect_handler self._request_timeout = request_timeout self._handle_retry = handle_retry self.max_attempts = max_attempts self._http_scheme = http_scheme
def test_retry_with(): max_attempts = 3 with_retry = exceptions.retry_with( handle_retry=exceptions.RetryWithExponentialBackoff(), exceptions=[SomeException], conditions={}, max_attempts=max_attempts, ) class FailerUntil(object): def __init__(self, until=1): self.attempt = 0 self._until = until def __call__(self): self.attempt += 1 if self.attempt > self._until: return raise SomeException(self.attempt) with_retry(FailerUntil(2).__call__)() with pytest.raises(SomeException): with_retry(FailerUntil(3).__call__)()