2424from google .api_core import gapic_v1 # type: ignore
2525from google .api_core import retry as retries # type: ignore
2626from google .auth import credentials as ga_credentials # type: ignore
27+ from google .oauth2 import service_account # type: ignore
2728
2829from google .cloud .dialogflow_v2 .types import answer_record
2930from google .cloud .dialogflow_v2 .types import answer_record as gcd_answer_record
4647 except pkg_resources .DistributionNotFound : # pragma: NO COVER
4748 _GOOGLE_AUTH_VERSION = None
4849
49- _API_CORE_VERSION = google .api_core .__version__
50-
5150
5251class AnswerRecordsTransport (abc .ABC ):
5352 """Abstract transport class for AnswerRecords."""
@@ -68,6 +67,7 @@ def __init__(
6867 scopes : Optional [Sequence [str ]] = None ,
6968 quota_project_id : Optional [str ] = None ,
7069 client_info : gapic_v1 .client_info .ClientInfo = DEFAULT_CLIENT_INFO ,
70+ always_use_jwt_access : Optional [bool ] = False ,
7171 ** kwargs ,
7272 ) -> None :
7373 """Instantiate the transport.
@@ -91,6 +91,8 @@ def __init__(
9191 API requests. If ``None``, then default info will be used.
9292 Generally, you only need to set this if you're developing
9393 your own client library.
94+ always_use_jwt_access (Optional[bool]): Whether self signed JWT should
95+ be used for service account credentials.
9496 """
9597 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
9698 if ":" not in host :
@@ -100,7 +102,7 @@ def __init__(
100102 scopes_kwargs = self ._get_scopes_kwargs (self ._host , scopes )
101103
102104 # Save the scopes.
103- self ._scopes = scopes or self . AUTH_SCOPES
105+ self ._scopes = scopes
104106
105107 # If no credentials are provided, then determine the appropriate
106108 # defaults.
@@ -119,13 +121,20 @@ def __init__(
119121 ** scopes_kwargs , quota_project_id = quota_project_id
120122 )
121123
124+ # If the credentials is service account credentials, then always try to use self signed JWT.
125+ if (
126+ always_use_jwt_access
127+ and isinstance (credentials , service_account .Credentials )
128+ and hasattr (service_account .Credentials , "with_always_use_jwt_access" )
129+ ):
130+ credentials = credentials .with_always_use_jwt_access (True )
131+
122132 # Save the credentials.
123133 self ._credentials = credentials
124134
125- # TODO(busunkim): These two class methods are in the base transport
135+ # TODO(busunkim): This method is in the base transport
126136 # to avoid duplicating code across the transport classes. These functions
127- # should be deleted once the minimum required versions of google-api-core
128- # and google-auth are increased.
137+ # should be deleted once the minimum required versions of google-auth is increased.
129138
130139 # TODO: Remove this function once google-auth >= 1.25.0 is required
131140 @classmethod
@@ -146,27 +155,6 @@ def _get_scopes_kwargs(
146155
147156 return scopes_kwargs
148157
149- # TODO: Remove this function once google-api-core >= 1.26.0 is required
150- @classmethod
151- def _get_self_signed_jwt_kwargs (
152- cls , host : str , scopes : Optional [Sequence [str ]]
153- ) -> Dict [str , Union [Optional [Sequence [str ]], str ]]:
154- """Returns kwargs to pass to grpc_helpers.create_channel depending on the google-api-core version"""
155-
156- self_signed_jwt_kwargs : Dict [str , Union [Optional [Sequence [str ]], str ]] = {}
157-
158- if _API_CORE_VERSION and (
159- packaging .version .parse (_API_CORE_VERSION )
160- >= packaging .version .parse ("1.26.0" )
161- ):
162- self_signed_jwt_kwargs ["default_scopes" ] = cls .AUTH_SCOPES
163- self_signed_jwt_kwargs ["scopes" ] = scopes
164- self_signed_jwt_kwargs ["default_host" ] = cls .DEFAULT_HOST
165- else :
166- self_signed_jwt_kwargs ["scopes" ] = scopes or cls .AUTH_SCOPES
167-
168- return self_signed_jwt_kwargs
169-
170158 def _prep_wrapped_messages (self , client_info ):
171159 # Precompute the wrapped methods.
172160 self ._wrapped_methods = {
0 commit comments