o
    Mf/                     @   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 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_accountzThis 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   sN   d|v rt dtj|}| jrd|vr|d|d< ||d< | |i |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   ^/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/google/cloud/client/__init__.pyr   6   s   z-_ClientFactoryMixin.from_service_account_infoc                 O   sR   t j|ddd}t|}W d   n1 sw   Y  | j|g|R i |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)encodingN)ioopenjsonloadr   )r   json_credentials_pathr   r   json_ficredentials_infor   r   r   from_service_account_jsonT   s   z-_ClientFactoryMixin.from_service_account_jsonN)__name__
__module____qualname____doc__r   classmethodr   r"   r   r   r   r   r	   ,   s    
r	   c                   @   sV   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 u rtjj }|r"|jr"tjj	d|r0t |tj
jjs0tt|jp5| j}|sT|d u rT|jrKtj
j|j|d\}}n	tj
j|d\}}tj
jj||d| _|jri| j|j| _|| _|j| _d S )NzK'credentials' and 'client_options.credentials_file' are mutually exclusive.)scopes)
isinstancedictgoogleapi_coreclient_options	from_dictClientOptionscredentials_file
exceptionsDuplicateCredentialArgsauthr
   r   
ValueError_GOOGLE_AUTH_CREDENTIALS_HELPr*   r)   load_credentials_from_filedefaultwith_scopes_if_required_credentialsquota_project_idwith_quota_project_http_internalclient_cert_source_client_cert_source)selfr
   _httpr/   r*   _r   r   r   __init__   s8   


zClient.__init__c                 C   s   t dddg)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.)r   joinrA   r   r   r   __getstate__   s   zClient.__getstate__c                 C   s6   | j du rt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)refresh_timeout)
r>   r-   r5   	transportrequestsAuthorizedSessionr;   _CREDENTIALS_REFRESH_TIMEOUTconfigure_mtls_channelr@   rG   r   r   r   rB      s   

zClient._httpc                 C   s   | j dur| j   dS 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)r>   closerG   r   r   r   rO      s   
zClient.close)NNN)r#   r$   r%   r&   r)   r   r   str__annotations__rD   rH   propertyrB   rO   r   r   r   r   r(   p   s   
 "
'
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 u rt tjt tj}|d u r|d urt|dd }|d u r&| |}|d u r.tdt|t	r8|
d}t|tsAt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   PROJECTLEGACY_PROJECTgetattr_determine_defaultEnvironmentErrorr+   bytesdecoderP   r6   r   )rA   r   r
   r   r   r   rD      s$   





z_ClientProjectMixin.__init__c                 C   s   t | S )z'Helper:  use default project detection.r   )r   r   r   r   rY     s   z&_ClientProjectMixin._determine_default)NN)r#   r$   r%   r&   rD   staticmethodrY   r   r   r   r   rS      s
    
!rS   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/   rB   )rS   rD   r(   )rA   r   r
   r/   rB   r   r   r   rD   ?  s   
zClientWithProject.__init__)NNNN)r#   r$   r%   r&   r   rD   r   r   r   r   r^   "  s    r^   )r&   r   r   rT   pickler   typingr   r   google.api_core.client_optionsr-   google.api_core.exceptionsgoogle.authr   google.auth.credentialsgoogle.auth.transport.requestsgoogle.cloud._helpersr   google.oauth2r   r7   rM   objectr	   r(   rS   r^   r   r   r   r   <module>   s,   Dz8