a
    !fB                     @   sv   d 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 G dd	 d	ejZd
d Zdd Zdd ZdS )z,Connections to gcloud datastore API servers.    N)
connection)GCD_HOST)make_exception)datastore_pb2)
status_pb2c                       s   e Zd ZdZdZdZdZdZd fdd	Zd	d
 Z	dd Z
dddZdddZdddZdd Zdd Zdd Zdd Z  ZS ) 
Connectiona  A connection to the Google Cloud Datastore via the Protobuf API.

    This class should understand only the basic types (and protobufs)
    in method arguments, however should be capable of returning advanced types.

    :type credentials: :class:`oauth2client.client.OAuth2Credentials`
    :param credentials: The OAuth2 Credentials to use for this connection.

    :type http: :class:`httplib2.Http` or class that defines ``request()``.
    :param http: An optional HTTP object to make requests.

    :type api_base_url: string
    :param api_base_url: The base of the API call URL. Defaults to
                         :attr:`API_BASE_URL`.
    z https://datastore.googleapis.comZv1beta3z4{api_base}/{api_version}/projects/{project}:{method})z)https://www.googleapis.com/auth/datastoreNc                    sV   t t| j||d |d u rLzdtjt f }W n tyJ   | jj}Y n0 || _	d S )N)credentialshttpz%s/datastore)
superr   __init__osenvironr   KeyError	__class__API_BASE_URLapi_base_url)selfr   r	   r   r    \/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/datastore/connection.pyr   8   s    zConnection.__init__c                 C   sh   dt t|| jd}| jj| j||dd||d\}}|d }|dkrdtj|}t	||j
dd	|S )
a  Make a request over the Http transport to the Cloud Datastore API.

        :type project: string
        :param project: The project to make the request for.

        :type method: string
        :param method: The API call method name (ie, ``runQuery``,
                       ``lookup``, etc)

        :type data: string
        :param data: The data to send with the API call.
                     Typically this is a serialized Protobuf string.

        :rtype: string
        :returns: The string response content from the API call.
        :raises: :class:`gcloud.exceptions.GCloudError` if the response
                 code is not 200 OK.
        zapplication/x-protobuf)zContent-TypezContent-Lengthz
User-Agent)projectmethodPOST)urir   headersbodystatusZ200F)Zuse_json)strlenZ
USER_AGENTr	   requestbuild_api_urlr   Status
FromStringr   message)r   r   r   datar   contentr   Zerror_statusr   r   r   _requestC   s    

zConnection._requestc                 C   s   | j ||| d}||S )a  Make a protobuf RPC request.

        :type project: string
        :param project: The project to connect to. This is
                        usually your project name in the cloud console.

        :type method: string
        :param method: The name of the method to invoke.

        :type request_pb: :class:`google.protobuf.message.Message` instance
        :param request_pb: the protobuf instance representing the request.

        :type response_pb_cls: A :class:`google.protobuf.message.Message`
                               subclass.
        :param response_pb_cls: The class used to unmarshall the response
                                protobuf.

        :rtype: :class:`google.protobuf.message.Message`
        :returns: The RPC message parsed from the response.
        )r   r   r$   )r&   ZSerializeToStringr"   )r   r   r   Z
request_pbZresponse_pb_clsresponser   r   r   _rpcf   s    zConnection._rpcc                 C   s    | j j|p| j|p| j||dS )a5  Construct the URL for a particular API call.

        This method is used internally to come up with the URL to use when
        making RPCs to the Cloud Datastore API.

        :type project: string
        :param project: The project to connect to. This is
                        usually your project name in the cloud console.

        :type method: string
        :param method: The API method to call (e.g. 'runQuery', 'lookup').

        :type base_url: string
        :param base_url: The base URL where the API lives.
                         You shouldn't have to provide this.

        :type api_version: string
        :param api_version: The version of the API to connect to.
                            You shouldn't have to provide this.

        :rtype: str
        :returns: The API URL created.
        )Zapi_baseapi_versionr   r   )API_URL_TEMPLATEformatr   API_VERSION)r   r   r   base_urlr)   r   r   r   r       s
    zConnection.build_api_urlFc           	      C   sb   t  }t||| t|j| | |d|t j}dd |jD }dd |jD }||t	|j
