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]): 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.
80 scopes (Optional[Sequence[str]]): A list of scopes.
81 quota_project_id (Optional[str]): An optional project to use for billing
82 and quota.
83 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
84 The client info used to send a user-agent string along with
85 API requests. If ``None``, then default info will be used.
86 Generally, you only need to set this if you're developing
87 your own client library.
88 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
89 be used for service account credentials.
90 """
91
92 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}
93
94 # Save the scopes.
95 self._scopes = scopes
96 if not hasattr(self, "_ignore_credentials"):
97 self._ignore_credentials: bool = False
98
99 # If no credentials are provided, then determine the appropriate
100 # defaults.
101 if credentials and credentials_file:
102 raise core_exceptions.DuplicateCredentialArgs(
103 "'credentials_file' and 'credentials' are mutually exclusive"
104 )
105
106 if credentials_file is not None:
107 credentials, _ = google.auth.load_credentials_from_file(
108 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
109 )
110 elif credentials is None and not self._ignore_credentials:
111 credentials, _ = google.auth.default(
112 **scopes_kwargs, quota_project_id=quota_project_id
113 )
114 # Don't apply audience if the credentials file passed from user.
115 if hasattr(credentials, "with_gdch_audience"):
116 credentials = credentials.with_gdch_audience(
117 api_audience if api_audience else host
118 )
119
120 # If the credentials are service account credentials, then always try to use self signed JWT.
121 if (
122 always_use_jwt_access
123 and isinstance(credentials, service_account.Credentials)
124 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
125 ):
126 credentials = credentials.with_always_use_jwt_access(True)
127
128 # Save the credentials.
129 self._credentials = credentials
130
131 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
132 if ":" not in host:
133 host += ":443"
134 self._host = host
135
136 @property
137 def host(self):
138 return self._host
139
140 def _prep_wrapped_messages(self, client_info):
141 # Precompute the wrapped methods.
142 self._wrapped_methods = {
143 self.create_schema: gapic_v1.method.wrap_method(
144 self.create_schema,
145 default_retry=retries.Retry(
146 initial=0.1,
147 maximum=60.0,
148 multiplier=1.3,
149 predicate=retries.if_exception_type(
150 core_exceptions.ServiceUnavailable,
151 ),
152 deadline=60.0,
153 ),
154 default_timeout=60.0,
155 client_info=client_info,
156 ),
157 self.get_schema: gapic_v1.method.wrap_method(
158 self.get_schema,
159 default_retry=retries.Retry(
160 initial=0.1,
161 maximum=60.0,
162 multiplier=1.3,
163 predicate=retries.if_exception_type(
164 core_exceptions.ServiceUnavailable,
165 ),
166 deadline=60.0,
167 ),
168 default_timeout=60.0,
169 client_info=client_info,
170 ),
171 self.list_schemas: gapic_v1.method.wrap_method(
172 self.list_schemas,
173 default_retry=retries.Retry(
174 initial=0.1,
175 maximum=60.0,
176 multiplier=1.3,
177 predicate=retries.if_exception_type(
178 core_exceptions.ServiceUnavailable,
179 ),
180 deadline=60.0,
181 ),
182 default_timeout=60.0,
183 client_info=client_info,
184 ),
185 self.list_schema_revisions: gapic_v1.method.wrap_method(
186 self.list_schema_revisions,
187 default_retry=retries.Retry(
188 initial=0.1,
189 maximum=60.0,
190 multiplier=1.3,
191 predicate=retries.if_exception_type(
192 core_exceptions.ServiceUnavailable,
193 ),
194 deadline=60.0,
195 ),
196 default_timeout=60.0,
197 client_info=client_info,
198 ),
199 self.commit_schema: gapic_v1.method.wrap_method(
200 self.commit_schema,
201 default_retry=retries.Retry(
202 initial=0.1,
203 maximum=60.0,
204 multiplier=1.3,
205 predicate=retries.if_exception_type(
206 core_exceptions.ServiceUnavailable,
207 ),
208 deadline=60.0,
209 ),
210 default_timeout=60.0,
211 client_info=client_info,
212 ),
213 self.rollback_schema: gapic_v1.method.wrap_method(
214 self.rollback_schema,
215 default_retry=retries.Retry(
216 initial=0.1,
217 maximum=60.0,
218 multiplier=1.3,
219 predicate=retries.if_exception_type(
220 core_exceptions.ServiceUnavailable,
221 ),
222 deadline=60.0,
223 ),
224 default_timeout=60.0,
225 client_info=client_info,
226 ),
227 self.delete_schema_revision: gapic_v1.method.wrap_method(
228 self.delete_schema_revision,
229 default_retry=retries.Retry(
230 initial=0.1,
231 maximum=60.0,
232 multiplier=1.3,
233 predicate=retries.if_exception_type(
234 core_exceptions.ServiceUnavailable,
235 ),
236 deadline=60.0,
237 ),
238 default_timeout=60.0,
239 client_info=client_info,
240 ),
241 self.delete_schema: gapic_v1.method.wrap_method(
242 self.delete_schema,
243 default_retry=retries.Retry(
244 initial=0.1,
245 maximum=60.0,
246 multiplier=1.3,
247 predicate=retries.if_exception_type(
248 core_exceptions.ServiceUnavailable,
249 ),
250 deadline=60.0,
251 ),
252 default_timeout=60.0,
253 client_info=client_info,
254 ),
255 self.validate_schema: gapic_v1.method.wrap_method(
256 self.validate_schema,
257 default_retry=retries.Retry(
258 initial=0.1,
259 maximum=60.0,
260 multiplier=1.3,
261 predicate=retries.if_exception_type(
262 core_exceptions.ServiceUnavailable,
263 ),
264 deadline=60.0,
265 ),
266 default_timeout=60.0,
267 client_info=client_info,
268 ),
269 self.validate_message: gapic_v1.method.wrap_method(
270 self.validate_message,
271 default_retry=retries.Retry(
272 initial=0.1,
273 maximum=60.0,
274 multiplier=1.3,
275 predicate=retries.if_exception_type(
276 core_exceptions.ServiceUnavailable,
277 ),
278 deadline=60.0,
279 ),
280 default_timeout=60.0,
281 client_info=client_info,
282 ),
283 self.get_iam_policy: gapic_v1.method.wrap_method(
284 self.get_iam_policy,
285 default_timeout=None,
286 client_info=client_info,
287 ),
288 self.set_iam_policy: gapic_v1.method.wrap_method(
289 self.set_iam_policy,
290 default_timeout=None,
291 client_info=client_info,
292 ),
293 self.test_iam_permissions: gapic_v1.method.wrap_method(
294 self.test_iam_permissions,
295 default_timeout=None,
296 client_info=client_info,
297 ),
298 }
299
300 def close(self):
301 """Closes resources associated with the transport.
302
303 .. warning::
304 Only call this method if the transport is NOT shared
305 with other clients - this may cause errors in other clients!
306 """
307 raise NotImplementedError()
308
309 @property
310 def create_schema(
311 self,
312 ) -> Callable[
313 [gp_schema.CreateSchemaRequest],
314 Union[gp_schema.Schema, Awaitable[gp_schema.Schema]],
315 ]:
316 raise NotImplementedError()
317
318 @property
319 def get_schema(
320 self,
321 ) -> Callable[
322 [schema.GetSchemaRequest], Union[schema.Schema, Awaitable[schema.Schema]]
323 ]:
324 raise NotImplementedError()
325
326 @property
327 def list_schemas(
328 self,
329 ) -> Callable[
330 [schema.ListSchemasRequest],
331 Union[schema.ListSchemasResponse, Awaitable[schema.ListSchemasResponse]],
332 ]:
333 raise NotImplementedError()
334
335 @property
336 def list_schema_revisions(
337 self,
338 ) -> Callable[
339 [schema.ListSchemaRevisionsRequest],
340 Union[
341 schema.ListSchemaRevisionsResponse,
342 Awaitable[schema.ListSchemaRevisionsResponse],
343 ],
344 ]:
345 raise NotImplementedError()
346
347 @property
348 def commit_schema(
349 self,
350 ) -> Callable[
351 [gp_schema.CommitSchemaRequest],
352 Union[gp_schema.Schema, Awaitable[gp_schema.Schema]],
353 ]:
354 raise NotImplementedError()
355
356 @property
357 def rollback_schema(
358 self,
359 ) -> Callable[
360 [schema.RollbackSchemaRequest], Union[schema.Schema, Awaitable[schema.Schema]]
361 ]:
362 raise NotImplementedError()
363
364 @property
365 def delete_schema_revision(
366 self,
367 ) -> Callable[
368 [schema.DeleteSchemaRevisionRequest],
369 Union[schema.Schema, Awaitable[schema.Schema]],
370 ]:
371 raise NotImplementedError()
372
373 @property
374 def delete_schema(
375 self,
376 ) -> Callable[
377 [schema.DeleteSchemaRequest], Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]]
378 ]:
379 raise NotImplementedError()
380
381 @property
382 def validate_schema(
383 self,
384 ) -> Callable[
385 [gp_schema.ValidateSchemaRequest],
386 Union[
387 gp_schema.ValidateSchemaResponse,
388 Awaitable[gp_schema.ValidateSchemaResponse],
389 ],
390 ]:
391 raise NotImplementedError()
392
393 @property
394 def validate_message(
395 self,
396 ) -> Callable[
397 [schema.ValidateMessageRequest],
398 Union[
399 schema.ValidateMessageResponse, Awaitable[schema.ValidateMessageResponse]
400 ],
401 ]:
402 raise NotImplementedError()
403
404 @property
405 def set_iam_policy(
406 self,
407 ) -> Callable[
408 [iam_policy_pb2.SetIamPolicyRequest],
409 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
410 ]:
411 raise NotImplementedError()
412
413 @property
414 def get_iam_policy(
415 self,
416 ) -> Callable[
417 [iam_policy_pb2.GetIamPolicyRequest],
418 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
419 ]:
420 raise NotImplementedError()
421
422 @property
423 def test_iam_permissions(
424 self,
425 ) -> Callable[
426 [iam_policy_pb2.TestIamPermissionsRequest],
427 Union[
428 iam_policy_pb2.TestIamPermissionsResponse,
429 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
430 ],
431 ]:
432 raise NotImplementedError()
433
434 @property
435 def kind(self) -> str:
436 raise NotImplementedError()
437
438
439__all__ = ("SchemaServiceTransport",)