a
    !f4K                     @   s   d 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 dd	lmZ dd
lmZ ddlmZ dddZG dd deZG dd deZdd Zdd Zdd Zdd ZdS )z$GAX wrapper for Pubsub API requests.    )CallOptions)INITIAL_PAGE)GaxError)exc_to_code)PubsubMessage)
PushConfig)
StatusCode)Conflict)NotFound)	_to_bytesNc                 C   s"   | du rt } d| i}tf i |S )z6Helper for :meth:'_PublisherAPI.list_topics' et aliae.N
page_token)r   r   )r   options r   S/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/pubsub/_gax.py_build_paging_options    s    r   c                   @   sL   e Zd ZdZdd ZdddZdd	 Zd
d Zdd Zdd Z	dddZ
dS )_PublisherAPIzHelper mapping publisher-related APIs.

    :type gax_api: :class:`google.pubsub.v1.publisher_api.PublisherApi`
    :param gax_api: API object used to make GAX requests.
    c                 C   s
   || _ d S N_gax_apiselfZgax_apir   r   r   __init__.   s    z_PublisherAPI.__init__r   Nc           	      C   sH   t |}d|f }| jj|||d}dd | D }|jp>d}||fS )a  List topics for the project associated with this API.

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

        :type project: string
        :param project: project ID

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

        :rtype: tuple, (list, str)
        :returns: list of ``Topic`` resource dicts, 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``).
        projects/%s	page_sizer   c                 S   s   g | ]}d |j iqS namer   ).0topic_pbr   r   r   
<listcomp>M       z-_PublisherAPI.list_topics.<locals>.<listcomp>N)r   r   list_topicsnextr   )	r   projectr   r   r   path	page_iterZtopicstokenr   r   r   r!   1   s    

z_PublisherAPI.list_topicsc              
   C   sZ   z| j |}W n> tyN } z&t|jtjkr8t| W Y d}~n
d}~0 0 d|jiS )a  API call:  create a topic

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

        :type topic_path: string
        :param topic_path: fully-qualified path of the new topic, in format
                            ``projects/<PROJECT>/topics/<TOPIC_NAME>``.

        :rtype: dict
        :returns: ``Topic`` resource returned from the API.
        :raises: :exc:`gcloud.exceptions.Conflict` if the topic already
                    exists
        Nr   )	r   Zcreate_topicr   r   causer   FAILED_PRECONDITIONr	   r   r   
topic_pathr   excr   r   r   topic_createQ   s    z_PublisherAPI.topic_createc              
   C   sZ   z| j |}W n> tyN } z&t|jtjkr8t| W Y d}~n
d}~0 0 d|jiS )a  API call:  retrieve a topic

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

        :type topic_path: string
        :param topic_path: fully-qualified path of the topic, in format
                        ``projects/<PROJECT>/topics/<TOPIC_NAME>``.

        :rtype: dict
        :returns: ``Topic`` resource returned from the API.
        :raises: :exc:`gcloud.exceptions.NotFound` if the topic does not
                    exist
        Nr   )	r   Z	get_topicr   r   r'   r   	NOT_FOUNDr
   r   r)   r   r   r   	topic_geth   s    z_PublisherAPI.topic_getc              
   C   sT   z| j | W n> tyN } z&t|jtjkr8t| W Y d}~n
d}~0 0 dS )a7  API call:  delete a topic

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

        :type topic_path: string
        :param topic_path: fully-qualified path of the new topic, in format
                            ``projects/<PROJECT>/topics/<TOPIC_NAME>``.
        N)r   Zdelete_topicr   r   r'   r   r-   r
   )r   r*   r+   r   r   r   topic_delete   s    
z_PublisherAPI.topic_deletec              
   C   st   t dd}dd |D }z| jj|||d}W n> tyl } z&t|jtjkrVt| W Y d}~n
d}~0 0 |j	S )af  API call:  publish one or more messages to a topic

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

        :type topic_path: string
        :param topic_path: fully-qualified path of the topic, in format
                            ``projects/<PROJECT>/topics/<TOPIC_NAME>``.

        :type messages: list of dict
        :param messages: messages to be published.

        :rtype: list of string
        :returns: list of opaque IDs for published messages.
        :raises: :exc:`gcloud.exceptions.NotFound` if the topic does not
                    exist
        F)Zis_bundlingc                 S   s   g | ]}t |qS r   )_message_pb_from_dict)r   messager   r   r   r      s   z/_PublisherAPI.topic_publish.<locals>.<listcomp>)r   N)
r   r   publishr   r   r'   r   r-   r
   Zmessage_ids)r   r*   messagesr   Zmessage_pbsresultr+   r   r   r   topic_publish   s    


