a
    !fP                     @   sj   d Z ddlZddlmZ ddlmZ G dd dejZG dd de	Z
G d	d
 d
e	ZG dd de	ZdS )z1Create / interact with gcloud pubsub connections.    N)
connection)PUBSUB_EMULATORc                       s@   e Zd ZdZdZdZdZdZd fdd	Zd fd	d
	Z	  Z
S )
Connectiona"  A connection to Google Cloud Pubsub via the JSON REST API.

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

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

    :type api_base_url: string
    :param api_base_url: The base of the API call URL. Defaults to the value
                         :attr:`Connection.API_BASE_URL`.
    zhttps://pubsub.googleapis.comZv1z"{api_base_url}/{api_version}{path})z&https://www.googleapis.com/auth/pubsubz.https://www.googleapis.com/auth/cloud-platformNc                    sJ   t t| j||d |d u r@tt}|d u r8| jj}nd| }|| _d S )N)credentialshttpzhttp://)	superr   __init__osgetenvr   	__class__API_BASE_URLapi_base_url)selfr   r   r   Zemulator_hostr    Y/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/pubsub/connection.pyr   3   s    

zConnection.__init__c                    s(   |du r| j }tt| jj||||dS )a  Construct an API url given a few components, some optional.

        Typically, you shouldn't need to use this method.

        :type path: string
        :param path: The path to the resource.

        :type query_params: dict or list
        :param query_params: A dictionary of keys and values (or list of
                             key-value pairs) to insert into the query
                             string of the URL.

        :type api_base_url: string
        :param api_base_url: The base URL for the API endpoint.
                             Typically you won't have to provide this.

        :type api_version: string
        :param api_version: The version of the API to call.
                            Typically you shouldn't provide this and instead
                            use the default for the library.

        :rtype: string
        :returns: The URL assembled from the pieces provided.
        N)query_paramsr   api_version)r   r   r   r   build_api_url)r   pathr   r   r   r   r   r   r   =   s    zConnection.build_api_url)NNN)NNN)__name__
__module____qualname____doc__r   ZAPI_VERSIONZAPI_URL_TEMPLATEZSCOPEr   r   __classcell__r   r   r   r   r      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 connection: :class:`Connection`
    :param connection: the connection used to make API requests.
    c                 C   s
   || _ d S N_connectionr   r   r   r   r   r   e   s    z_PublisherAPI.__init__Nc                 C   sZ   | j }i }|dur||d< |dur*||d< d|f }|jd||d}|dd|d	fS )
ax  API call:  list topics for a given project

        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``).
        NpageSize	pageTokenz/projects/%s/topicsGETmethodr   r   Ztopicsr   nextPageTokenr   api_requestgetr   project	page_size
page_tokenconnparamsr   respr   r   r   list_topicsh   s    
z_PublisherAPI.list_topicsc                 C   s   | j }|jdd|f d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: the fully-qualified path of the new topic, in format
                           ``projects/<PROJECT>/topics/<TOPIC_NAME>``.

        :rtype: dict
        :returns: ``Topic`` resource returned from the API.
        PUT/%sr$   r   r   r'   r   
topic_pathr-   r   r   r   topic_create   s    z_PublisherAPI.topic_createc                 C   s   | j }|jdd|f d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: the fully-qualified path of the topic, in format
                           ``projects/<PROJECT>/topics/<TOPIC_NAME>``.

        :rtype: dict
        :returns: ``Topic`` resource returned from the API.
        r"   r2   r3   r4   r5   r   r   r   	topic_get   s    z_PublisherAPI.topic_getc                 C   s   | j }|jdd|f d dS )a6  API call:  delete a topic

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

        :type topic_path: string
        :param topic_path: the fully-qualified path of the topic, in format
                           ``projects/<PROJECT>/topics/<TOPIC_NAME>``.
        DELETEr2   r3   Nr4   r5   r   r   r   topic_delete   s    
z_PublisherAPI.topic_deletec                 C   s,   | j }d|i}|jdd|f |d}|d S )a  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: the 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.
        messagesPOSTz/%s:publishr$   r   dataZ
messageIdsr4   )r   r6   r;   r-   r>   responser   r   r   topic_publish   s    z_PublisherAPI.topic_publishc                 C   sZ   | j }i }|dur||d< |dur*||d< d|f }|jd||d}|dd|d	fS )
ah  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: the 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 topics. If not
                           passed, the API will return the first page of
                           topics.

        :rtype: list of strings
        :returns: fully-qualified names of subscriptions for the supplied
                  topic.
        Nr    r!   z/%s/subscriptionsr"   r#   subscriptionsr   r%   r&   )r   r6   r+   r,   r-   r.   r   r/   r   r   r   topic_list_subscriptions   s    
