
                             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 Z G d de      Zy)z"Utilities for Pub/Sub Lite topics.    )absolute_import)division)unicode_literals)cloudpubsub)types)message_transforms)topics)	lite_util)
gapic_util)http_encodingc                  R    t        j                  t        j                               S )N)credentials)r   PublisherClientr   GetGapicCredentials     0lib/googlecloudsdk/api_lib/pubsub/lite_topics.pyGetDefaultPublisherClientr      s!    		$	$002
4 4r   c                   :    e Zd ZdZddZd Zd Zd Z	 	 	 	 d	dZy)
r   z,Wrapper client for a Pub/Sub Lite publisher.Nc                 *    |xs
 t               | _        y N)r   _client)selfclients     r   __init__zPublisherClient.__init__'   s    868DLr   c                 :    | j                   j                          | S r   )r   	__enter__)r   s    r   r   zPublisherClient.__enter__*   s    LLKr   c                 >    | j                   j                  |||       y r   )r   __exit__)r   exc_type	exc_value	tracebacks       r   r   zPublisherClient.__exit__.   s    LL(Iy9r   c                     t        j                  t        j                  |j                        t        j
                  |j                        |j                        S )N)projectlocationname)r   	TopicPathr
   ProjectIdToProjectNumber
projectsIdLocationToZoneOrRegionlocationsIdtopicsId)r   resources     r   _TopicResourceToPathz$PublisherClient._TopicResourceToPath1   sD    ??2283F3FG11(2F2FG   r   c                    |s|st        j                  d      | j                  |      }|xs i }|r&t        j                  |      |t        j
                  <   	 t        j                  j                   | j                  j                  |t        j                  |      |fi |j                               S # t        $ r*}t        j                  dj!                  |            d}~ww xY w)ah  Publishes a message to the specified Pub/Sub Lite topic.

    Args:
      topic_resource: The pubsub.lite_topic resource to publish to.
      message: The string message to publish.
      ordering_key: The key for ordering delivery to subscribers.
      attributes: A dict of attributes to attach to the message.
      event_time: A user-specified event timestamp.

    Raises:
      EmptyMessageException: if the message is empty.
      PublishOperationException: if the publish operation is not successful.

    Returns:
      The messageId of the published message, containing the Partition and
      Offset.
    zdYou cannot send an empty message. You must specify either a MESSAGE, one or more ATTRIBUTE, or both.z,Publish operation failed with error: {error})errorN)r	   EmptyMessageExceptionr.   r   encode_attribute_event_timePUBSUB_LITE_EVENT_TIMEr   MessageMetadatadecoder   publishr   Encoderesult	ExceptionPublishOperationExceptionformat)r   topic_resourcemessageordering_key
attributes
event_time
topic_pathes           r   PublishzPublisherClient.Publish7   s    . :((56 6 **>:J!rJ

8
8
D #::;J""))
$,,

z=+?+?+H+;/9;;A68E E  J,,
8
?
?a
?
HJ JJs   AB7 7	C* %C%%C*r   )NNNN)	__name__
__module____qualname____doc__r   r   r   r.   rC   r   r   r   r   r   $   s-    49:  &Jr   r   N)rG   
__future__r   r   r   google.cloud.pubsubliter   r   #google.cloud.pubsublite.cloudpubsubr   googlecloudsdk.api_lib.pubsubr	   !googlecloudsdk.command_lib.pubsubr
   googlecloudsdk.corer   googlecloudsdk.core.utilr   r   objectr   r   r   r   <module>rP      s;     ) &  ' / ) B 0 7 * 24
9Jf 9Jr   