z_PublisherAPI.topic_publishc           	   
   C   sx   t |}z| jj|||d}W n> ty\ } z&t|jtjkrFt| W Y d}~n
d}~0 0 |	 }|j
pnd}||fS )a  API call:  list subscriptions bound to a topic

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

        :type topic_path: string
        :param topic_path: fully-qualified path of the topic, in format
                            ``projects/<PROJECT>/topics/<TOPIC_NAME>``.

        :type page_size: int
        :param page_size: maximum number of subscriptions 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 subscriptions.
                           If not passed, the API will return the first page
                           of subscriptions.

        :rtype: list of strings
        :returns: fully-qualified names of subscriptions for the supplied
                topic.
        :raises: :exc:`gcloud.exceptions.NotFound` if the topic does not
                    exist
        r   N)r   r   Zlist_topic_subscriptionsr   r   r'   r   r-   r
   r"   r   )	r   r*   r   r   r   r%   r+   subsr&   r   r   r   topic_list_subscriptions   s    

z&_PublisherAPI.topic_list_subscriptions)r   N)r   N)__name__
__module____qualname____doc__r   r!   r,   r.   r/   r5   r7   r   r   r   r   r   (   s   
   r   c                   @   s^   e Zd ZdZdd ZdddZddd	Zd
d Zdd Zdd Z	dddZ
dd Zdd ZdS )_SubscriberAPIzHelper mapping subscriber-related APIs.

    :type gax_api: :class:`google.pubsub.v1.publisher_api.SubscriberApi`
    :param gax_api: API object used to make GAX requests.
    c                 C   s
   || _ d S r   r   r   r   r   r   r      s    z_SubscriberAPI.__init__r   Nc           	      C   sH   t |}d|f }| jj|||d}dd | D }|jp>d}||fS )a  List subscriptions for the project associated with this API.

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

        :type project: string
        :param project: project ID

        :type page_size: int
        :param page_size: maximum number of subscriptions 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 subscriptions.
                           If not passed, the API will return the first page
                           of subscriptions.

        :rtype: tuple, (list, str)
        :returns: list of ``Subscription`` resource dicts, 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   c                 S   s   g | ]}t |qS r   )_subscription_pb_to_mapping)r   sub_pbr   r   r   r      s   z5_SubscriberAPI.list_subscriptions.<locals>.<listcomp>N)r   r   list_subscriptionsr"   r   )	r   r#   r   r   r   r$   r%   Zsubscriptionsr&   r   r   r   r?      s    

z!_SubscriberAPI.list_subscriptionsc              
   C   s   |durt |d}nd}|du r$d}z| j||||}W n> tyx } z&t|jtjkrbt| W Y d}~n
d}~0 0 t	|S )a  API call:  create a subscription

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

        :type subscription_path: string
        :param subscription_path: the fully-qualified path of the new
                                  subscription, in format
                                  ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.

        :type topic_path: string
        :param topic_path: the fully-qualified path of the topic being
                           subscribed, in format
                           ``projects/<PROJECT>/topics/<TOPIC_NAME>``.

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

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

        :rtype: dict
        :returns: ``Subscription`` resource returned from the API.
        Npush_endpointr   )
r   r   Zcreate_subscriptionr   r   r'   r   r(   r	   r=   )r   subscription_pathr*   ack_deadlinerA   push_configr>   r+   r   r   r   subscription_create   s    z"_SubscriberAPI.subscription_createc              
   C   sX   z| j |}W n> tyN } z&t|jtjkr8t| W Y d}~n
d}~0 0 t|S )a  API call:  retrieve a subscription

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

        :type subscription_path: string
        :param subscription_path: the fully-qualified path of the subscription,
                                  in format
                                  ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.

        :rtype: dict
        :returns: ``Subscription`` resource returned from the API.
        N)	r   Zget_subscriptionr   r   r'   r   r-   r
   r=   )r   rB   r>   r+   r   r   r   subscription_get-  s    z_SubscriberAPI.subscription_getc              
   C   sT   z| j | W n> tyN } z&t|jtjkr8t| W Y d}~n
