a
    !fA                     @   s   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	 ddl
mZ dd	lmZ G d
d deZG dd deZdS )zDefine API Topics.    N)_datetime_to_rfc3339)_NOW)NotFound)subscription_name_from_path)topic_name_from_path)PolicySubscriptionc                   @   s   e Zd ZdZd$ddZd%ddZedd	 Zed
d Z	edd Z
dd Zd&ddZd'ddZd(ddZdd Zd)ddZd*ddZd+ddZd,ddZd-d d!Zd.d"d#ZdS )/Topica  Topics are targets to which messages can be published.

    Subscribers then receive those messages.

    See:
    https://cloud.google.com/pubsub/reference/rest/v1/projects.topics

    :type name: string
    :param name: the name of the topic

    :type client: :class:`gcloud.pubsub.client.Client`
    :param client: A client which holds credentials and project configuration
                   for the topic (which requires a project).

    :type timestamp_messages: boolean
    :param timestamp_messages: If true, the topic will add a ``timestamp`` key
                               to the attributes of each published message:
                               the value will be an RFC 3339 timestamp.
    Fc                 C   s   || _ || _|| _d S N)name_clienttimestamp_messages)selfr   clientr    r   T/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/pubsub/topic.py__init__0   s    zTopic.__init__Nc                 C   s   t || ||dS )a2  Creates a subscription bound to the current topic.

        Example:  pull-mode subcription, default paramter values

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_subscription_defaults]
           :end-before: [END topic_subscription_defaults]

        Example:  pull-mode subcription, override ``ack_deadline`` default

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_subscription_ack90]
           :end-before: [END topic_subscription_ack90]

        Example:  push-mode subcription

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_subscription_push]
           :end-before: [END topic_subscription_push]

        :type name: string
        :param name: the name of the subscription

        :type ack_deadline: int
        :param ack_deadline: the deadline (in seconds) by which messages pulled
                             from the back-end must be acknowledged.

        :type push_endpoint: string
        :param push_endpoint: URL to which messages will be pushed by the
                              back-end. If not set, the application must pull
                              messages.

        :rtype: :class:`Subscription`
        :returns: The subscription created with the passed in arguments.
        )ack_deadlinepush_endpointr   )r   r   r   r   r   r   r   subscription5   s    $zTopic.subscriptionc                 C   s   t |d |j}| ||dS )a|  Factory:  construct a topic given its API representation

        :type resource: dict
        :param resource: topic resource representation returned from the API

        :type client: :class:`gcloud.pubsub.client.Client`
        :param client: Client which holds credentials and project
                       configuration for the topic.

        :rtype: :class:`gcloud.pubsub.topic.Topic`
        :returns: Topic parsed from ``resource``.
        :raises: :class:`ValueError` if ``client`` is not ``None`` and the
                 project from the resource does not agree with the project
                 from the client.
        r   )r   )r   project)clsresourcer   Z
topic_namer   r   r   from_api_repr\   s    zTopic.from_api_reprc                 C   s   | j jS )zProject bound to the topic.)r   r   r   r   r   r   r   p   s    zTopic.projectc                 C   s   d| j | jf S )z6Fully-qualified name used in topic / subscription APIszprojects/%s/topics/%s)r   r   r   r   r   r   	full_nameu   s    zTopic.full_namec                 C   s   |du r| j }|S )a}  Check client or verify over-ride.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :rtype: :class:`gcloud.pubsub.client.Client`
        :returns: The client passed in or the currently bound client.
        N)r   r   r   r   r   r   _require_clientz   s    
zTopic._require_clientc                 C   s"   |  |}|j}|j| jd dS )a  API call:  create the topic via a PUT request

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/create

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_create]
           :end-before: [END topic_create]

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.
        Z
topic_pathN)r   publisher_apiZtopic_creater   r   r   apir   r   r   create   s    
zTopic.createc                 C   s@   |  |}|j}z|j| jd W n ty6   Y dS 0 dS dS )ai  API call:  test for the existence of the topic via a GET request

        See
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/get

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_exists]
           :end-before: [END topic_exists]

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :rtype: bool
        :returns: Boolean indicating existence of the topic.
        r   FTN)r   r    Z	topic_getr   r   r!   r   r   r   exists   s    
zTopic.existsc                 C   s"   |  |}|j}|j| jd dS )a
  API call:  delete the topic via a DELETE request

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/delete

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_delete]
           :end-before: [END topic_delete]

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.
        r   N)r   r    Ztopic_deleter   r!   r   r   r   delete   s    
zTopic.deletec                 C   s    | j rd|vrtt |d< dS )zAdd a timestamp to ``attrs``, if the topic is so configured.

        If ``attrs`` already has the key, do nothing.

        Helper method for ``publish``/``Batch.publish``.
        	timestampN)r   r   r   )r   attrsr   r   r   _timestamp_message   s    zTopic._timestamp_messagec                 K   sL   |  |}|j}| | t|d}||d}|| j|g}|d S )aA  API call:  publish a message to a topic via a POST request

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/publish

        Example without message attributes:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_publish_simple_message]
           :end-before: [END topic_publish_simple_message]

        With message attributes:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_publish_message_with_attrs]
           :end-before: [END topic_publish_message_with_attrs]

        :type message: bytes
        :param message: the message payload

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :type attrs: dict (string -> string)
        :param attrs: key-value pairs to send as message attributes

        :rtype: str
        :returns: message ID assigned by the server to the published message
        asciidata
attributesr   )r   r    r(   base64	b64encodedecodetopic_publishr   )r   messager   r'   r"   Z	message_bZmessage_datamessage_idsr   r   r   publish   s    


