1# -*- coding: utf-8 -*-
2# Copyright 2025 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import abc
17from typing import Awaitable, Callable, Dict, Optional, Sequence, Union
18
19from google.pubsub_v1 import gapic_version as package_version
20
21import google.auth # type: ignore
22import google.api_core
23from google.api_core import exceptions as core_exceptions
24from google.api_core import gapic_v1
25from google.api_core import retry as retries
26from google.auth import credentials as ga_credentials # type: ignore
27from google.oauth2 import service_account # type: ignore
28import google.protobuf
29
30from google.iam.v1 import iam_policy_pb2 # type: ignore
31from google.iam.v1 import policy_pb2 # type: ignore
32from google.protobuf import empty_pb2 # type: ignore
33from google.pubsub_v1.types import schema
34from google.pubsub_v1.types import schema as gp_schema
35
36DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
37 client_library_version=package_version.__version__
38)
39
40if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER
41 DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__
42
43
44class SchemaServiceTransport(abc.ABC):
45 """Abstract transport class for SchemaService."""
46
47 AUTH_SCOPES = (
48 "https://www.googleapis.com/auth/cloud-platform",
49 "https://www.googleapis.com/auth/pubsub",
50 )
51
52 DEFAULT_HOST: str = "pubsub.googleapis.com"
53
54 def __init__(
55 self,
56 *,
57 host: str = DEFAULT_HOST,
58 credentials: Optional[ga_credentials.Credentials] = None,
59 credentials_file: Optional[str] = None,
60 scopes: Optional[Sequence[str]] = None,
61 quota_project_id: Optional[str] = None,
62 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
63 always_use_jwt_access: Optional[bool] = False,
64 api_audience: Optional[str] = None,
65 **kwargs,
66 ) -> None:
67 """Instantiate the transport.
68
69 Args:
70 host (Optional[str]):
71 The hostname to connect to (default: 'pubsub.googleapis.com').
72 credentials (Optional[google.auth.credentials.Credentials]): The
73 authorization credentials to attach to requests. These
74 credentials identify the application to the service; if none
75 are specified, the client will attempt to ascertain the
76 credentials from the environment.
77 credentials_file (Optional[str]): Deprecated. A file with credentials that can
78 be loaded with :func:`google.auth.load_credentials_from_file`.
79 This argument is mutually exclusive with credentials. This argument will be
80 removed in the next major version of this library.
81 scopes (Optional[Sequence[str]]): A list of scopes.
82 quota_project_id (Optional[str]): An optional project to use for billing
83 and quota.
84 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
85 The client info used to send a user-agent string along with
86 API requests. If ``None``, then default info will be used.
87 Generally, you only need to set this if you're developing
88 your own client library.
89 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
90 be used for service account credentials.
91 """
92
93 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}
94
95 # Save the scopes.
96 self._scopes = scopes
97 if not hasattr(self, "_ignore_credentials"):
98 self._ignore_credentials: bool = False
99
100 # If no credentials are provided, then determine the appropriate
101 # defaults.
102 if credentials and credentials_file:
103 raise core_exceptions.DuplicateCredentialArgs(
104 "'credentials_file' and 'credentials' are mutually exclusive"
105 )
106
107 if credentials_file is not None:
108 credentials, _ = google.auth.load_credentials_from_file(
109 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
110 )
111 elif credentials is None and not self._ignore_credentials:
112 credentials, _ = google.auth.default(
113 **scopes_kwargs, quota_project_id=quota_project_id
114 )
115 # Don't apply audience if the credentials file passed from user.
116 if hasattr(credentials, "with_gdch_audience"):
117 credentials = credentials.with_gdch_audience(
118 api_audience if api_audience else host
119 )
120
121 # If the credentials are service account credentials, then always try to use self signed JWT.
122 if (
123 always_use_jwt_access
124 and isinstance(credentials, service_account.Credentials)
125 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
126 ):
127 credentials = credentials.with_always_use_jwt_access(True)
128
129 # Save the credentials.
130 self._credentials = credentials
131
132 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
133 if ":" not in host:
134 host += ":443"
135 self._host = host
136
137 @property
138 def host(self):
139 return self._host
140
141 def _prep_wrapped_messages(self, client_info):
142 # Precompute the wrapped methods.
143 self._wrapped_methods = {
144 self.create_schema: gapic_v1.method.wrap_method(
145 self.create_schema,
146 default_retry=retries.Retry(
147 initial=0.1,
148 maximum=60.0,
149 multiplier=1.3,
150 predicate=retries.if_exception_type(
151 core_exceptions.ServiceUnavailable,
152 ),
153 deadline=60.0,
154 ),
155 default_timeout=60.0,
156 client_info=client_info,
157 ),
158 self.get_schema: gapic_v1.method.wrap_method(
159 self.get_schema,
160 default_retry=retries.Retry(
161 initial=0.1,
162 maximum=60.0,
163 multiplier=1.3,
164 predicate=retries.if_exception_type(
165 core_exceptions.ServiceUnavailable,
166 ),
167 deadline=60.0,
168 ),
169 default_timeout=60.0,
170 client_info=client_info,
171 ),
172 self.list_schemas: gapic_v1.method.wrap_method(
173 self.list_schemas,
174 default_retry=retries.Retry(
175 initial=0.1,
176 maximum=60.0,
177 multiplier=1.3,
178 predicate=retries.if_exception_type(
179 core_exceptions.ServiceUnavailable,
180 ),
181 deadline=60.0,
182 ),
183 default_timeout=60.0,
184 client_info=client_info,
185 ),
186 self.list_schema_revisions: gapic_v1.method.wrap_method(
187 self.list_schema_revisions,
188 default_retry=retries.Retry(
189 initial=0.1,
190 maximum=60.0,
191 multiplier=1.3,
192 predicate=retries.if_exception_type(
193 core_exceptions.ServiceUnavailable,
194 ),
195 deadline=60.0,
196 ),
197 default_timeout=60.0,
198 client_info=client_info,
199 ),
200 self.commit_schema: gapic_v1.method.wrap_method(
201 self.commit_schema,
202 default_retry=retries.Retry(
203 initial=0.1,
204 maximum=60.0,
205 multiplier=1.3,
206 predicate=retries.if_exception_type(
207 core_exceptions.ServiceUnavailable,
208 ),
209 deadline=60.0,
210 ),
211 default_timeout=60.0,
212 client_info=client_info,
213 ),
214 self.rollback_schema: gapic_v1.method.wrap_method(
215 self.rollback_schema,
216 default_retry=retries.Retry(
217 initial=0.1,
218 maximum=60.0,
219 multiplier=1.3,
220 predicate=retries.if_exception_type(
221 core_exceptions.ServiceUnavailable,
222 ),
223 deadline=60.0,
224 ),
225 default_timeout=60.0,
226 client_info=client_info,
227 ),
228 self.delete_schema_revision: gapic_v1.method.wrap_method(
229 self.delete_schema_revision,
230 default_retry=retries.Retry(
231 initial=0.1,
232 maximum=60.0,
233 multiplier=1.3,
234 predicate=retries.if_exception_type(
235 core_exceptions.ServiceUnavailable,
236 ),
237 deadline=60.0,
238 ),
239 default_timeout=60.0,
240 client_info=client_info,
241 ),
242 self.delete_schema: gapic_v1.method.wrap_method(
243 self.delete_schema,
244 default_retry=retries.Retry(
245 initial=0.1,
246 maximum=60.0,
247 multiplier=1.3,
248 predicate=retries.if_exception_type(
249 core_exceptions.ServiceUnavailable,
250 ),
251 deadline=60.0,
252 ),
253 default_timeout=60.0,
254 client_info=client_info,
255 ),
256 self.validate_schema: gapic_v1.method.wrap_method(
257 self.validate_schema,
258 default_retry=retries.Retry(
259 initial=0.1,
260 maximum=60.0,
261 multiplier=1.3,
262 predicate=retries.if_exception_type(
263 core_exceptions.ServiceUnavailable,
264 ),
265 deadline=60.0,
266 ),
267 default_timeout=60.0,
268 client_info=client_info,
269 ),
270 self.validate_message: gapic_v1.method.wrap_method(
271 self.validate_message,
272 default_retry=retries.Retry(
273 initial=0.1,
274 maximum=60.0,
275 multiplier=1.3,
276 predicate=retries.if_exception_type(
277 core_exceptions.ServiceUnavailable,
278 ),
279 deadline=60.0,
280 ),
281 default_timeout=60.0,
282 client_info=client_info,
283 ),
284 self.get_iam_policy: gapic_v1.method.wrap_method(
285 self.get_iam_policy,
286 default_timeout=None,
287 client_info=client_info,
288 ),
289 self.set_iam_policy: gapic_v1.method.wrap_method(
290 self.set_iam_policy,
291 default_timeout=None,
292 client_info=client_info,
293 ),
294 self.test_iam_permissions: gapic_v1.method.wrap_method(
295 self.test_iam_permissions,
296 default_timeout=None,
297 client_info=client_info,
298 ),
299 }
300
301 def close(self):
302 """Closes resources associated with the transport.
303
304 .. warning::
305 Only call this method if the transport is NOT shared
306 with other clients - this may cause errors in other clients!
307 """
308 raise NotImplementedError()
309
310 @property
311 def create_schema(
312 self,
313 ) -> Callable[
314 [gp_schema.CreateSchemaRequest],
315 Union[gp_schema.Schema, Awaitable[gp_schema.Schema]],
316 ]:
317 raise NotImplementedError()
318
319 @property
320 def get_schema(
321 self,
322 ) -> Callable[
323 [schema.GetSchemaRequest], Union[schema.Schema, Awaitable[schema.Schema]]
324 ]:
325 raise NotImplementedError()
326
327 @property
328 def list_schemas(
329 self,
330 ) -> Callable[
331 [schema.ListSchemasRequest],
332 Union[schema.ListSchemasResponse, Awaitable[schema.ListSchemasResponse]],
333 ]:
334 raise NotImplementedError()
335
336 @property
337 def list_schema_revisions(
338 self,
339 ) -> Callable[
340 [schema.ListSchemaRevisionsRequest],
341 Union[
342 schema.ListSchemaRevisionsResponse,
343 Awaitable[schema.ListSchemaRevisionsResponse],
344 ],
345 ]:
346 raise NotImplementedError()
347
348 @property
349 def commit_schema(
350 self,
351 ) -> Callable[
352 [gp_schema.CommitSchemaRequest],
353 Union[gp_schema.Schema, Awaitable[gp_schema.Schema]],
354 ]:
355 raise NotImplementedError()
356
357 @property
358 def rollback_schema(
359 self,
360 ) -> Callable[
361 [schema.RollbackSchemaRequest], Union[schema.Schema, Awaitable[schema.Schema]]
362 ]:
363 raise NotImplementedError()
364
365 @property
366 def delete_schema_revision(
367 self,
368 ) -> Callable[
369 [schema.DeleteSchemaRevisionRequest],
370 Union[schema.Schema, Awaitable[schema.Schema]],
371 ]:
372 raise NotImplementedError()
373
374 @property
375 def delete_schema(
376 self,
377 ) -> Callable[
378 [schema.DeleteSchemaRequest], Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]]
379 ]:
380 raise NotImplementedError()
381
382 @property
383 def validate_schema(
384 self,
385 ) -> Callable[
386 [gp_schema.ValidateSchemaRequest],
387 Union[
388 gp_schema.ValidateSchemaResponse,
389 Awaitable[gp_schema.ValidateSchemaResponse],
390 ],
391 ]:
392 raise NotImplementedError()
393
394 @property
395 def validate_message(
396 self,
397 ) -> Callable[
398 [schema.ValidateMessageRequest],
399 Union[
400 schema.ValidateMessageResponse, Awaitable[schema.ValidateMessageResponse]
401 ],
402 ]:
403 raise NotImplementedError()
404
405 @property
406 def set_iam_policy(
407 self,
408 ) -> Callable[
409 [iam_policy_pb2.SetIamPolicyRequest],
410 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
411 ]:
412 raise NotImplementedError()
413
414 @property
415 def get_iam_policy(
416 self,
417 ) -> Callable[
418 [iam_policy_pb2.GetIamPolicyRequest],
419 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
420 ]:
421 raise NotImplementedError()
422
423 @property
424 def test_iam_permissions(
425 self,
426 ) -> Callable[
427 [iam_policy_pb2.TestIamPermissionsRequest],
428 Union[
429 iam_policy_pb2.TestIamPermissionsResponse,
430 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
431 ],
432 ]:
433 raise NotImplementedError()
434
435 @property
436 def kind(self) -> str:
437 raise NotImplementedError()
438
439
440__all__ = ("SchemaServiceTransport",)