def __init__( self, host, # type: Text port, # type: int user, # type: Text password, # type: Text resource_group="default", 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=prestodb.redirect.GatewayRedirectHandler(), max_attempts=MAX_ATTEMPTS, # type: int request_timeout=constants.DEFAULT_REQUEST_TIMEOUT, # type: Union[float, Tuple[float, float]] handle_retry=exceptions.RetryWithExponentialBackoff(), ): # type: (...) -> None self._client_session = ClientSession( catalog, schema, source, user, password, resource_group, 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.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, # 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_headers=None, # type: Optional[Dict[Text, Text]] http_scheme=constants.HTTP, # type: Text auth=constants.DEFAULT_AUTH, # type: Optional[Any] max_attempts=MAX_ATTEMPTS, # type: int request_timeout=constants. DEFAULT_REQUEST_TIMEOUT, # type: Union[float, Tuple[float, float]] handle_retry=exceptions.RetryWithExponentialBackoff(), ): self._client_session = ClientSession( catalog, schema, source, user, session_properties, http_headers, ) self._host = host self._port = port self._next_uri = None # type: Optional[Text] # mypy cannot follow module import self._http_session = self.http.Session() self._http_session.headers.update(self.http_headers) self._auth = auth if self._auth: if http_scheme == constants.HTTP: raise ValueError('cannot use authentication with HTTP') self._auth.set_session(self._session) 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], 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__)()
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_scheme=constants.HTTP, # type: Text auth=constants.DEFAULT_AUTH, # type: Optional[Any] max_attempts=MAX_ATTEMPTS, # type: int handle_retry=exceptions.RetryWithExponentialBackoff(), ): self._host = host self._port = port self._user = user self._source = source self._catalog = catalog self._schema = schema if session_properties is None: self._session_properties = {} else: self._session_properties = session_properties self._http_scheme = http_scheme self._next_uri = None # type: Optional[Text] # mypy cannot follow module import self._session = self.http.Session() self._session.headers.update(self.headers) self._auth = auth if self._auth: if http_scheme == constants.HTTP: raise ValueError('cannot use authentication with HTTP') self._auth.set_session(self._session) self._handle_retry = handle_retry self.max_attempts = max_attempts
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=prestodb.redirect.GatewayRedirectHandler(), max_attempts=MAX_ATTEMPTS, # type: int request_timeout=constants. DEFAULT_REQUEST_TIMEOUT, # type: Union[float, Tuple[float, float]] handle_retry=exceptions.RetryWithExponentialBackoff(), service_account_file=None, ): # 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.credentials = None self.auth_req = None if service_account_file is not None: import google.auth.transport.requests from google.oauth2 import service_account self.auth_req = google.auth.transport.requests.Request() self.credentials = service_account.Credentials.from_service_account_file( service_account_file, scopes=[constants.GCS_READ_ONLY]) self._http_session.headers.update(self.get_oauth_token()) 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