Skip to content

hdx.utilities.session

module hdx.utilities.session

Session utilities for urls.

Classes

Functions

  • get_session Set up and return Session object that is set up with retrying. Requires either global user agent to be set or appropriate user agent parameter(s) to be completed. If the EXTRA_PARAMS, BASIC_AUTH or BEARER_TOKEN environment variable is supplied, the extra_params* parameters will be ignored. extra_params_dict takes precedence over extra_params_json and extra_params_yaml. extra_params_lookup, if supplied, only applies to extra_params_json and extra_params_yaml.

class SessionError()

Bases : Exception

get_session(user_agent: str | None = None, user_agent_config_yaml: Path | str | None = None, user_agent_lookup: str | None = None, use_env: bool = True, fail_on_missing_file: bool = True, verify: bool = True, retry_attempts: int = 5, backoff_factor: int = 1, **kwargs: Any)requests.Session

Set up and return Session object that is set up with retrying. Requires either global user agent to be set or appropriate user agent parameter(s) to be completed. If the EXTRA_PARAMS, BASIC_AUTH or BEARER_TOKEN environment variable is supplied, the extra_params* parameters will be ignored. extra_params_dict takes precedence over extra_params_json and extra_params_yaml. extra_params_lookup, if supplied, only applies to extra_params_json and extra_params_yaml.

Parameters

  • user_agent : str | None User agent string. HDXPythonUtilities/X.X.X- is prefixed.

  • user_agent_config_yaml : Path | str | None Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yaml.

  • user_agent_lookup : str | None Lookup key for YAML. Ignored if user_agent supplied.

  • use_env : bool Whether to read environment variables. Defaults to True.

  • fail_on_missing_file : bool Raise an exception if any specified configuration files are missing. Defaults to True.

  • verify : bool Whether to verify SSL certificates. Defaults to True.

  • retry_attempts : int Number of retry attempts. Defaults to 5.

  • backoff_factor : int Backoff factor for retry. Defaults to 1 (0s, 2s, 4s, 8s, 16s, 32s).

  • **kwargs : Any See below

  • auth : tuple[str, str] Authorisation information in tuple form (user, pass) OR

  • basic_auth : str Authorisation information in basic auth string form (Basic xxxxxxxxxxxxxxxx) OR

  • basic_auth_file : str Path to file containing authorisation information in basic auth string form (Basic xxxxxxxxxxxxxxxx) OR

  • bearer_token : str Bearer token string OR

  • bearer_token_file : str Path to file containing bearer token string OR

  • extra_params_dict : dict Extra parameters to put on end of url as a dictionary OR

  • extra_params_json : str Path to JSON file containing extra parameters to put on end of url OR

  • extra_params_yaml : str Path to YAML file containing extra parameters to put on end of url

  • extra_params_lookup : str Lookup key for parameters. If not given assumes parameters are at root of the dict.

  • headers : dict Additional headers to add to request.

  • use_auth : str If more than one auth found, specify which one to use, rather than failing.

  • status_forcelist : Sequence[int] HTTP statuses for which to force retry. Defaults to (429, 500, 502, 503, 504).

  • allowed_methods : Sequence[str] HTTP methods for which to force retry. Defaults to ("HEAD", "TRACE", "GET", "PUT", "OPTIONS", "DELETE").

Raises