1919import re
2020from typing import (
2121 Dict ,
22+ Callable ,
2223 Mapping ,
2324 MutableMapping ,
2425 MutableSequence ,
@@ -579,7 +580,9 @@ def __init__(
579580 self ,
580581 * ,
581582 credentials : Optional [ga_credentials .Credentials ] = None ,
582- transport : Optional [Union [str , PublisherTransport ]] = None ,
583+ transport : Optional [
584+ Union [str , PublisherTransport , Callable [..., PublisherTransport ]]
585+ ] = None ,
583586 client_options : Optional [Union [client_options_lib .ClientOptions , dict ]] = None ,
584587 client_info : gapic_v1 .client_info .ClientInfo = DEFAULT_CLIENT_INFO ,
585588 ) -> None :
@@ -591,9 +594,11 @@ def __init__(
591594 credentials identify the application to the service; if none
592595 are specified, the client will attempt to ascertain the
593596 credentials from the environment.
594- transport (Union[str, PublisherTransport]): The
595- transport to use. If set to None, a transport is chosen
596- automatically.
597+ transport (Optional[Union[str,PublisherTransport,Callable[..., PublisherTransport]]]):
598+ The transport to use, or a Callable that constructs and returns a new transport.
599+ If a Callable is given, it will be called with the same set of initialization
600+ arguments as used in the PublisherTransport constructor.
601+ If set to None, a transport is chosen automatically.
597602 client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]):
598603 Custom options for the client.
599604
@@ -699,17 +704,24 @@ def __init__(
699704 api_key_value
700705 )
701706
702- Transport = type (self ).get_transport_class (cast (str , transport ))
707+ transport_init : Union [
708+ Type [PublisherTransport ], Callable [..., PublisherTransport ]
709+ ] = (
710+ type (self ).get_transport_class (transport )
711+ if isinstance (transport , str ) or transport is None
712+ else cast (Callable [..., PublisherTransport ], transport )
713+ )
714+ # initialize with the provided callable or the passed in class
703715
704716 emulator_host = os .environ .get ("PUBSUB_EMULATOR_HOST" )
705717 if emulator_host :
706- if issubclass (Transport , type (self )._transport_registry ["grpc" ]):
718+ if issubclass (transport_init , type (self )._transport_registry ["grpc" ]):
707719 channel = grpc .insecure_channel (target = emulator_host )
708720 else :
709721 channel = grpc .aio .insecure_channel (target = emulator_host )
710- Transport = functools .partial (Transport , channel = channel )
722+ transport_init = functools .partial (transport_init , channel = channel )
711723
712- self ._transport = Transport (
724+ self ._transport = transport_init (
713725 credentials = credentials ,
714726 credentials_file = self ._client_options .credentials_file ,
715727 host = self ._api_endpoint ,
@@ -788,19 +800,17 @@ def sample_create_topic():
788800 A topic resource.
789801 """
790802 # Create or coerce a protobuf request object.
791- # Quick check: If we got a request object, we should *not* have
792- # gotten any keyword arguments that map to the request.
803+ # - Quick check: If we got a request object, we should *not* have
804+ # gotten any keyword arguments that map to the request.
793805 has_flattened_params = any ([name ])
794806 if request is not None and has_flattened_params :
795807 raise ValueError (
796808 "If the `request` argument is set, then none of "
797809 "the individual field arguments should be set."
798810 )
799811
800- # Minor optimization to avoid making a copy if the user passes
801- # in a pubsub.Topic.
802- # There's no risk of modifying the input as we've already verified
803- # there are no flattened fields.
812+ # - Use the request object if provided (there's no risk of modifying the input as
813+ # there are no flattened fields), or create one.
804814 if not isinstance (request , pubsub .Topic ):
805815 request = pubsub .Topic (request )
806816 # If we have keyword arguments corresponding to fields on the
@@ -907,19 +917,17 @@ def sample_update_topic():
907917 A topic resource.
908918 """
909919 # Create or coerce a protobuf request object.
910- # Quick check: If we got a request object, we should *not* have
911- # gotten any keyword arguments that map to the request.
920+ # - Quick check: If we got a request object, we should *not* have
921+ # gotten any keyword arguments that map to the request.
912922 has_flattened_params = any ([topic , update_mask ])
913923 if request is not None and has_flattened_params :
914924 raise ValueError (
915925 "If the `request` argument is set, then none of "
916926 "the individual field arguments should be set."
917927 )
918928
919- # Minor optimization to avoid making a copy if the user passes
920- # in a pubsub.UpdateTopicRequest.
921- # There's no risk of modifying the input as we've already verified
922- # there are no flattened fields.
929+ # - Use the request object if provided (there's no risk of modifying the input as
930+ # there are no flattened fields), or create one.
923931 if not isinstance (request , pubsub .UpdateTopicRequest ):
924932 request = pubsub .UpdateTopicRequest (request )
925933 # If we have keyword arguments corresponding to fields on the
@@ -1022,19 +1030,17 @@ def sample_publish():
10221030 Response for the Publish method.
10231031 """
10241032 # Create or coerce a protobuf request object.
1025- # Quick check: If we got a request object, we should *not* have
1026- # gotten any keyword arguments that map to the request.
1033+ # - Quick check: If we got a request object, we should *not* have
1034+ # gotten any keyword arguments that map to the request.
10271035 has_flattened_params = any ([topic , messages ])
10281036 if request is not None and has_flattened_params :
10291037 raise ValueError (
10301038 "If the `request` argument is set, then none of "
10311039 "the individual field arguments should be set."
10321040 )
10331041
1034- # Minor optimization to avoid making a copy if the user passes
1035- # in a pubsub.PublishRequest.
1036- # There's no risk of modifying the input as we've already verified
1037- # there are no flattened fields.
1042+ # - Use the request object if provided (there's no risk of modifying the input as
1043+ # there are no flattened fields), or create one.
10381044 if not isinstance (request , pubsub .PublishRequest ):
10391045 request = pubsub .PublishRequest (request )
10401046 # If we have keyword arguments corresponding to fields on the
@@ -1127,19 +1133,17 @@ def sample_get_topic():
11271133 A topic resource.
11281134 """
11291135 # Create or coerce a protobuf request object.
1130- # Quick check: If we got a request object, we should *not* have
1131- # gotten any keyword arguments that map to the request.
1136+ # - Quick check: If we got a request object, we should *not* have
1137+ # gotten any keyword arguments that map to the request.
11321138 has_flattened_params = any ([topic ])
11331139 if request is not None and has_flattened_params :
11341140 raise ValueError (
11351141 "If the `request` argument is set, then none of "
11361142 "the individual field arguments should be set."
11371143 )
11381144
1139- # Minor optimization to avoid making a copy if the user passes
1140- # in a pubsub.GetTopicRequest.
1141- # There's no risk of modifying the input as we've already verified
1142- # there are no flattened fields.
1145+ # - Use the request object if provided (there's no risk of modifying the input as
1146+ # there are no flattened fields), or create one.
11431147 if not isinstance (request , pubsub .GetTopicRequest ):
11441148 request = pubsub .GetTopicRequest (request )
11451149 # If we have keyword arguments corresponding to fields on the
@@ -1235,19 +1239,17 @@ def sample_list_topics():
12351239
12361240 """
12371241 # Create or coerce a protobuf request object.
1238- # Quick check: If we got a request object, we should *not* have
1239- # gotten any keyword arguments that map to the request.
1242+ # - Quick check: If we got a request object, we should *not* have
1243+ # gotten any keyword arguments that map to the request.
12401244 has_flattened_params = any ([project ])
12411245 if request is not None and has_flattened_params :
12421246 raise ValueError (
12431247 "If the `request` argument is set, then none of "
12441248 "the individual field arguments should be set."
12451249 )
12461250
1247- # Minor optimization to avoid making a copy if the user passes
1248- # in a pubsub.ListTopicsRequest.
1249- # There's no risk of modifying the input as we've already verified
1250- # there are no flattened fields.
1251+ # - Use the request object if provided (there's no risk of modifying the input as
1252+ # there are no flattened fields), or create one.
12511253 if not isinstance (request , pubsub .ListTopicsRequest ):
12521254 request = pubsub .ListTopicsRequest (request )
12531255 # If we have keyword arguments corresponding to fields on the
@@ -1354,19 +1356,17 @@ def sample_list_topic_subscriptions():
13541356
13551357 """
13561358 # Create or coerce a protobuf request object.
1357- # Quick check: If we got a request object, we should *not* have
1358- # gotten any keyword arguments that map to the request.
1359+ # - Quick check: If we got a request object, we should *not* have
1360+ # gotten any keyword arguments that map to the request.
13591361 has_flattened_params = any ([topic ])
13601362 if request is not None and has_flattened_params :
13611363 raise ValueError (
13621364 "If the `request` argument is set, then none of "
13631365 "the individual field arguments should be set."
13641366 )
13651367
1366- # Minor optimization to avoid making a copy if the user passes
1367- # in a pubsub.ListTopicSubscriptionsRequest.
1368- # There's no risk of modifying the input as we've already verified
1369- # there are no flattened fields.
1368+ # - Use the request object if provided (there's no risk of modifying the input as
1369+ # there are no flattened fields), or create one.
13701370 if not isinstance (request , pubsub .ListTopicSubscriptionsRequest ):
13711371 request = pubsub .ListTopicSubscriptionsRequest (request )
13721372 # If we have keyword arguments corresponding to fields on the
@@ -1477,19 +1477,17 @@ def sample_list_topic_snapshots():
14771477
14781478 """
14791479 # Create or coerce a protobuf request object.
1480- # Quick check: If we got a request object, we should *not* have
1481- # gotten any keyword arguments that map to the request.
1480+ # - Quick check: If we got a request object, we should *not* have
1481+ # gotten any keyword arguments that map to the request.
14821482 has_flattened_params = any ([topic ])
14831483 if request is not None and has_flattened_params :
14841484 raise ValueError (
14851485 "If the `request` argument is set, then none of "
14861486 "the individual field arguments should be set."
14871487 )
14881488
1489- # Minor optimization to avoid making a copy if the user passes
1490- # in a pubsub.ListTopicSnapshotsRequest.
1491- # There's no risk of modifying the input as we've already verified
1492- # there are no flattened fields.
1489+ # - Use the request object if provided (there's no risk of modifying the input as
1490+ # there are no flattened fields), or create one.
14931491 if not isinstance (request , pubsub .ListTopicSnapshotsRequest ):
14941492 request = pubsub .ListTopicSnapshotsRequest (request )
14951493 # If we have keyword arguments corresponding to fields on the
@@ -1587,19 +1585,17 @@ def sample_delete_topic():
15871585 sent along with the request as metadata.
15881586 """
15891587 # Create or coerce a protobuf request object.
1590- # Quick check: If we got a request object, we should *not* have
1591- # gotten any keyword arguments that map to the request.
1588+ # - Quick check: If we got a request object, we should *not* have
1589+ # gotten any keyword arguments that map to the request.
15921590 has_flattened_params = any ([topic ])
15931591 if request is not None and has_flattened_params :
15941592 raise ValueError (
15951593 "If the `request` argument is set, then none of "
15961594 "the individual field arguments should be set."
15971595 )
15981596
1599- # Minor optimization to avoid making a copy if the user passes
1600- # in a pubsub.DeleteTopicRequest.
1601- # There's no risk of modifying the input as we've already verified
1602- # there are no flattened fields.
1597+ # - Use the request object if provided (there's no risk of modifying the input as
1598+ # there are no flattened fields), or create one.
16031599 if not isinstance (request , pubsub .DeleteTopicRequest ):
16041600 request = pubsub .DeleteTopicRequest (request )
16051601 # If we have keyword arguments corresponding to fields on the
@@ -1686,10 +1682,8 @@ def sample_detach_subscription():
16861682
16871683 """
16881684 # Create or coerce a protobuf request object.
1689- # Minor optimization to avoid making a copy if the user passes
1690- # in a pubsub.DetachSubscriptionRequest.
1691- # There's no risk of modifying the input as we've already verified
1692- # there are no flattened fields.
1685+ # - Use the request object if provided (there's no risk of modifying the input as
1686+ # there are no flattened fields), or create one.
16931687 if not isinstance (request , pubsub .DetachSubscriptionRequest ):
16941688 request = pubsub .DetachSubscriptionRequest (request )
16951689
0 commit comments