def __init__( self, region: CloudRegion, credentials: Optional[Credentials] = None, transport: Optional[str] = None, client_options: Optional[ClientOptions] = None, ): """ Create a new AdminClient. Args: region: The cloud region to connect to. credentials: The credentials to use when connecting. transport: The transport to use. client_options: The client options to use when connecting. If used, must explicitly set `api_endpoint`. """ if client_options is None: client_options = ClientOptions(api_endpoint=regional_endpoint(region)) self._impl = AdminClientImpl( AdminServiceClient( client_options=client_options, transport=transport, credentials=credentials, ), region, )
def make_async_subscriber( subscription: SubscriptionPath, transport: str, per_partition_flow_control_settings: FlowControlSettings, nack_handler: Optional[NackHandler] = None, message_transformer: Optional[MessageTransformer] = None, fixed_partitions: Optional[Set[Partition]] = None, credentials: Optional[Credentials] = None, client_options: Optional[ClientOptions] = None, metadata: Optional[Mapping[str, str]] = None, ) -> AsyncSingleSubscriber: """ Make a Pub/Sub Lite AsyncSubscriber. Args: subscription: The subscription to subscribe to. transport: The transport type to use. per_partition_flow_control_settings: The flow control settings for each partition subscribed to. Note that these settings apply to each partition individually, not in aggregate. nack_handler: An optional handler for when nack() is called on a Message. The default will fail the client. message_transformer: An optional transformer from Pub/Sub Lite messages to Cloud Pub/Sub messages. fixed_partitions: A fixed set of partitions to subscribe to. If not present, will instead use auto-assignment. credentials: The credentials to use to connect. GOOGLE_DEFAULT_CREDENTIALS is used if None. client_options: Other options to pass to the client. Note that if you pass any you must set api_endpoint. metadata: Additional metadata to send with the RPC. Returns: A new AsyncSubscriber. """ metadata = merge_metadata(pubsub_context(framework="CLOUD_PUBSUB_SHIM"), metadata) if client_options is None: client_options = ClientOptions( api_endpoint=regional_endpoint(subscription.location.region) ) assigner_factory: Callable[[], Assigner] if fixed_partitions: assigner_factory = lambda: FixedSetAssigner(fixed_partitions) # noqa: E731 else: assigner_factory = lambda: _make_dynamic_assigner( # noqa: E731 subscription, transport, client_options, credentials, metadata, ) if nack_handler is None: nack_handler = DefaultNackHandler() if message_transformer is None: message_transformer = MessageTransformer.of_callable(to_cps_subscribe_message) partition_subscriber_factory = _make_partition_subscriber_factory( subscription, transport, client_options, credentials, metadata, per_partition_flow_control_settings, nack_handler, message_transformer, ) return AssigningSingleSubscriber(assigner_factory, partition_subscriber_factory)
def make_publisher( topic: TopicPath, transport: str, per_partition_batching_settings: Optional[BatchSettings] = None, credentials: Optional[Credentials] = None, client_options: Optional[ClientOptions] = None, metadata: Optional[Mapping[str, str]] = None, ) -> Publisher: """ Make a new publisher for the given topic. Args: topic: The topic to publish to. transport: The transport type to use. per_partition_batching_settings: Settings for batching messages on each partition. The default is reasonable for most cases. credentials: The credentials to use to connect. GOOGLE_DEFAULT_CREDENTIALS is used if None. client_options: Other options to pass to the client. Note that if you pass any you must set api_endpoint. metadata: Additional metadata to send with the RPC. Returns: A new Publisher. Throws: GoogleApiCallException on any error determining topic structure. """ if per_partition_batching_settings is None: per_partition_batching_settings = DEFAULT_BATCHING_SETTINGS admin_client = AdminClient( region=topic.location.region, credentials=credentials, client_options=client_options, ) if client_options is None: client_options = ClientOptions( api_endpoint=regional_endpoint(topic.location.region) ) client_cache = ClientCache( lambda: async_client.PublisherServiceAsyncClient( credentials=credentials, transport=transport, client_options=client_options ) ) def publisher_factory(partition: Partition): def connection_factory(requests: AsyncIterator[PublishRequest]): final_metadata = merge_metadata( metadata, topic_routing_metadata(topic, partition) ) return client_cache.get().publish( requests, metadata=list(final_metadata.items()) ) return SinglePartitionPublisher( InitialPublishRequest(topic=str(topic), partition=partition.value), per_partition_batching_settings, GapicConnectionFactory(connection_factory), ) def policy_factory(partition_count: int): return DefaultRoutingPolicy(partition_count) return PartitionCountWatchingPublisher( PartitionCountWatcherImpl(admin_client, topic, DEFAULT_PARTITION_POLL_PERIOD), publisher_factory, policy_factory, )