fS )a  Lookup keys from a project in the Cloud Datastore.

        Maps the ``DatastoreService.Lookup`` protobuf RPC.

        This uses mostly protobufs
        (:class:`gcloud.datastore._generated.entity_pb2.Key` as input and
        :class:`gcloud.datastore._generated.entity_pb2.Entity` as output). It
        is used under the hood in
        :meth:`Client.get() <.datastore.client.Client.get>`:

        >>> from gcloud import datastore
        >>> client = datastore.Client(project='project')
        >>> key = client.key('MyKind', 1234)
        >>> client.get(key)
        [<Entity object>]

        Using a :class:`Connection` directly:

        >>> connection.lookup('project', [key.to_protobuf()])
        [<Entity protobuf>]

        :type project: string
        :param project: The project to look up the keys in.

        :type key_pbs: list of
                       :class:`gcloud.datastore._generated.entity_pb2.Key`
        :param key_pbs: The keys to retrieve from the datastore.

        :type eventual: bool
        :param eventual: If False (the default), request ``STRONG`` read
                         consistency.  If True, request ``EVENTUAL`` read
                         consistency.

        :type transaction_id: string
        :param transaction_id: If passed, make the request in the scope of
                               the given transaction.  Incompatible with
                               ``eventual==True``.

        :rtype: tuple
        :returns: A triple of (``results``, ``missing``, ``deferred``) where
                  both ``results`` and ``missing`` are lists of
                  :class:`gcloud.datastore._generated.entity_pb2.Entity` and
                  ``deferred`` is a list of
                  :class:`gcloud.datastore._generated.entity_pb2.Key`.
        lookupc                 S   s   g | ]
}|j qS r   entity.0resultr   r   r   
<listcomp>       z%Connection.lookup.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   r/   r1   r   r   r   r4      r5   )_datastore_pb2ZLookupRequest_set_read_options_add_keys_to_requestkeysr(   ZLookupResponsefoundmissinglistdeferred)	r   r   key_pbseventualtransaction_idZlookup_requestZlookup_responseresultsr;   r   r   r   r.      s    /
zConnection.lookupc                 C   sd   t  }t||| |r ||j_|j| | |d|t j}dd |j	j
D |j	j|j	j|j	jfS )a	  Run a query on the Cloud Datastore.

        Maps the ``DatastoreService.RunQuery`` protobuf RPC.

        Given a Query protobuf, sends a ``runQuery`` request to the
        Cloud Datastore API and returns a list of entity protobufs
        matching the query.

        You typically wouldn't use this method directly, in favor of the
        :meth:`gcloud.datastore.query.Query.fetch` method.

        Under the hood, the :class:`gcloud.datastore.query.Query` class
        uses this method to fetch data:

        >>> from gcloud import datastore
        >>> client = datastore.Client()
        >>> query = client.query(kind='MyKind')
        >>> query.add_filter('property', '=', 'val')

        Using the query iterator's
        :meth:`next_page() <.datastore.query.Iterator.next_page>` method:

        >>> query_iter = query.fetch()
        >>> entities, more_results, cursor = query_iter.next_page()
        >>> entities
        [<list of Entity unmarshalled from protobuf>]
        >>> more_results
        <boolean of more results>
        >>> cursor
        <string containing cursor where fetch stopped>

        Under the hood this is doing:

        >>> connection.run_query('project', query.to_protobuf())
        [<list of Entity Protobufs>], cursor, more_results, skipped_results

        :type project: string
        :param project: The project over which to run the query.

        :type query_pb: :class:`gcloud.datastore._generated.query_pb2.Query`
        :param query_pb: The Protobuf representing the query to run.

        :type namespace: string
        :param namespace: The namespace over which to run the query.

        :type eventual: bool
        :param eventual: If False (the default), request ``STRONG`` read
                         consistency.  If True, request ``EVENTUAL`` read
                         consistency.

        :type transaction_id: string
        :param transaction_id: If passed, make the request in the scope of
                               the given transaction.  Incompatible with
                               ``eventual==True``.

        :rtype: tuple
        :returns: Four-tuple containing the entities returned,
                  the end cursor of the query, a ``more_results``
                  enum and a count of the number of skipped results.
        ZrunQueryc                 S   s   g | ]
}|j qS r   r/   )r2   er   r   r   r4      r5   z(Connection.run_query.<locals>.<listcomp>)r6   ZRunQueryRequestr7   Zpartition_idZnamespace_idqueryCopyFromr(   ZRunQueryResponsebatchZentity_resultsZ
end_cursorZmore_resultsZskipped_results)r   r   Zquery_pb	namespacer?   r@   r   r'   r   r   r   	run_query   s    >
zConnection.run_queryc                 C   s    t  }| |d|t j}|jS )a  Begin a transaction.

        Maps the ``DatastoreService.BeginTransaction`` protobuf RPC.

        :type project: string
        :param project: The project to which the transaction applies.

        :rtype: bytes
        :returns: The serialized transaction that was begun.
        ZbeginTransaction)r6   ZBeginTransactionRequestr(   ZBeginTransactionResponsetransaction)r   r   r   r'   r   r   r   begin_transaction&  s
    
