(
    request, path, root=_METADATA_ROOT, params=None, recursive=False, retry_count=5
)
Fetch a resource from the metadata server.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        path (str): The resource to retrieve. For example,
            ``'instance/service-accounts/default'``.
        root (str): The full path to the metadata server root.
        params (Optional[Mapping[str, str]]): A mapping of query parameter
            keys to values.
        recursive (bool): Whether to do a recursive query of metadata. See
            https://cloud.google.com/compute/docs/metadata#aggcontents for more
            details.
        retry_count (int): How many times to attempt connecting to metadata
            server using above timeout.

    Returns:
        Union[Mapping, str]: If the metadata server returns JSON, a mapping of
            the decoded JSON is return. Otherwise, the response content is
            returned as a string.

    Raises:
        google.auth.exceptions.TransportError: if an error occurred while
            retrieving metadata.
Fetch a resource from the metadata server.
Fetch a resource from the metadata server .
def get(
    request, path, root=_METADATA_ROOT, params=None, recursive=False, retry_count=5
):
    """Fetch a resource from the metadata server.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        path (str): The resource to retrieve. For example,
            ``'instance/service-accounts/default'``.
        root (str): The full path to the metadata server root.
        params (Optional[Mapping[str, str]]): A mapping of query parameter
            keys to values.
        recursive (bool): Whether to do a recursive query of metadata. See
            https://cloud.google.com/compute/docs/metadata#aggcontents for more
            details.
        retry_count (int): How many times to attempt connecting to metadata
            server using above timeout.

    Returns:
        Union[Mapping, str]: If the metadata server returns JSON, a mapping of
            the decoded JSON is return. Otherwise, the response content is
            returned as a string.

    Raises:
        google.auth.exceptions.TransportError: if an error occurred while
            retrieving metadata.
    """
    base_url = urlparse.urljoin(root, path)
    query_params = {} if params is None else params

    if recursive:
        query_params["recursive"] = "true"

    url = _helpers.update_query(base_url, query_params)

    retries = 0
    while retries < retry_count:
        try:
            response = request(url=url, method="GET", headers=_METADATA_HEADERS)
            break

        except exceptions.TransportError as e:
            _LOGGER.warning(
                "Compute Engine Metadata server unavailable on "
                "attempt %s of %s. Reason: %s",
                retries + 1,
                retry_count,
                e,
            )
            retries += 1
    else:
        raise exceptions.TransportError(
            "Failed to retrieve {} from the Google Compute Engine "
            "metadata service. Compute Engine Metadata server unavailable".format(url)
        )

    if response.status == http_client.OK:
        content = _helpers.from_bytes(response.data)
        if response.headers["content-type"] == "application/json":
            try:
                return json.loads(content)
            except ValueError as caught_exc:
                new_exc = exceptions.TransportError(
                    "Received invalid JSON from the Google Compute Engine "
                    "metadata service: {:.20}".format(content)
                )
                six.raise_from(new_exc, caught_exc)
        else:
            return content
    else:
        raise exceptions.TransportError(
            "Failed to retrieve {} from the Google Compute Engine "
            "metadata service. Status: {} Response:\n{}".format(
                url, response.status, response.data
            ),
            response,
        )
def get ( request , path , root = _METADATA_ROOT , params = None , recursive = False , retry_count = 5 ) : base_url = urlparse . urljoin ( root , path ) query_params = { } if params is None else params if recursive : query_params [ "recursive" ] = "true" url = _helpers . update_query ( base_url , query_params ) retries = 0 while retries < retry_count : try : response = request ( url = url , method = "GET" , headers = _METADATA_HEADERS ) break except exceptions . TransportError as e : _LOGGER . warning ( "Compute Engine Metadata server unavailable on " "attempt %s of %s. Reason: %s" , retries + 1 , retry_count , e , ) retries += 1 else : raise exceptions . TransportError ( "Failed to retrieve {} from the Google Compute Engine " "metadata service. Compute Engine Metadata server unavailable" . format ( url ) ) if response . status == http_client . OK : content = _helpers . from_bytes ( response . data ) if response . headers [ "content-type" ] == "application/json" : try : return json . loads ( content ) except ValueError as caught_exc : new_exc = exceptions . TransportError ( "Received invalid JSON from the Google Compute Engine " "metadata service: {:.20}" . format ( content ) ) six . raise_from ( new_exc , caught_exc ) else : return content else : raise exceptions . TransportError ( "Failed to retrieve {} from the Google Compute Engine " "metadata service. Status: {} Response:\n{}" . format ( url , response . status , response . data ) , response , )