
    J,                        S SK Jr  S SKrS SKrS SKJrJrJrJrJrJ	r	  S SK
r
S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  \R6                  (       a  S SKJr  S SKJr  \R>                  r " S S\R@                  5      r!g)    )absolute_importN)castAnyCallableOptionalSequenceUnion)AnonymousCredentials)service_account)types)futures)streaming_pull_manager)client)gapic_version)
subscriber)SubscriberGrpcTransportc                   .  ^  \ rS rSrSrS\4U 4S jjr\S\S\SS 4S j5       r	\	r
\S\4S j5       r\S\4S	 j5       r\U 4S
 j5       r    SS\S\S/\4   S\\R&                  \4   S\S   S\S\S\R.                  4S jjrSS jrSS jrS rSrU =r$ )Client(   ad  A subscriber client for Google Cloud Pub/Sub.

This creates an object that is capable of subscribing to messages.
Generally, you can instantiate this client with no arguments, and you
get sensible defaults.

Args:
    kwargs: Any additional arguments provided are sent as keyword
        keyword arguments to the underlying
        :class:`~google.cloud.pubsub_v1.gapic.subscriber_client.SubscriberClient`.
        Generally you should not need to set additional keyword
        arguments. Optionally, regional endpoints can be set via
        ``client_options`` that takes a single key-value pair that
        defines the endpoint.

Example:

.. code-block:: python

    from google.cloud import pubsub_v1

    subscriber_client = pubsub_v1.SubscriberClient(
        # Optional
        client_options = {
            "api_endpoint": REGIONAL_ENDPOINT
        }
    )
kwargsc                   > [         R                  R                  S5      (       a1  S[         R                  R                  S5      0US'   [        5       US'   [        TU ]  " S0 UD6  U R                  R                  U l        SU l	        g )NPUBSUB_EMULATOR_HOSTapi_endpointclient_optionscredentialsF )
osenvirongetr
   super__init__
_transport_host_target_closed)selfr   	__class__s     ;lib/third_party/google/cloud/pubsub_v1/subscriber/client.pyr!   Client.__init__F   sr     ::>>011

/E F(F#$ %9$:F=! 	"6",,    filenamereturnc                 X    [         R                  R                  U5      nX2S'   U " S0 UD6$ )aS  Creates an instance of this client using the provided credentials
file.

Args:
    filename: The path to the service account private key json file.
    kwargs: Additional arguments to pass to the constructor.

Returns:
    A Subscriber :class:`~google.cloud.pubsub_v1.subscriber.client.Client`
    instance that is the constructed client.
r   r   )r   Credentialsfrom_service_account_file)clsr+   r   r   s       r(   r/    Client.from_service_account_fileU   s.     &11KKHU +}}V}r*   c                     U R                   $ )zMReturn the target (where the API is).

Returns:
    The location of the API.
)r$   r&   s    r(   targetClient.targetj   s     ||r*   c                     U R                   $ )z_Return whether the client has been closed and cannot be used anymore.

