U
    $FZh2                     @   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	Z
ddlZ
ddlZ
ddlmZ ddlZ
ddlZ
ddlmZ ddlmZ zddlZ
d	ZW n ek
r   d
ZY nX dZdZG dd deZG dd deZG dd deZG dd deeZdS )z@Base classes for client used to interact with Google Cloud APIs.    N)PicklingError)Tuple)Union)environment_vars_determine_default_project)service_accountTFzThis library only supports credentials from google-auth-library-python. See https://google-auth.readthedocs.io/en/latest/ for help on authentication with this library.i,  c                   @   s,   e Zd ZdZdZedd Zedd ZdS )_ClientFactoryMixinzeMixin to allow factories that create credentials.

    .. note::

        This class is virtual.
    Fc                 O   sJ   d|krt dtj|}| jr8d|kr8|d|d< ||d< | ||S )a  Factory to retrieve JSON credentials while creating client.

        :type info: dict
        :param info:
            The JSON object with a private key and other credentials
            information (downloaded from the Google APIs console).

        :type args: tuple
        :param args: Remaining positional arguments to pass to constructor.

        :param kwargs: Remaining keyword arguments to pass to constructor.

        :rtype: :class:`_ClientFactoryMixin`
        :returns: The client created with the retrieved JSON credentials.
        :raises TypeError: if there is a conflict with the kwargs
                 and the credentials created by the factory.
        credentialsz,credentials must not be in keyword argumentsproject
project_id)	TypeErrorr   Credentialsfrom_service_account_info_SET_PROJECTget)clsinfoargskwargsr
    r   c/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/google/cloud/client/__init__.pyr   =   s    z-_ClientFactoryMixin.from_service_account_infoc              	   O   s8   t j|ddd}t|}W 5 Q R X | j|f||S )a  Factory to retrieve JSON credentials while creating client.

        :type json_credentials_path: str
        :param json_credentials_path: The path to a private key file (this file
                                      was given to you when you created the
                                      service account). This file must contain
                                      a JSON object with a private key and
                                      other credentials information (downloaded
                                      from the Google APIs console).

        :type args: tuple
        :param args: Remaining positional arguments to pass to constructor.

        :param kwargs: Remaining keyword arguments to pass to constructor.

        :rtype: :class:`_ClientFactoryMixin`
        :returns: The client created with the retrieved JSON credentials.
        :raises TypeError: if there is a conflict with the kwargs
                 and the credentials created by the factory.
        rutf-8)encoding)ioopenjsonloadr   )r   Zjson_credentials_pathr   r   Zjson_fiZcredentials_infor   r   r   from_service_account_json[   s    z-_ClientFactoryMixin.from_service_account_jsonN)__name__
__module____qualname____doc__r   classmethodr   r   r   r   r   r   r	   3   s   
r	   c                   @   sT   e Zd ZU dZdZeeedf df ed< dddZ	dd Z
ed	d
 Zdd ZdS )Clienta  Client to bundle configuration needed for API requests.

    Stores ``credentials`` and an HTTP object so that subclasses
    can pass them along to a connection class.

    If no value is passed in for ``_http``, a :class:`requests.Session` object
    will be created and authorized with the ``credentials``. If not, the
    ``credentials`` and ``_http`` need not be related.

    Callers and subclasses may seek to use the private key from
    ``credentials`` to sign data.

    Args:
        credentials (google.auth.credentials.Credentials):
            (Optional) The OAuth2 Credentials to use for this client. If not
            passed (and if no ``_http`` object is passed), falls back to the
            default inferred from the environment.
        client_options (google.api_core.client_options.ClientOptions):
            (Optional) Custom options for the client.
        _http (requests.Session):
            (Optional) HTTP object to make requests. Can be any object that
            defines ``request()`` with the same interface as
            :meth:`requests.Session.request`. If not passed, an ``_http``
            object is created that is bound to the ``credentials`` for the
            current object.
            This parameter should be considered private, and could change in
            the future.

    Raises:
        google.auth.exceptions.DefaultCredentialsError:
            Raised if ``credentials`` is not specified and the library fails
            to acquire default credentials.
    N.SCOPEc                 C   s(  t |trtjj|}|d kr,tjj }|rD|jrDtjj	dt
rf|jrf|sX|jrftjj	d|rt |tjjjstt|jp| j}|s|d kr|jrtjj|j|d\}}n2t
r|jd k	rtjj|j}ntjj|d\}}tjjj||d| _|jr| j|j| _|| _|j| _d S )NzK'credentials' and 'client_options.credentials_file' are mutually exclusive.zh'client_options.api_key' is mutually exclusive with 'credentials' and 'client_options.credentials_file'.)scopes)
isinstancedictgoogleZapi_coreclient_options	from_dictZClientOptionsZcredentials_file
exceptionsZDuplicateCredentialArgsHAS_GOOGLE_AUTH_API_KEYZapi_keyauthr
   r   