zTopic.publishc                 C   s   |  |}t| |S )a  Return a batch to use as a context manager.

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_batch]
           :end-before: [END topic_batch]

        .. note::

           The only API request happens during the ``__exit__()`` of the topic
           used as a context manager, and only if the block exits without
           raising an exception.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :rtype: :class:`Batch`
        :returns: A batch to use as a context manager.
        )r   Batchr   r   r   r   batch   s    
zTopic.batchc           
      C   sV   |  |}|j}|| j||\}}g }|D ] }t|| j}	|t|	|  q,||fS )a  List subscriptions for the project associated with this client.

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics.subscriptions/list

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_list_subscriptions]
           :end-before: [END topic_list_subscriptions]

        :type page_size: int
        :param page_size: maximum number of topics to return, If not passed,
                          defaults to a value set by the API.

        :type page_token: string
        :param page_token: opaque marker for the next "page" of topics. If not
                           passed, the API will return the first page of
                           topics.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :rtype: tuple, (list, str)
        :returns: list of :class:`gcloud.pubsub.subscription.Subscription`,
                  plus a "next page token" string:  if not None, indicates that
                  more topics can be retrieved with another call (pass that
                  value as ``page_token``).
        )r   r    Ztopic_list_subscriptionsr   r   r   appendr	   )
r   Z	page_sizeZ
page_tokenr   r"   Z	sub_pathsZ
next_tokenZsubscriptionsZsub_pathZsub_namer   r   r   list_subscriptions  s    
zTopic.list_subscriptionsc                 C   s&   |  |}|j}|| j}t|S )a  Fetch the IAM policy for the topic.

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/getIamPolicy

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_get_iam_policy]
           :end-before: [END topic_get_iam_policy]

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.

        :rtype: :class:`gcloud.pubsub.iam.Policy`
        :returns: policy created from the resource returned by the
                  ``getIamPolicy`` API request.
        )r   iam_policy_apiget_iam_policyr   r   r   )r   r   r"   respr   r   r   r9   A  s    
zTopic.get_iam_policyc                 C   s0   |  |}|j}| }|| j|}t|S )a|  Update the IAM policy for the topic.

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/setIamPolicy

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_set_iam_policy]
           :end-before: [END topic_set_iam_policy]

        :type policy: :class:`gcloud.pubsub.iam.Policy`
        :param policy: the new policy, typically fetched via
                       :meth:`get_iam_policy` and updated in place.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.

        :rtype: :class:`gcloud.pubsub.iam.Policy`
        :returns: updated policy created from the resource returned by the
                  ``setIamPolicy`` API request.
        )r   r8   Zto_api_reprset_iam_policyr   r   r   )r   policyr   r"   r   r:   r   r   r   r;   Z  s
    
zTopic.set_iam_policyc                 C   s"   |  |}|j}|| jt|S )a  Verify permissions allowed for the current user.

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/testIamPermissions

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_check_iam_permissions]
           :end-before: [END topic_check_iam_permissions]

        :type permissions: list of string
        :param permissions: list of permissions to be tested

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.

        :rtype: sequence of string
        :returns: subset of ``permissions`` allowed by current IAM policy.
        )r   r8   Ztest_iam_permissionsr   list)r   Zpermissionsr   r"   r   r   r   check_iam_permissionsx  s
    

zTopic.check_iam_permissions)F)NN)N)N)N)N)N)NNN)N)N)N)__name__
__module____qualname____doc__r   r   classmethodr   propertyr   r   r   r#   r$   r%   r(   r3   r5   r7   r9   r;   r>   r   r   r   r   r
      s(   

'







(

)

r
   c                   @   sB   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdddZ	dS )r4   a2  Context manager:  collect messages to publish via a single API call.

    Helper returned by :meth:Topic.batch

    :type topic: :class:`gcloud.pubsub.topic.Topic`
    :param topic: the topic being published

    :type client: :class:`gcloud.pubsub.client.Client`
    :param client: The client to use.
    c                 C   s   || _ g | _g | _|| _d S r   )topicmessagesr2   r   )r   rE   r   r   r   r   r     s    zBatch.__init__c                 C   s   | S r   r   r   r   r   r   	__enter__  s    zBatch.__enter__c                 C   s   |d u r|    d S r   )commit)r   exc_typeexc_valexc_tbr   r   r   __exit__  s    zBatch.__exit__c                 C   s
   t | jS r   )iterr2   r   r   r   r   __iter__  s    zBatch.__iter__c                 K   s.   | j | | jt|d|d dS )zEmulate publishing a message, but save it.

        :type message: bytes
        :param message: the message payload

        :type attrs: dict (string -> string)
        :param attrs: key-value pairs to send as message attributes
        r)   r*   N)rE   r(   rF   r6   r-   r.   r/   )r   r1   r'   r   r   r   r3     s    	zBatch.publishNc                 C   sT   | j s
dS |du r| j}|j}|| jj| j dd }| j| | j dd= dS )a	  Send saved messages as a single API call.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.
        N)rF   r   r    r0   rE   r   r2   extend)r   r   r"   r2   r   r   r   rH     s    zBatch.commit)N)
r?   r@   rA   rB   r   rG   rL   rN   r3   rH   r   r   r   r   r4     s   
r4   )rB   r-   Zgcloud._helpersr   r   Zgcloud.exceptionsr   Zgcloud.pubsub._helpersr   r   Zgcloud.pubsub.iamr   Zgcloud.pubsub.subscriptionr	   objectr
   r4   r   r   r   r   <module>   s     z