.. versionadded:: 2.8.0
)r%   r3   s    r(   closedClient.closeds   s     ||r*   c                 N   > Sn[         R                  " U[        S9  [        5       $ )aP  The underlying gapic API client.

.. versionchanged:: 2.10.0
    Instead of a GAPIC ``SubscriberClient`` client instance, this property is a
    proxy object to it with the same interface.

.. deprecated:: 2.10.0
    Use the GAPIC methods and properties on the client instance directly
    instead of through the :attr:`api` attribute.
zThe "api" property only exists for backward compatibility, access its attributes directly thorugh the client instance (e.g. "client.foo" instead of "client.api.foo").)category)warningswarnDeprecationWarningr    )r&   msgr'   s     r(   api
Client.api{   s%    , 	
 	c$67wr*   subscriptioncallbackzsubscriber.message.Messageflow_control	schedulerz$subscriber.scheduler.ThreadScheduleruse_legacy_flow_controlawait_callbacks_on_shutdownc           	          [         R                  " U6 n[        R                  " U UUUUUS9n[        R
                  " U5      nUR                  X(R                  S9  U$ )a8  Asynchronously start receiving messages on a given subscription.

This method starts a background thread to begin pulling messages from
a Pub/Sub subscription and scheduling them to be processed using the
provided ``callback``.

The ``callback`` will be called with an individual
:class:`google.cloud.pubsub_v1.subscriber.message.Message`. It is the
responsibility of the callback to either call ``ack()`` or ``nack()``
on the message when it finished processing. If an exception occurs in
the callback during processing, the exception is logged and the message
is ``nack()`` ed.

The ``flow_control`` argument can be used to control the rate of at
which messages are pulled. The settings are relatively conservative by
default to prevent "message hoarding" - a situation where the client
pulls a large number of messages but can not process them fast enough
leading it to "starve" other clients of messages. Increasing these
settings may lead to faster throughput for messages that do not take
a long time to process.

The ``use_legacy_flow_control`` argument disables enforcing flow control
settings at the Cloud Pub/Sub server, and only the client side flow control
will be enforced.

This method starts the receiver in the background and returns a
*Future* representing its execution. Waiting on the future (calling
``result()``) will block forever or until a non-recoverable error
is encountered (such as loss of network connectivity). Cancelling the
future will signal the process to shutdown gracefully and exit.

.. note:: This uses Pub/Sub's *streaming pull* feature. This feature
    properties that may be surprising. Please take a look at
    https://cloud.google.com/pubsub/docs/pull#streamingpull for
    more details on how streaming pull behaves compared to the
    synchronous pull method.

Example:

.. code-block:: python

    from google.cloud import pubsub_v1

    subscriber_client = pubsub_v1.SubscriberClient()

    # existing subscription
    subscription = subscriber_client.subscription_path(
        'my-project-id', 'my-subscription')

    def callback(message):
        print(message)
        message.ack()

    future = subscriber_client.subscribe(
        subscription, callback)

    try:
        future.result()
    except KeyboardInterrupt:
        future.cancel()  # Trigger the shutdown.
        future.result()  # Block until the shutdown is complete.

Args:
    subscription:
        The name of the subscription. The subscription should have already been
        created (for example, by using :meth:`create_subscription`).
    callback:
        The callback function. This function receives the message as
        its only argument and will be called from a different thread/
        process depending on the scheduling strategy.
    flow_control:
        The flow control settings. Use this to prevent situations where you are
        inundated with too many messages at once.
    scheduler:
        An optional *scheduler* to use when executing the callback. This
        controls how callbacks are executed concurrently. This object must not
        be shared across multiple ``SubscriberClient`` instances.
    use_legacy_flow_control (bool):
        If set to ``True``, flow control at the Cloud Pub/Sub server is disabled,
        though client-side flow control is still enabled. If set to ``False``
        (default), both server-side and client-side flow control are enabled.
    await_callbacks_on_shutdown:
        If ``True``, after canceling the returned future, the latter's
        ``result()`` method will block until the background stream and its
        helper threads have been terminated, and all currently executing message
        callbacks are done processing.

        If ``False`` (default), the returned future's ``result()`` method will
        not block after canceling the future. The method will instead return
        immediately after the background stream and its helper threads have been
        terminated, but some of the message callback threads might still be
        running at that point.

Returns:
    A future instance that can be used to manage the background stream.
)rC   rD   rE   rF   )rB   on_callback_error)r   FlowControlr   StreamingPullManagerr   StreamingPullFutureopenset_exception)	r&   rA   rB   rC   rD   rE   rF   managerfutures	            r(   	subscribeClient.subscribe   sb    R ((,7(==%$;(C
 ,,W5h:N:NOr*   c                 r    [        SU R                  5      nUR                  R                  5         SU l        g)zClose the underlying channel to release socket resources.

After a channel has been closed, the client instance cannot be used
anymore.

This method is idempotent.
r   TN)r   r"   grpc_channelcloser%   )r&   	transports     r(   rT   Client.close	  s.     2DOOD	$$&r*   c                 >    U R                   (       a  [        S5      eU $ )Nz4Closed subscriber cannot be used as context manager.)r%   RuntimeErrorr3   s    r(   	__enter__Client.__enter__  s    <<UVVr*   c                 $    U R                  5         g )N)rT   )r&   exc_typeexc_valexc_tbs       r(   __exit__Client.__exit__  s    

r*   )r%   r$   )r   NFF)r,   N)r,   r   )__name__
__module____qualname____firstlineno____doc__r   r!   classmethodstrr/   from_service_account_jsonpropertyr4   boolr7   r?   r   r	   r   rI   r   r   r   rK   rP   rT   rY   r_   __static_attributes____classcell__)r'   s   @r(   r   r   (   s.   :  &)	 $ !:       . <>FJ(-,1xx 893>?x E--x78	x
 BCx "&x &*x 
	$	$xt

 r*   r   )"
__future__r   r   typingr   r   r   r   r   r	   r;   google.auth.credentialsr
   google.oauth2r   google.cloud.pubsub_v1r   !google.cloud.pubsub_v1.subscriberr   +google.cloud.pubsub_v1.subscriber._protocolr   $google.pubsub_v1.services.subscriberr   subscriber_clientgoogle.pubsub_v1r   package_versionTYPE_CHECKINGr   4google.pubsub_v1.services.subscriber.transports.grpcr   __version__SubscriberClientr   r   r*   r(   <module>r|      sa    ' 	  A A  8 ) ( 5 N L =	1 ))s// sr*   