11# -*- coding: utf-8 -*-
22
3- # Copyright (C) 2019 Google LLC
3+ # Copyright 2020 Google LLC
44#
55# Licensed under the Apache License, Version 2.0 (the "License");
66# you may not use this file except in compliance with the License.
1616#
1717
1818from collections import OrderedDict
19- from typing import Dict , Sequence , Tuple , Type , Union
19+ import re
20+ from typing import Callable , Dict , Sequence , Tuple , Type , Union
2021import pkg_resources
2122
2223import google .api_core .client_options as ClientOptions # type: ignore
@@ -77,8 +78,38 @@ class DocumentUnderstandingServiceClient(
7778 as natural language, computer vision, and translation.
7879 """
7980
80- DEFAULT_OPTIONS = ClientOptions .ClientOptions (
81- api_endpoint = "us-documentai.googleapis.com"
81+ @staticmethod
82+ def _get_default_mtls_endpoint (api_endpoint ):
83+ """Convert api endpoint to mTLS endpoint.
84+ Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to
85+ "*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively.
86+ Args:
87+ api_endpoint (Optional[str]): the api endpoint to convert.
88+ Returns:
89+ str: converted mTLS api endpoint.
90+ """
91+ if not api_endpoint :
92+ return api_endpoint
93+
94+ mtls_endpoint_re = re .compile (
95+ r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?"
96+ )
97+
98+ m = mtls_endpoint_re .match (api_endpoint )
99+ name , mtls , sandbox , googledomain = m .groups ()
100+ if mtls or not googledomain :
101+ return api_endpoint
102+
103+ if sandbox :
104+ return api_endpoint .replace (
105+ "sandbox.googleapis.com" , "mtls.sandbox.googleapis.com"
106+ )
107+
108+ return api_endpoint .replace (".googleapis.com" , ".mtls.googleapis.com" )
109+
110+ DEFAULT_ENDPOINT = "us-documentai.googleapis.com"
111+ DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint .__func__ ( # type: ignore
112+ DEFAULT_ENDPOINT
82113 )
83114
84115 @classmethod
@@ -106,7 +137,7 @@ def __init__(
106137 * ,
107138 credentials : credentials .Credentials = None ,
108139 transport : Union [str , DocumentUnderstandingServiceTransport ] = None ,
109- client_options : ClientOptions = DEFAULT_OPTIONS ,
140+ client_options : ClientOptions = None ,
110141 ) -> None :
111142 """Instantiate the document understanding service client.
112143
@@ -120,6 +151,17 @@ def __init__(
120151 transport to use. If set to None, a transport is chosen
121152 automatically.
122153 client_options (ClientOptions): Custom options for the client.
154+ (1) The ``api_endpoint`` property can be used to override the
155+ default endpoint provided by the client.
156+ (2) If ``transport`` argument is None, ``client_options`` can be
157+ used to create a mutual TLS transport. If ``client_cert_source``
158+ is provided, mutual TLS transport will be created with the given
159+ ``api_endpoint`` or the default mTLS endpoint, and the client
160+ SSL credentials obtained from ``client_cert_source``.
161+
162+ Raises:
163+ google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
164+ creation failed for any reason.
123165 """
124166 if isinstance (client_options , dict ):
125167 client_options = ClientOptions .from_dict (client_options )
@@ -128,17 +170,46 @@ def __init__(
128170 # Ordinarily, we provide the transport, but allowing a custom transport
129171 # instance provides an extensibility point for unusual situations.
130172 if isinstance (transport , DocumentUnderstandingServiceTransport ):
173+ # transport is a DocumentUnderstandingServiceTransport instance.
131174 if credentials :
132175 raise ValueError (
133176 "When providing a transport instance, "
134177 "provide its credentials directly."
135178 )
136179 self ._transport = transport
137- else :
180+ elif client_options is None or (
181+ client_options .api_endpoint is None
182+ and client_options .client_cert_source is None
183+ ):
184+ # Don't trigger mTLS if we get an empty ClientOptions.
138185 Transport = type (self ).get_transport_class (transport )
139186 self ._transport = Transport (
187+ credentials = credentials , host = self .DEFAULT_ENDPOINT
188+ )
189+ else :
190+ # We have a non-empty ClientOptions. If client_cert_source is
191+ # provided, trigger mTLS with user provided endpoint or the default
192+ # mTLS endpoint.
193+ if client_options .client_cert_source :
194+ api_mtls_endpoint = (
195+ client_options .api_endpoint
196+ if client_options .api_endpoint
197+ else self .DEFAULT_MTLS_ENDPOINT
198+ )
199+ else :
200+ api_mtls_endpoint = None
201+
202+ api_endpoint = (
203+ client_options .api_endpoint
204+ if client_options .api_endpoint
205+ else self .DEFAULT_ENDPOINT
206+ )
207+
208+ self ._transport = DocumentUnderstandingServiceGrpcTransport (
140209 credentials = credentials ,
141- host = client_options .api_endpoint or "us-documentai.googleapis.com" ,
210+ host = api_endpoint ,
211+ api_mtls_endpoint = api_mtls_endpoint ,
212+ client_cert_source = client_options .client_cert_source ,
142213 )
143214
144215 def batch_process_documents (
@@ -195,6 +266,7 @@ def batch_process_documents(
195266
196267 # If we have keyword arguments corresponding to fields on the
197268 # request, apply these.
269+
198270 if requests is not None :
199271 request .requests = requests
200272
0 commit comments