zConnection.begin_transactionc                 C   s:   |rt jj|_||_n
t jj|_| |d|t j}t|S )a  Commit mutations in context of current transation (if any).

        Maps the ``DatastoreService.Commit`` protobuf RPC.

        :type project: string
        :param project: The project to which the transaction applies.

        :type request: :class:`._generated.datastore_pb2.CommitRequest`
        :param request: The protobuf with the mutations being committed.

        :type transaction_id: string or None
        :param transaction_id: The transaction ID returned from
                               :meth:`begin_transaction`.  Non-transactional
                               batches must pass ``None``.

        .. note::

            This method will mutate ``request`` before using it.

        :rtype: tuple
        :returns: The pair of the number of index updates and a list of
                  :class:`._generated.entity_pb2.Key` for each incomplete key
                  that was completed in the commit.
        commit)	r6   ZCommitRequestZTRANSACTIONALmoderH   ZNON_TRANSACTIONALr(   ZCommitResponse_parse_commit_response)r   r   r   r@   r'   r   r   r   rJ   6  s    


zConnection.commitc                 C   s$   t  }||_| |d|t j dS )a{  Rollback the connection's existing transaction.

        Maps the ``DatastoreService.Rollback`` protobuf RPC.

        :type project: string
        :param project: The project to which the transaction belongs.

        :type transaction_id: string
        :param transaction_id: The transaction ID returned from
                               :meth:`begin_transaction`.
        rollbackN)r6   ZRollbackRequestrH   r(   ZRollbackResponse)r   r   r@   r   r   r   r   rM   Y  s
    
zConnection.rollbackc                 C   s0   t  }t|j| | |d|t j}t|jS )a1  Obtain backend-generated IDs for a set of keys.

        Maps the ``DatastoreService.AllocateIds`` protobuf RPC.

        :type project: string
        :param project: The project to which the transaction belongs.

        :type key_pbs: list of
                       :class:`gcloud.datastore._generated.entity_pb2.Key`
        :param key_pbs: The keys for which the backend should allocate IDs.

        :rtype: list of :class:`gcloud.datastore._generated.entity_pb2.Key`
        :returns: An equal number of keys,  with IDs filled in by the backend.
        ZallocateIds)r6   ZAllocateIdsRequestr8   r9   r(   ZAllocateIdsResponser<   )r   r   r>   r   r'   r   r   r   allocate_idsk  s    
zConnection.allocate_ids)NNN)NN)FN)NFN)__name__
__module____qualname____doc__r   r,   r*   ZSCOPEr   r&   r(   r    r.   rG   rI   rJ   rM   rN   __classcell__r   r   r   r   r      s(   #  
 
;  
N#r   c                 C   s8   |r|durt d| j}|r*tjj|_n
|r4||_dS )zValidate rules for read options, and assign to the request.

    Helper method for ``lookup()`` and ``run_query``.

    :raises: :class:`ValueError` if ``eventual`` is ``True`` and the
             ``transaction_id`` is not ``None``.
    Nz,eventual must be False when in a transaction)
ValueErrorZread_optionsr6   ZReadOptionsZEVENTUALZread_consistencyrH   )r   r?   r@   optsr   r   r   r7     s    r7   c                 C   s   |D ]}|   | qdS )a4  Add protobuf keys to a request object.

    :type request_field_pb: `RepeatedCompositeFieldContainer`
    :param request_field_pb: A repeated proto field that contains keys.

    :type key_pbs: list of :class:`gcloud.datastore._generated.entity_pb2.Key`
    :param key_pbs: The keys to add to a request.
    N)addrD   )Zrequest_field_pbr>   Zkey_pbr   r   r   r8     s    	r8   c                 C   s"   | j }| j}dd |D }||fS )a  Extract response data from a commit response.

    :type commit_response_pb: :class:`._generated.datastore_pb2.CommitResponse`
    :param commit_response_pb: The protobuf response from a commit request.

    :rtype: tuple
    :returns: The pair of the number of index updates and a list of
              :class:`._generated.entity_pb2.Key` for each incomplete key
              that was completed in the commit.
    c                 S   s   g | ]}| d r|jqS )key)ZHasFieldrW   )r2   Z
mut_resultr   r   r   r4     s   
z*_parse_commit_response.<locals>.<listcomp>)Zmutation_resultsindex_updates)Zcommit_response_pbZmut_resultsrX   Zcompleted_keysr   r   r   rL     s    rL   )rR   r   Zgcloudr   Zgcloud.environment_varsr   Zgcloud.exceptionsr   Zgcloud.datastore._generatedr   r6   Z
google.rpcr   r   r7   r8   rL   r   r   r   r   <module>   s     j