z&_PublisherAPI.topic_list_subscriptions)NN)NN)r   r   r   r   r   r0   r7   r8   r:   r@   rB   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 connection: :class:`Connection`
    :param connection: the connection used to make API requests.
    c                 C   s
   || _ d S r   r   r   r   r   r   r      s    z_SubscriberAPI.__init__Nc                 C   sZ   | j }i }|dur||d< |dur*||d< d|f }|jd||d}|dd|d	fS )
a  API call:  list subscriptions for a given project

        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 subscriptions can be retrieved with another call (pass
                  that value as ``page_token``).
        Nr    r!   z/projects/%s/subscriptionsr"   r#   rA   r   r%   r&   r)   r   r   r   list_subscriptions   s    
z!_SubscriberAPI.list_subscriptionsc                 C   sL   | j }d|f }d|i}|dur(||d< |dur<d|i|d< |jd||d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.
        r2   ZtopicNackDeadlineSecondspushEndpoint
pushConfigr1   r=   r4   )r   subscription_pathr6   ack_deadlinepush_endpointr-   r   resourcer   r   r   subscription_create%  s    
z"_SubscriberAPI.subscription_createc                 C   s   | j }d|f }|jd|d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.
        r2   r"   r3   r4   r   rH   r-   r   r   r   r   subscription_getN  s    
z_SubscriberAPI.subscription_getc                 C   s"   | j }d|f }|jd|d 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>``.
        r2   r9   r3   Nr4   rM   r   r   r   subscription_delete`  s    
z"_SubscriberAPI.subscription_deletec                 C   s0   | j }d|f }dd|ii}|jd||d 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.
        z/%s:modifyPushConfigrG   rF   r<   r=   Nr4   )r   rH   rJ   r-   r   rK   r   r   r   subscription_modify_push_configo  s    
z._SubscriberAPI.subscription_modify_push_configF   c                 C   s6   | j }d|f }||d}|jd||d}|d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.
        z/%s:pull)ZreturnImmediatelyZmaxMessagesr<   r=   ZreceivedMessagesr   r&   )r   rH   Zreturn_immediatelyZmax_messagesr-   r   r>   r?   r   r   r   subscription_pull  s    
z _SubscriberAPI.subscription_pullc                 C   s,   | j }d|f }d|i}|jd||d 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
        z/%s:acknowledgeackIdsr<   r=   Nr4   )r   rH   ack_idsr-   r   r>   r   r   r   subscription_acknowledge  s
    
z'_SubscriberAPI.subscription_acknowledgec                 C   s.   | j }d|f }||d}|jd||d 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.
        z/%s:modifyAckDeadline)rS   rE   r<   r=   Nr4   )r   rH   rT   rI   r-   r   r>   r   r   r    subscription_modify_ack_deadline  s    
z/_SubscriberAPI.subscription_modify_ack_deadline)NN)NN)FrQ   )r   r   r   r   r   rD   rL   rN   rO   rP   rR   rU   rV   r   r   r   r   rC      s   
& 
)  
!rC   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_IAMPolicyAPIzHelper mapping IAM policy-related APIs.

    :type connection: :class:`Connection`
    :param connection: the connection used to make API requests.
    c                 C   s
   || _ d S r   r   r   r   r   r   r     s    z_IAMPolicyAPI.__init__c                 C   s   | j }d|f }|jd|dS )a  API call:  fetch the IAM policy for the target

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

        :type target_path: string
        :param target_path: the path of the target object.

        :rtype: dict
        :returns: the resource returned by the ``getIamPolicy`` API request.
        z/%s:getIamPolicyr"   r3   r4   )r   target_pathr-   r   r   r   r   get_iam_policy  s    
z_IAMPolicyAPI.get_iam_policyc                 C   s(   | j }d|i}d|f }|jd||dS )a  API call:  update the IAM policy for the target

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

        :type target_path: string
        :param target_path: the path of the target object.

        :type policy: dict
        :param policy: the new policy resource.

        :rtype: dict
        :returns: the resource returned by the ``setIamPolicy`` API request.
        policyz/%s:setIamPolicyr<   r=   r4   )r   rX   rZ   r-   wrappedr   r   r   r   set_iam_policy  s    
z_IAMPolicyAPI.set_iam_policyc                 C   s4   | j }d|i}d|f }|jd||d}|dg S )a  API call:  test permissions

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

        :type target_path: string
        :param target_path: the path of the target object.

        :type permissions: list of string
        :param permissions: the permissions to check

        :rtype: dict
        :returns: the resource returned by the ``getIamPolicy`` API request.
        permissionsz/%s:testIamPermissionsr<   r=   r&   )r   rX   r]   r-   r[   r   r/   r   r   r   test_iam_permissions  s
    
z"_IAMPolicyAPI.test_iam_permissionsN)r   r   r   r   r   rY   r\   r^   r   r   r   r   rW     s
   rW   )r   r	   Zgcloudr   Zbase_connectionZgcloud.environment_varsr   ZJSONConnectionr   objectr   rC   rW   r   r   r   r   <module>   s   G  b