@@ -130,15 +130,19 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs):
130130 target = os .environ .get ("PUBSUB_EMULATOR_HOST" )
131131 )
132132
133+ # The GAPIC client has mTLS logic to determine the api endpoint and the
134+ # ssl credentials to use. Here we create a GAPIC client to help compute the
135+ # api endpoint and ssl credentials. The api endpoint will be used to set
136+ # `self._target`, and ssl credentials will be passed to
137+ # `grpc_helpers.create_channel` to establish a mTLS channel (if ssl
138+ # credentials is not None).
133139 client_options = kwargs .get ("client_options" , None )
134- if (
135- client_options
136- and "api_endpoint" in client_options
137- and isinstance (client_options ["api_endpoint" ], six .string_types )
138- ):
139- self ._target = client_options ["api_endpoint" ]
140- else :
141- self ._target = publisher_client .PublisherClient .SERVICE_ADDRESS
140+ credentials = kwargs .get ("credentials" , None )
141+ client_for_mtls_info = publisher_client .PublisherClient (
142+ credentials = credentials , client_options = client_options
143+ )
144+
145+ self ._target = client_for_mtls_info ._transport ._host
142146
143147 # Use a custom channel.
144148 # We need this in order to set appropriate default message size and
@@ -149,6 +153,7 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs):
149153 channel = grpc_helpers .create_channel (
150154 credentials = kwargs .pop ("credentials" , None ),
151155 target = self .target ,
156+ ssl_credentials = client_for_mtls_info ._transport ._ssl_channel_credentials ,
152157 scopes = publisher_client .PublisherClient ._DEFAULT_SCOPES ,
153158 options = {
154159 "grpc.max_send_message_length" : - 1 ,
0 commit comments