a
    !fU                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddl	mZ ddl	mZ ded	d
ZddiZedfddZdddZdddZdS )zProvides helper methods for talking to the Compute Engine metadata server.

See https://cloud.google.com/compute/docs/metadata
    N)http_client)parse)_helpers)client)	transportzhttp://{}/computeMetadata/v1/ZGCE_METADATA_ROOTzmetadata.google.internalzMetadata-FlavorZGooglec                 C   sv   t ||}t|d|}tj| |td\}}|jtj	kr`t
|}|d dkrZt|S |S ntd||dS )a  Fetch a resource from the metadata server.

    Args:
        http: an object to be used to make HTTP requests.
        path: A string indicating the resource to retrieve. For example,
            'instance/service-accounts/default'
        root: A string indicating the full path to the metadata server root.
        recursive: A boolean indicating whether to do a recursive query of
            metadata. See
            https://cloud.google.com/compute/docs/metadata#aggcontents

    Returns:
        A dictionary if the metadata server returns JSON, otherwise a string.

    Raises:
        http_client.HTTPException if an error corrured while
        retrieving metadata.
    	recursive)headerszcontent-typezapplication/jsonzTFailed to retrieve {0} from the Google Compute Enginemetadata service. Response:
{1}N)urlparseurljoinr   Z_add_query_parameterr   requestMETADATA_HEADERSstatusr   OKZ_from_bytesjsonloadsHTTPExceptionformat)httppathrootr   urlresponsecontentdecoded r   _/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/contrib/_metadata.pyget%   s    


r   defaultc                 C   s   t | d|ddS )aq  Get information about a service account from the metadata server.

    Args:
        http: an object to be used to make HTTP requests.
        service_account: An email specifying the service account for which to
            look up information. Default will be information for the "default"
            service account of the current compute engine instance.

    Returns:
         A dictionary with information about the specified service account,
         for example:

            {
                'email': '...',
                'scopes': ['scope', ...],
                'aliases': ['default', '...']
            }
    zinstance/service-accounts/{0}/T)r   )r   r   )r   service_accountr   r   r   get_service_account_infoJ   s
    r   c                 C   s4   t | d|}t tj|d d }|d |fS )a#  Fetch an oauth token for the

    Args:
        http: an object to be used to make HTTP requests.
        service_account: An email specifying the service account this token
            should represent. Default will be a token for the "default" service
            account of the current compute engine instance.

    Returns:
         A tuple of (access token, token expiration), where access token is the
         access token as a string and token expiration is a datetime object
         that indicates when the access token will expire.
    z#instance/service-accounts/{0}/tokenZ
expires_in)secondsZaccess_token)r   r   r   Z_UTCNOWdatetime	timedelta)r   r   Z
token_jsonZtoken_expiryr   r   r   	get_tokenc   s    
r#   )r   )r   )__doc__r!   r   osZ	six.movesr   Zsix.moves.urllibr   r	   Zoauth2clientr   r   r   r   getenvZMETADATA_ROOTr   r   r   r#   r   r   r   r   <module>   s   
%
