HTTP ONS¶
OnsHttpConnProperties(host, api_key=None, user=None, password=None, conn_timeout=30, request_timeout=300, verify_ssl=False, headers=(lambda: {'content-type': 'application/json', 'accept': 'application/json', 'accept-language': 'en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7'})(), payload_kwargs=None, payload_kwargs_key='json', user_agent=_DEFAULT_USER_AGENT, login_target='SAGER')
dataclass
¶
Subclass of HttpConnProperties used for Ons, adding login_target attribute.
Parameters:
-
(host¶str) –The hostname of the database server.
-
(login_target¶Literal['SINAPSE', 'SAGER'], default:'SAGER') –The target login endpoint. Currently only supports "SINAPSE" and "SAGER".
By default "SAGER".
-
(user¶str | None, default:None) –User name. This will only be used if api_key is not provided. By default None.
-
(password¶str | None, default:None) –Password for the user. This will only be used if api_key is not provided. By default None.
-
(conn_timeout¶int, default:30) –Timeout in seconds for connection, by default 30
-
(request_timeout¶int, default:300) –Query timeout in seconds, by default 300
-
(verify_ssl¶bool, default:False) –If True, SSL certificates will be verified, by default False
-
(headers¶dict | None, default:(lambda: {'content-type': 'application/json', 'accept': 'application/json', 'accept-language': 'en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7'})()) –Dictionary with headers to be added to the request.
By default {"content-type": "application/json", "accept": "application/json"}.
-
(payload_kwargs¶dict | None, default:None) –Dictionary with key-word arguments to be added to the payload of every request made to the server.
Mainly used in subclasses that require a token at every request.
By default None.
-
(payload_kwargs_key¶str, default:'json') –Key to be used in the payload kwargs. By default "json".
This defines the key to be used in the payload kwargs. Usually this is "json" but in some cases it can be "data" or "params".
By default "json".
-
(api_key¶str | None, default:None) –Do not use, not applicable to ONS login.
__post_init__()
¶
Method that checks if inputs are valid after initialization.
Source code in echo_connhandler/http_ons.py
def __post_init__(self) -> None:
"""Method that checks if inputs are valid after initialization."""
if not isinstance(self.login_target, str) or self.login_target not in [
"SINAPSE",
"SAGER",
]:
raise ValueError(
f"Invalid login target {self.login_target}. Supported targets are 'SINAPSE' and 'SAGER'.",
)
OnsHttpHandler(connection_properties, max_retries=8, max_conn_retries=3, retry_wait_time=30, exponential_min_retry_wait_time=0.1, exponential_multiplier=1, skip_connect=False, **kwargs)
¶
Subclass of HttpHandler used for Ons, customizing the connection method to allow for user and password authentication.
This already connects to the HTTP server.
This handler implements a robust retry strategy using exponential backoff to gracefully manage connection or query failures. By default, operations are attempted up to 8 times; between failures, the wait time doubles exponentially—starting at 0.1 second (0.1s, 0.2s, 0.4s, 0.8s, 1.6s, 3.2s, 6.4s, 12.8s )—until it reaches a configured ceiling of 30 seconds. This approach allows for rapid recovery from momentary glitches while preventing server overload during persistent outages.
Using the default parameters the connection will fail after around 90 seconds if the server is not reachable.
Parameters:
-
(connection_properties¶OnsHttpConnProperties) –Object containing connection parameters.
-
(max_retries¶int, default:8) –Number of retries that will be attempted when doing queries. Will be used in
stopparameter of tenacity.stop_after_attempt, by default 8 -
(max_conn_retries¶int, default:3) –Number of retries that will be attempted when reconnecting. Will be used in
stopparameter of tenacity.stop_after_attempt, by default 3 -
(exponential_min_retry_wait_time¶float, default:0.1) –Min time to wait between retries when reconnecting or doing queries. Will be used in
minparameter of tenacity.wait_exponential, by default 0.1 -
(exponential_multiplier¶float, default:1) –Multiplier to use when calculating wait time between retries when reconnecting or doing queries. Will be used in
multiplierparameter of tenacity.wait_exponential, by default 1 -
(skip_connect¶bool, default:False) –If True, the connection will not be established when the object is created.
If this is set toTrue, the user will need to manually call the reconnect() method when they want to connect to the server.
By default False
-
(**kwargs¶dict, default:{}) –Just kept here for compatibility.
Source code in echo_connhandler/http_ons.py
@validate_call
def __init__(
self,
connection_properties: OnsHttpConnProperties,
max_retries: int = 8,
max_conn_retries: int = 3,
retry_wait_time: float = 30,
exponential_min_retry_wait_time: float = 0.1,
exponential_multiplier: float = 1,
skip_connect: bool = False,
**kwargs, # pylint: disable=unused-argument # noqa
) -> None:
"""Method that initializes the HTTP handler.
This already connects to the HTTP server.
This handler implements a robust retry strategy using exponential backoff to gracefully manage connection or query failures. By default, operations are attempted up to 8 times; between failures, the wait time doubles exponentially—starting at 0.1 second (0.1s, 0.2s, 0.4s, 0.8s, 1.6s, 3.2s, 6.4s, 12.8s )—until it reaches a configured ceiling of 30 seconds. This approach allows for rapid recovery from momentary glitches while preventing server overload during persistent outages.
Using the default parameters the connection will fail after around 90 seconds if the server is not reachable.
Parameters
----------
connection_properties : OnsHttpConnProperties
Object containing connection parameters.
max_retries : int, optional
Number of retries that will be attempted when doing queries. Will be used in `stop` parameter of tenacity.stop_after_attempt, by default 8
max_conn_retries : int, optional
Number of retries that will be attempted when reconnecting. Will be used in `stop` parameter of tenacity.stop_after_attempt, by default 3
exponential_min_retry_wait_time: float, optional
Min time to wait between retries when reconnecting or doing queries. Will be used in `min` parameter of tenacity.wait_exponential, by default 0.1
exponential_multiplier: float, optional
Multiplier to use when calculating wait time between retries when reconnecting or doing queries. Will be used in `multiplier` parameter of tenacity.wait_exponential, by default 1
skip_connect : bool, optional
If True, the connection will not be established when the object is created.
If this is set toTrue, the user will need to manually call the reconnect() method when they want to connect to the server.
By default False
**kwargs : dict, optional
Just kept here for compatibility.
"""
super().__init__(
connection_properties=connection_properties,
max_retries=max_retries,
max_conn_retries=max_conn_retries,
retry_wait_time=retry_wait_time,
exponential_min_retry_wait_time=exponential_min_retry_wait_time,
exponential_multiplier=exponential_multiplier,
skip_connect=skip_connect,
)