ValueError_GOOGLE_AUTH_CREDENTIALS_HELPr'   r&   Zload_credentials_from_filedefaultZwith_scopes_if_required_credentialsZquota_project_idZwith_quota_project_http_internalZclient_cert_source_client_cert_source)selfr
   _httpr+   r'   _r   r   r   __init__   sX    

   zClient.__init__c                 C   s   t dddgdS )z1Explicitly state that clients are not pickleable.
z4Pickling client objects is explicitly not supported.z>Clients have non-trivial state that is local and unpickleable.N)r   joinr6   r   r   r   __getstate__   s    zClient.__getstate__c                 C   s6   | j dkr0tjjjj| jtd| _ | j | j	 | j S )zGetter for object used for HTTP transport.

        :rtype: :class:`~requests.Session`
        :returns: An HTTP object.
        N)Zrefresh_timeout)
r4   r*   r/   	transportrequestsZAuthorizedSessionr3   _CREDENTIALS_REFRESH_TIMEOUTZconfigure_mtls_channelr5   r<   r   r   r   r7      s    

zClient._httpc                 C   s   | j dk	r| j   dS )zClean up transport, if set.

        Suggested use:

        .. code-block:: python

           import contextlib

           with contextlib.closing(client):  # closes on exit
               do_something_with(client)
        N)r4   closer<   r   r   r   rA      s    
zClient.close)NNN)r    r!   r"   r#   r&   r   r   str__annotations__r9   r=   propertyr7   rA   r   r   r   r   r%   w   s   
"
2
r%   c                   @   s&   e Zd ZdZdddZedd ZdS )_ClientProjectMixinai  Mixin to allow setting the project on the client.

    :type project: str
    :param project:
        (Optional) the project which the client acts on behalf of. If not
        passed, falls back to the default inferred from the environment.

    :type credentials: :class:`google.auth.credentials.Credentials`
    :param credentials:
        (Optional) credentials used to discover a project, if not passed.

    :raises: :class:`EnvironmentError` if the project is neither passed in nor
             set on the credentials or in the environment. :class:`ValueError`
             if the project value is invalid.
    Nc                 C   s   |d krt tjt tj}|d kr:|d k	r:t|dd }|d krL| |}|d kr\tdt|t	rp|
d}t|tstd|| _d S )Nr   zHProject was not passed and could not be determined from the environment.r   zProject must be a string.)osgetenvr   ZPROJECTZLEGACY_PROJECTgetattr_determine_defaultEnvironmentErrorr(   bytesdecoderB   r0   r   )r6   r   r
   r   r   r   r9     s$    




z_ClientProjectMixin.__init__c                 C   s   t | S )z'Helper:  use default project detection.r   )r   r   r   r   rI   .  s    z&_ClientProjectMixin._determine_default)NN)r    r!   r"   r#   r9   staticmethodrI   r   r   r   r   rE      s   
!rE   c                   @   s   e Zd ZdZdZdddZdS )ClientWithProjecta  Client that also stores a project.

    :type project: str
    :param project: the project which the client acts on behalf of. If not
                    passed falls back to the default inferred from the
                    environment.

    :type credentials: :class:`~google.auth.credentials.Credentials`
    :param credentials: (Optional) The OAuth2 Credentials to use for this
                        client. If not passed (and if no ``_http`` object is
                        passed), falls back to the default inferred from the
                        environment.

    :type _http: :class:`~requests.Session`
    :param _http: (Optional) HTTP object to make requests. Can be any object
                  that defines ``request()`` with the same interface as
                  :meth:`~requests.Session.request`. If not passed, an
                  ``_http`` object is created that is bound to the
                  ``credentials`` for the current object.
                  This parameter should be considered private, and could
                  change in the future.

    :raises: :class:`ValueError` if the project is neither passed in nor
             set in the environment.
    TNc                 C   s&   t j| ||d tj| |||d d S )N)r   r
   )r
   r+   r7   )rE   r9   r%   )r6   r   r
   r+   r7   r   r   r   r9   Q  s       zClientWithProject.__init__)NNNN)r    r!   r"   r#   r   r9   r   r   r   r   rN   4  s   rN   )r#   r   r   rF   pickler   typingr   r   Zgoogle.api_core.client_optionsr*   Zgoogle.api_core.exceptionsZgoogle.authr   Zgoogle.auth.credentialsZgoogle.auth.transport.requestsZgoogle.cloud._helpersr   Zgoogle.oauth2r   Zgoogle.auth.api_keyr.   ImportErrorr1   r@   objectr	   r%   rE   rN   r   r   r   r   <module>   s6   
D 8