a
    !f@K                     @   sX   d 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 Subscriptions.    )NotFound)topic_name_from_path)Policy)Messagec                   @   s   e Zd ZdZdZd*ddZed+ddZedd	 Z	ed
d Z
edd Zd,ddZdd Zd-ddZd.ddZd/ddZd0ddZd1ddZd2ddZd3d d!Zd4d"d#Zd5d$d%Zd6d&d'Zd7d(d)ZdS )8Subscriptiona  Subscriptions receive messages published to their topics.

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

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

    :type topic: :class:`gcloud.pubsub.topic.Topic` or ``NoneType``
    :param topic: the topic to which the subscription belongs;  if ``None``,
                  the subscription's topic has been deleted.

    :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.

    :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 topic.
    z_deleted-topic_Nc                 C   sb   |d u r|d u rt d|d ur0|d ur0t d|| _|| _|pD|j| _| jj| _|| _|| _d S )Nz%Pass only one of 'topic' or 'client'.)	TypeErrornametopic_clientprojectZ_projectack_deadlinepush_endpoint)selfr   r	   r   r   client r   [/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/pubsub/subscription.py__init__8   s    
zSubscription.__init__c                 C   s   |du ri }|d }|| j kr$d}n0||}|du rTt||j}|| }||< |d d\}}}}|d}	|di }
|
d}|du r| ||	||dS | |||	|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 a topic.

        :type topics: dict or None
        :param topics: A mapping of topic names -> topics.  If not passed,
                       the subscription will have a newly-created topic.

        :rtype: :class:`gcloud.pubsub.subscription.Subscription`
        :returns: Subscription parsed from ``resource``.
        Nr	   r   /ackDeadlineSeconds
pushConfigpushEndpoint)r   r   r   )_DELETED_TOPIC_PATHgetr   r   r	   split)clsresourcer   ZtopicsZ
topic_pathr	   Z
topic_name_r   r   push_configr   r   r   r   from_api_reprH   s$    



zSubscription.from_api_reprc                 C   s   | j jS )z"Project bound to the subscription.)r
   r   r   r   r   r   r   o   s    zSubscription.projectc                 C   s   d| j | jf S )z.Fully-qualified name used in subscription APIszprojects/%s/subscriptions/%s)r   r   r   r   r   r   	full_namet   s    zSubscription.full_namec                 C   s   d| j f S )z$URL path for the subscription's APIsz/%s)r    r   r   r   r   pathy   s    zSubscription.pathF   c                 C   s   t | |||S )aB  :class:`AutoAck` factory

        :type return_immediately: boolean
        :param return_immediately: passed through to :meth:`Subscription.pull`

        :type max_messages: int
        :param max_messages: passed through to :meth:`Subscription.pull`

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: passed through to :meth:`Subscription.pull` and
                      :meth:`Subscription.acknowledge`.

        :rtype: :class:`AutoAck`
        :returns: the instance created for the given ``ack_id`` and ``message``
        )AutoAck)r   return_immediatelymax_messagesr   r   r   r   auto_ack~   s    zSubscription.auto_ackc                 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 topic of the
                       current subscription.

        :rtype: :class:`gcloud.pubsub.client.Client`
        :returns: The client passed in or the currently bound client.
        N)r
   )r   r   r   r   r   _require_client   s    zSubscription._require_clientc                 C   s.   |  |}|j}|| j| jj| j| j dS )a2  API call:  create the subscription via a PUT request

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

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START subscription_create]
           :end-before: [END subscription_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 subscription's topic.
        N)r'   subscriber_apiZsubscription_creater    r	   r   r   r   r   apir   r   r   create   s    
zSubscription.createc                 C   s>   |  |}|j}z|| j W n ty4   Y dS 0 dS dS )a  API call:  test existence of the subscription via a GET request

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

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START subscription_exists]
           :end-before: [END subscription_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 subscription's topic.

        :rtype: bool
        :returns: Boolean indicating existence of the subscription.
        FTN)r'   r(   subscription_getr    r   r)   r   r   r   exists   s    
zSubscription.existsc                 C   sD   |  |}|j}|| j}|d| _|di }|d| _dS )a<  API call:  sync local subscription configuration via a GET request

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

        Example:

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

        :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 subscription's topic.
        r   r   r   N)r'   r(   r,   r    r   r   r   )r   r   r*   datar   r   r   r   reload   s    
zSubscription.reloadc                 C   s    |  |}|j}|| j dS )a6  API call:  delete the subscription via a DELETE request.

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

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START subscription_delete]
           :end-before: [END subscription_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 subscription's topic.
        N)r'   r(   Zsubscription_deleter    r)   r   r   r   delete   s    
zSubscription.deletec                 C   s(   |  |}|j}|| j| || _dS )a  API call:  update the push endpoint for the subscription.

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

        Example:

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

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

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

        :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 subscription's topic.
        N)r'   r(   Zsubscription_modify_push_configr    r   )r   r   r   r*   r   r   r   modify_push_configuration   s    
z&Subscription.modify_push_configurationc                 C   s.   |  |}|j}|| j||}dd |D S )a  API call:  retrieve messages for the subscription.

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

        Example:

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

        :type return_immediately: boolean
        :param return_immediately: if True, the back-end returns even if no
                                   messages are available;  if False, the API
                                   call blocks until one or more messages are
                                   available.

        :type max_messages: int
        :param max_messages: the maximum number of messages to return.

        :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 subscription's topic.

        :rtype: list of (ack_id, message) tuples
        :returns: sequence of tuples: ``ack_id`` is the ID to be used in a
                  subsequent call to :meth:`acknowledge`, and ``message``
                  is an instance of :class:`gcloud.pubsub.message.Message`.
        c                 S   s"   g | ]}|d  t |d fqS )ZackIdmessage)r   r   ).0infor   r   r   
<listcomp><  s   z%Subscription.pull.<locals>.<listcomp>)r'   r(   Zsubscription_pullr    )r   r$   r%   r   r*   responser   r   r   pull  s    
zSubscription.pullc                 C   s"   |  |}|j}|| j| dS )a  API call:  acknowledge retrieved messages for the subscription.

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

        Example:

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

        :type ack_ids: list of string
        :param ack_ids: ack IDs of messages being acknowledged

        :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 subscription's topic.
        N)r'   r(   Zsubscription_acknowledger    )r   ack_idsr   r*   r   r   r   acknowledge?  s    
zSubscription.acknowledgec                 C   s$   |  |}|j}|| j|| dS )ak  API call:  update acknowledgement deadline for a retrieved message.

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

        :type ack_ids: list of string
        :param ack_ids: ack IDs of messages being updated

        :type ack_deadline: int
        :param ack_deadline: new deadline for the message, in seconds

        :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 subscription's topic.
        N)r'   r(   Z subscription_modify_ack_deadliner    )r   r8   r   r   r*   r   r   r   modify_ack_deadlineV  s
    
z Subscription.modify_ack_deadlinec                 C   s&   |  |}|j}|| j}t|S )a  Fetch the IAM policy for the subscription.

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

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START subscription_get_iam_policy]
           :end-before: [END subscription_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 subscription's topic.

        :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   r<   k  s    
zSubscription.get_iam_policyc                 C   s0   |  |}|j}| }|| j|}t|S )a  Update the IAM policy for the subscription.

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

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START subscription_set_iam_policy]
           :end-before: [END subscription_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 subscription's topic.

        :rtype: :class:`gcloud.pubsub.iam.Policy`
        :returns: updated policy created from the resource returned by the
                  ``setIamPolicy`` API request.
        )r'   r;   Zto_api_reprset_iam_policyr    r   r   )r   policyr   r*   r   r=   r   r   r   r>     s
    
zSubscription.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.subscriptions/testIamPermissions

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START subscription_check_iam_permissions]
           :end-before: [END subscription_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 subscription's topic.

        :rtype: sequence of string
        :returns: subset of ``permissions`` allowed by current IAM policy.
        )r'   r;   Ztest_iam_permissionsr    list)r   Zpermissionsr   r*   r   r   r   check_iam_permissions  s
    

z"Subscription.check_iam_permissions)NNNN)N)Fr"   N)N)N)N)N)N)Fr"   N)N)N)N)N)N)__name__
__module____qualname____doc__r   r   classmethodr   propertyr   r    r!   r&   r'   r+   r-   r/   r0   r1   r7   r9   r:   r<   r>   rA   r   r   r   r   r      s4     
&









%



r   c                       s2   e Zd ZdZd fdd	Zdd Zd	d
 Z  ZS )r#   a|  Wrapper for :meth:`Subscription.pull` results.

    Mapping, tracks messages still-to-be-acknowledged.

    When used as a context manager, acknowledges all messages still in the
    mapping on `__exit__`.  When processing the pulled messsages, application
    code MUST delete messages from the :class:`AutoAck` mapping which are not
    successfully processed, e.g.:

    .. code-block: python

       with AutoAck(subscription) as ack:  # calls ``subscription.pull``
           for ack_id, message in ack.items():
               try:
                   do_something_with(message):
               except:
                   del ack[ack_id]

    :type subscription: :class:`Subscription`
    :param subscription: subcription to be pulled.

    :type return_immediately: boolean
    :param return_immediately: passed through to :meth:`Subscription.pull`

    :type max_messages: int
    :param max_messages: passed through to :meth:`Subscription.pull`

    :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
    :param client: passed through to :meth:`Subscription.pull` and
                   :meth:`Subscription.acknowledge`.
    Fr"   Nc                    s*   t t|   || _|| _|| _|| _d S N)superr#   r   _subscription_return_immediately_max_messagesr
   )r   Zsubscriptionr$   r%   r   	__class__r   r   r     s
    zAutoAck.__init__c                 C   s$   | j | j| j| j}| | | S rH   )rJ   r7   rK   rL   r
   update)r   itemsr   r   r   	__enter__  s
    
zAutoAck.__enter__c                 C   s   | j t| | j d S rH   )rJ   r9   r@   r
   )r   exc_typeexc_valexc_tbr   r   r   __exit__  s    zAutoAck.__exit__)Fr"   N)rB   rC   rD   rE   r   rQ   rU   __classcell__r   r   rM   r   r#     s
     r#   N)rE   Zgcloud.exceptionsr   Zgcloud.pubsub._helpersr   Zgcloud.pubsub.iamr   Zgcloud.pubsub.messager   objectr   dictr#   r   r   r   r   <module>   s      *