d}~0 0 dS )a  API call:  delete a subscription

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

        :type subscription_path: string
        :param subscription_path: the fully-qualified path of the subscription,
                                  in format
                                  ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.
        N)r   Zdelete_subscriptionr   r   r'   r   r-   r
   )r   rB   r+   r   r   r   subscription_deleteC  s    z"_SubscriberAPI.subscription_deletec              
   C   s`   t |d}z| j|| W n> tyZ } z&t|jtjkrDt| W Y d}~n
d}~0 0 dS )a  API call:  update push config of a subscription

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

        :type subscription_path: string
        :param subscription_path: the fully-qualified path of the new
                                  subscription, in format
                                  ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.

        :type push_endpoint: string, or ``NoneType``
        :param push_endpoint: URL to which messages will be pushed by the
                              back-end.  If not set, the application must pull
                              messages.
        r@   N)	r   r   Zmodify_push_configr   r   r'   r   r-   r
   )r   rB   rA   rD   r+   r   r   r   subscription_modify_push_configU  s    
z._SubscriberAPI.subscription_modify_push_configF   c              
   C   sd   z| j |||}W n> tyR } z&t|jtjkr<t| W Y d}~n
d}~0 0 dd |jD S )a  API call:  retrieve messages for a subscription

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

        :type subscription_path: string
        :param subscription_path: the fully-qualified path of the new
                                  subscription, in format
                                  ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.

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

        :rtype: list of dict
        :returns:  the ``receivedMessages`` element of the response.
        Nc                 S   s   g | ]}t |qS r   )_received_message_pb_to_mapping)r   Zrmpbr   r   r   r     s   z4_SubscriberAPI.subscription_pull.<locals>.<listcomp>)	r   pullr   r   r'   r   r-   r
   Zreceived_messages)r   rB   Zreturn_immediatelyZmax_messagesZresponse_pbr+   r   r   r   subscription_pulln  s    z _SubscriberAPI.subscription_pullc              
   C   sV   z| j || W n> tyP } z&t|jtjkr:t| W Y d}~n
d}~0 0 dS )a  API call:  acknowledge retrieved messages

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

        :type subscription_path: string
        :param subscription_path: the fully-qualified path of the new
                                  subscription, in format
                                  ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.

        :type ack_ids: list of string
        :param ack_ids: ack IDs of messages being acknowledged
        N)r   Zacknowledger   r   r'   r   r-   r
   )r   rB   ack_idsr+   r   r   r   subscription_acknowledge  s    z'_SubscriberAPI.subscription_acknowledgec              
   C   sX   z| j ||| W n> tyR } z&t|jtjkr<t| W Y d}~n
d}~0 0 dS )a  API call:  update ack deadline for retrieved messages

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

        :type subscription_path: string
        :param subscription_path: the fully-qualified path of the new
                                  subscription, in format
                                  ``projects/<PROJECT>/subscriptions/<SUB_NAME>``.

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

        :type ack_deadline: int
        :param ack_deadline: the deadline (in seconds) by which messages pulled
                            from the back-end must be acknowledged.
        N)r   Zmodify_ack_deadliner   r   r'   r   r-   r
   )r   rB   rM   rC   r+   r   r   r    subscription_modify_ack_deadline  s    z/_SubscriberAPI.subscription_modify_ack_deadline)r   N)NN)FrI   )r8   r9   r:   r;   r   r?   rE   rF   rG   rH   rL   rN   rO   r   r   r   r   r<      s   
" 
.  
"r<   c                 C   s   t t| d | d dS )z/Helper for :meth:`_PublisherAPI.topic_publish`.data
attributes)rP   rQ   )r   r   )r1   r   r   r   r0     s    r0   c                 C   s2   | j | j| jd}| jjdkr.d| jji|d< |S )zHelper for :meth:`list_subscriptions`, et aliae

    Ideally, would use a function from :mod:`protobuf.json_format`, but
    the right one isn't public.  See:
    https://github.com/google/protobuf/issues/1351
    )r   topicZackDeadlineSeconds ZpushEndpointZ
pushConfig)r   rR   Zack_deadline_secondsrD   rA   )r>   mappingr   r   r   r=     s    r=   c                 C   s   | j | j| jdS )Helper for :meth:`pull`, et aliae

    Ideally, would use a function from :mod:`protobuf.json_format`, but
    the right one isn't public.  See:
    https://github.com/google/protobuf/issues/1351
    )Z	messageIdrP   rQ   )Z
message_idrP   rQ   )Z
message_pbr   r   r   _message_pb_to_mapping  s    rV   c                 C   s   | j t| jdS )rU   )ZackIdr1   )Zack_idrV   r1   )Zreceived_message_pbr   r   r   rJ     s
    rJ   )N)r;   Z
google.gaxr   r   Zgoogle.gax.errorsr   Zgoogle.gax.grpcr   Zgoogle.pubsub.v1.pubsub_pb2r   r   Zgrpc.beta.interfacesr   Zgcloud.exceptionsr	   r
   Zgcloud._helpersr   r   objectr   r<   r0   r=   rV   rJ   r   r   r   r   <module>   s&   
 . m