Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add static code for supporting universe domain (to be used in both GAPICs and Apiary) #624

Closed
@ohmayr

Description

@ohmayr

Universes other than the default "googleapis.com" need to be supported in both GAPICs and Apiary. The following code was added to GAPICs but can be moved to python-api-core:

def _get_universe_domain(
    client_universe_domain: Optional[str], universe_domain_env: Optional[str]
) -> str:  # temp disable Optional
    """Return the universe domain used by the client.

    Args:
        client_universe_domain (Optional[str]): The universe domain configured via the client options.
        universe_domain_env (Optional[str]): The universe domain configured via the "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable.

    Returns:
        str: The universe domain to be used by the client.

    Raises:
        ValueError: If the universe domain is an empty string.
    """
    universe_domain = _DEFAULT_UNIVERSE
    if client_universe_domain is not None:
        universe_domain = client_universe_domain
    elif universe_domain_env is not None:
        universe_domain = universe_domain_env
    if len(universe_domain.strip()) == 0:
        raise _Empty_Universe_Error
    return universe_domain


def _compare_universes(
    client_universe: str, credentials: Union[ga_credentials, oauth2_credentials]
) -> bool:
    """Returns True iff the universe domains used by the client and credentials match.

    Args:
        client_universe (str): The universe domain configured via the client options.
        credentials Union[google.auth.credentials.Credentials, oauth2client.client.Credentials]: The credentials being used in the client.

    Returns:
        bool: True iff client_universe matches the universe in credentials.

    Raises:
        ValueError: when client_universe does not match the universe in credentials.
    """
    credentials_universe = getattr(credentials, "universe_domain", _DEFAULT_UNIVERSE)

    if client_universe != credentials_universe:
        raise _UniverseMismatchError(client_universe, credentials_universe)
    return True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions