1# -*- coding: utf-8 -*-
2# Copyright 2022 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 warnings
17from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union
18
19from google.api_core import gapic_v1
20from google.api_core import grpc_helpers_async
21from google.auth import credentials as ga_credentials # type: ignore
22from google.auth.transport.grpc import SslCredentials # type: ignore
23
24import grpc # type: ignore
25from grpc.experimental import aio # type: ignore
26
27from google.iam.v1 import iam_policy_pb2 # type: ignore
28from google.iam.v1 import policy_pb2 # type: ignore
29from google.protobuf import empty_pb2 # type: ignore
30from google.pubsub_v1.types import schema
31from google.pubsub_v1.types import schema as gp_schema
32from .base import SchemaServiceTransport, DEFAULT_CLIENT_INFO
33from .grpc import SchemaServiceGrpcTransport
34
35
36class SchemaServiceGrpcAsyncIOTransport(SchemaServiceTransport):
37 """gRPC AsyncIO backend transport for SchemaService.
38
39 Service for doing schema-related operations.
40
41 This class defines the same methods as the primary client, so the
42 primary client can load the underlying transport implementation
43 and call it.
44
45 It sends protocol buffers over the wire using gRPC (which is built on
46 top of HTTP/2); the ``grpcio`` package must be installed.
47 """
48
49 _grpc_channel: aio.Channel
50 _stubs: Dict[str, Callable] = {}
51
52 @classmethod
53 def create_channel(
54 cls,
55 host: str = "pubsub.googleapis.com",
56 credentials: Optional[ga_credentials.Credentials] = None,
57 credentials_file: Optional[str] = None,
58 scopes: Optional[Sequence[str]] = None,
59 quota_project_id: Optional[str] = None,
60 **kwargs,
61 ) -> aio.Channel:
62 """Create and return a gRPC AsyncIO channel object.
63 Args:
64 host (Optional[str]): The host for the channel to use.
65 credentials (Optional[~.Credentials]): The
66 authorization credentials to attach to requests. These
67 credentials identify this application to the service. If
68 none are specified, the client will attempt to ascertain
69 the credentials from the environment.
70 credentials_file (Optional[str]): A file with credentials that can
71 be loaded with :func:`google.auth.load_credentials_from_file`.
72 This argument is ignored if ``channel`` is provided.
73 scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
74 service. These are only used when credentials are not specified and
75 are passed to :func:`google.auth.default`.
76 quota_project_id (Optional[str]): An optional project to use for billing
77 and quota.
78 kwargs (Optional[dict]): Keyword arguments, which are passed to the
79 channel creation.
80 Returns:
81 aio.Channel: A gRPC AsyncIO channel object.
82 """
83
84 return grpc_helpers_async.create_channel(
85 host,
86 credentials=credentials,
87 credentials_file=credentials_file,
88 quota_project_id=quota_project_id,
89 default_scopes=cls.AUTH_SCOPES,
90 scopes=scopes,
91 default_host=cls.DEFAULT_HOST,
92 **kwargs,
93 )
94
95 def __init__(
96 self,
97 *,
98 host: str = "pubsub.googleapis.com",
99 credentials: Optional[ga_credentials.Credentials] = None,
100 credentials_file: Optional[str] = None,
101 scopes: Optional[Sequence[str]] = None,
102 channel: Optional[aio.Channel] = None,
103 api_mtls_endpoint: Optional[str] = None,
104 client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None,
105 ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None,
106 client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None,
107 quota_project_id: Optional[str] = None,
108 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
109 always_use_jwt_access: Optional[bool] = False,
110 api_audience: Optional[str] = None,
111 ) -> None:
112 """Instantiate the transport.
113
114 Args:
115 host (Optional[str]):
116 The hostname to connect to.
117 credentials (Optional[google.auth.credentials.Credentials]): The
118 authorization credentials to attach to requests. These
119 credentials identify the application to the service; if none
120 are specified, the client will attempt to ascertain the
121 credentials from the environment.
122 This argument is ignored if ``channel`` is provided.
123 credentials_file (Optional[str]): A file with credentials that can
124 be loaded with :func:`google.auth.load_credentials_from_file`.
125 This argument is ignored if ``channel`` is provided.
126 scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
127 service. These are only used when credentials are not specified and
128 are passed to :func:`google.auth.default`.
129 channel (Optional[aio.Channel]): A ``Channel`` instance through
130 which to make calls.
131 api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
132 If provided, it overrides the ``host`` argument and tries to create
133 a mutual TLS channel with client SSL credentials from
134 ``client_cert_source`` or application default SSL credentials.
135 client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]):
136 Deprecated. A callback to provide client SSL certificate bytes and
137 private key bytes, both in PEM format. It is ignored if
138 ``api_mtls_endpoint`` is None.
139 ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
140 for the grpc channel. It is ignored if ``channel`` is provided.
141 client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
142 A callback to provide client certificate bytes and private key bytes,
143 both in PEM format. It is used to configure a mutual TLS channel. It is
144 ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
145 quota_project_id (Optional[str]): An optional project to use for billing
146 and quota.
147 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
148 The client info used to send a user-agent string along with
149 API requests. If ``None``, then default info will be used.
150 Generally, you only need to set this if you're developing
151 your own client library.
152 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
153 be used for service account credentials.
154
155 Raises:
156 google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
157 creation failed for any reason.
158 google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
159 and ``credentials_file`` are passed.
160 """
161 self._grpc_channel = None
162 self._ssl_channel_credentials = ssl_channel_credentials
163 self._stubs: Dict[str, Callable] = {}
164
165 if api_mtls_endpoint:
166 warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
167 if client_cert_source:
168 warnings.warn("client_cert_source is deprecated", DeprecationWarning)
169
170 if channel:
171 # Ignore credentials if a channel was passed.
172 credentials = False
173 # If a channel was explicitly provided, set it.
174 self._grpc_channel = channel
175 self._ssl_channel_credentials = None
176 else:
177 if api_mtls_endpoint:
178 host = api_mtls_endpoint
179
180 # Create SSL credentials with client_cert_source or application
181 # default SSL credentials.
182 if client_cert_source:
183 cert, key = client_cert_source()
184 self._ssl_channel_credentials = grpc.ssl_channel_credentials(
185 certificate_chain=cert, private_key=key
186 )
187 else:
188 self._ssl_channel_credentials = SslCredentials().ssl_credentials
189
190 else:
191 if client_cert_source_for_mtls and not ssl_channel_credentials:
192 cert, key = client_cert_source_for_mtls()
193 self._ssl_channel_credentials = grpc.ssl_channel_credentials(
194 certificate_chain=cert, private_key=key
195 )
196
197 # The base transport sets the host, credentials and scopes
198 super().__init__(
199 host=host,
200 credentials=credentials,
201 credentials_file=credentials_file,
202 scopes=scopes,
203 quota_project_id=quota_project_id,
204 client_info=client_info,
205 always_use_jwt_access=always_use_jwt_access,
206 api_audience=api_audience,
207 )
208
209 if not self._grpc_channel:
210 self._grpc_channel = type(self).create_channel(
211 self._host,
212 # use the credentials which are saved
213 credentials=self._credentials,
214 # Set ``credentials_file`` to ``None`` here as
215 # the credentials that we saved earlier should be used.
216 credentials_file=None,
217 scopes=self._scopes,
218 ssl_credentials=self._ssl_channel_credentials,
219 quota_project_id=quota_project_id,
220 options=[
221 ("grpc.max_send_message_length", -1),
222 ("grpc.max_receive_message_length", -1),
223 ("grpc.max_metadata_size", 4 * 1024 * 1024),
224 ("grpc.keepalive_time_ms", 30000),
225 ],
226 )
227
228 # Wrap messages. This must be done after self._grpc_channel exists
229 self._prep_wrapped_messages(client_info)
230
231 @property
232 def grpc_channel(self) -> aio.Channel:
233 """Create the channel designed to connect to this service.
234
235 This property caches on the instance; repeated calls return
236 the same channel.
237 """
238 # Return the channel from cache.
239 return self._grpc_channel
240
241 @property
242 def create_schema(
243 self,
244 ) -> Callable[[gp_schema.CreateSchemaRequest], Awaitable[gp_schema.Schema]]:
245 r"""Return a callable for the create schema method over gRPC.
246
247 Creates a schema.
248
249 Returns:
250 Callable[[~.CreateSchemaRequest],
251 Awaitable[~.Schema]]:
252 A function that, when called, will call the underlying RPC
253 on the server.
254 """
255 # Generate a "stub function" on-the-fly which will actually make
256 # the request.
257 # gRPC handles serialization and deserialization, so we just need
258 # to pass in the functions for each.
259 if "create_schema" not in self._stubs:
260 self._stubs["create_schema"] = self.grpc_channel.unary_unary(
261 "/google.pubsub.v1.SchemaService/CreateSchema",
262 request_serializer=gp_schema.CreateSchemaRequest.serialize,
263 response_deserializer=gp_schema.Schema.deserialize,
264 )
265 return self._stubs["create_schema"]
266
267 @property
268 def get_schema(
269 self,
270 ) -> Callable[[schema.GetSchemaRequest], Awaitable[schema.Schema]]:
271 r"""Return a callable for the get schema method over gRPC.
272
273 Gets a schema.
274
275 Returns:
276 Callable[[~.GetSchemaRequest],
277 Awaitable[~.Schema]]:
278 A function that, when called, will call the underlying RPC
279 on the server.
280 """
281 # Generate a "stub function" on-the-fly which will actually make
282 # the request.
283 # gRPC handles serialization and deserialization, so we just need
284 # to pass in the functions for each.
285 if "get_schema" not in self._stubs:
286 self._stubs["get_schema"] = self.grpc_channel.unary_unary(
287 "/google.pubsub.v1.SchemaService/GetSchema",
288 request_serializer=schema.GetSchemaRequest.serialize,
289 response_deserializer=schema.Schema.deserialize,
290 )
291 return self._stubs["get_schema"]
292
293 @property
294 def list_schemas(
295 self,
296 ) -> Callable[[schema.ListSchemasRequest], Awaitable[schema.ListSchemasResponse]]:
297 r"""Return a callable for the list schemas method over gRPC.
298
299 Lists schemas in a project.
300
301 Returns:
302 Callable[[~.ListSchemasRequest],
303 Awaitable[~.ListSchemasResponse]]:
304 A function that, when called, will call the underlying RPC
305 on the server.
306 """
307 # Generate a "stub function" on-the-fly which will actually make
308 # the request.
309 # gRPC handles serialization and deserialization, so we just need
310 # to pass in the functions for each.
311 if "list_schemas" not in self._stubs:
312 self._stubs["list_schemas"] = self.grpc_channel.unary_unary(
313 "/google.pubsub.v1.SchemaService/ListSchemas",
314 request_serializer=schema.ListSchemasRequest.serialize,
315 response_deserializer=schema.ListSchemasResponse.deserialize,
316 )
317 return self._stubs["list_schemas"]
318
319 @property
320 def list_schema_revisions(
321 self,
322 ) -> Callable[
323 [schema.ListSchemaRevisionsRequest],
324 Awaitable[schema.ListSchemaRevisionsResponse],
325 ]:
326 r"""Return a callable for the list schema revisions method over gRPC.
327
328 Lists all schema revisions for the named schema.
329
330 Returns:
331 Callable[[~.ListSchemaRevisionsRequest],
332 Awaitable[~.ListSchemaRevisionsResponse]]:
333 A function that, when called, will call the underlying RPC
334 on the server.
335 """
336 # Generate a "stub function" on-the-fly which will actually make
337 # the request.
338 # gRPC handles serialization and deserialization, so we just need
339 # to pass in the functions for each.
340 if "list_schema_revisions" not in self._stubs:
341 self._stubs["list_schema_revisions"] = self.grpc_channel.unary_unary(
342 "/google.pubsub.v1.SchemaService/ListSchemaRevisions",
343 request_serializer=schema.ListSchemaRevisionsRequest.serialize,
344 response_deserializer=schema.ListSchemaRevisionsResponse.deserialize,
345 )
346 return self._stubs["list_schema_revisions"]
347
348 @property
349 def commit_schema(
350 self,
351 ) -> Callable[[gp_schema.CommitSchemaRequest], Awaitable[gp_schema.Schema]]:
352 r"""Return a callable for the commit schema method over gRPC.
353
354 Commits a new schema revision to an existing schema.
355
356 Returns:
357 Callable[[~.CommitSchemaRequest],
358 Awaitable[~.Schema]]:
359 A function that, when called, will call the underlying RPC
360 on the server.
361 """
362 # Generate a "stub function" on-the-fly which will actually make
363 # the request.
364 # gRPC handles serialization and deserialization, so we just need
365 # to pass in the functions for each.
366 if "commit_schema" not in self._stubs:
367 self._stubs["commit_schema"] = self.grpc_channel.unary_unary(
368 "/google.pubsub.v1.SchemaService/CommitSchema",
369 request_serializer=gp_schema.CommitSchemaRequest.serialize,
370 response_deserializer=gp_schema.Schema.deserialize,
371 )
372 return self._stubs["commit_schema"]
373
374 @property
375 def rollback_schema(
376 self,
377 ) -> Callable[[schema.RollbackSchemaRequest], Awaitable[schema.Schema]]:
378 r"""Return a callable for the rollback schema method over gRPC.
379
380 Creates a new schema revision that is a copy of the provided
381 revision_id.
382
383 Returns:
384 Callable[[~.RollbackSchemaRequest],
385 Awaitable[~.Schema]]:
386 A function that, when called, will call the underlying RPC
387 on the server.
388 """
389 # Generate a "stub function" on-the-fly which will actually make
390 # the request.
391 # gRPC handles serialization and deserialization, so we just need
392 # to pass in the functions for each.
393 if "rollback_schema" not in self._stubs:
394 self._stubs["rollback_schema"] = self.grpc_channel.unary_unary(
395 "/google.pubsub.v1.SchemaService/RollbackSchema",
396 request_serializer=schema.RollbackSchemaRequest.serialize,
397 response_deserializer=schema.Schema.deserialize,
398 )
399 return self._stubs["rollback_schema"]
400
401 @property
402 def delete_schema_revision(
403 self,
404 ) -> Callable[[schema.DeleteSchemaRevisionRequest], Awaitable[schema.Schema]]:
405 r"""Return a callable for the delete schema revision method over gRPC.
406
407 Deletes a specific schema revision.
408
409 Returns:
410 Callable[[~.DeleteSchemaRevisionRequest],
411 Awaitable[~.Schema]]:
412 A function that, when called, will call the underlying RPC
413 on the server.
414 """
415 # Generate a "stub function" on-the-fly which will actually make
416 # the request.
417 # gRPC handles serialization and deserialization, so we just need
418 # to pass in the functions for each.
419 if "delete_schema_revision" not in self._stubs:
420 self._stubs["delete_schema_revision"] = self.grpc_channel.unary_unary(
421 "/google.pubsub.v1.SchemaService/DeleteSchemaRevision",
422 request_serializer=schema.DeleteSchemaRevisionRequest.serialize,
423 response_deserializer=schema.Schema.deserialize,
424 )
425 return self._stubs["delete_schema_revision"]
426
427 @property
428 def delete_schema(
429 self,
430 ) -> Callable[[schema.DeleteSchemaRequest], Awaitable[empty_pb2.Empty]]:
431 r"""Return a callable for the delete schema method over gRPC.
432
433 Deletes a schema.
434
435 Returns:
436 Callable[[~.DeleteSchemaRequest],
437 Awaitable[~.Empty]]:
438 A function that, when called, will call the underlying RPC
439 on the server.
440 """
441 # Generate a "stub function" on-the-fly which will actually make
442 # the request.
443 # gRPC handles serialization and deserialization, so we just need
444 # to pass in the functions for each.
445 if "delete_schema" not in self._stubs:
446 self._stubs["delete_schema"] = self.grpc_channel.unary_unary(
447 "/google.pubsub.v1.SchemaService/DeleteSchema",
448 request_serializer=schema.DeleteSchemaRequest.serialize,
449 response_deserializer=empty_pb2.Empty.FromString,
450 )
451 return self._stubs["delete_schema"]
452
453 @property
454 def validate_schema(
455 self,
456 ) -> Callable[
457 [gp_schema.ValidateSchemaRequest], Awaitable[gp_schema.ValidateSchemaResponse]
458 ]:
459 r"""Return a callable for the validate schema method over gRPC.
460
461 Validates a schema.
462
463 Returns:
464 Callable[[~.ValidateSchemaRequest],
465 Awaitable[~.ValidateSchemaResponse]]:
466 A function that, when called, will call the underlying RPC
467 on the server.
468 """
469 # Generate a "stub function" on-the-fly which will actually make
470 # the request.
471 # gRPC handles serialization and deserialization, so we just need
472 # to pass in the functions for each.
473 if "validate_schema" not in self._stubs:
474 self._stubs["validate_schema"] = self.grpc_channel.unary_unary(
475 "/google.pubsub.v1.SchemaService/ValidateSchema",
476 request_serializer=gp_schema.ValidateSchemaRequest.serialize,
477 response_deserializer=gp_schema.ValidateSchemaResponse.deserialize,
478 )
479 return self._stubs["validate_schema"]
480
481 @property
482 def validate_message(
483 self,
484 ) -> Callable[
485 [schema.ValidateMessageRequest], Awaitable[schema.ValidateMessageResponse]
486 ]:
487 r"""Return a callable for the validate message method over gRPC.
488
489 Validates a message against a schema.
490
491 Returns:
492 Callable[[~.ValidateMessageRequest],
493 Awaitable[~.ValidateMessageResponse]]:
494 A function that, when called, will call the underlying RPC
495 on the server.
496 """
497 # Generate a "stub function" on-the-fly which will actually make
498 # the request.
499 # gRPC handles serialization and deserialization, so we just need
500 # to pass in the functions for each.
501 if "validate_message" not in self._stubs:
502 self._stubs["validate_message"] = self.grpc_channel.unary_unary(
503 "/google.pubsub.v1.SchemaService/ValidateMessage",
504 request_serializer=schema.ValidateMessageRequest.serialize,
505 response_deserializer=schema.ValidateMessageResponse.deserialize,
506 )
507 return self._stubs["validate_message"]
508
509 @property
510 def set_iam_policy(
511 self,
512 ) -> Callable[[iam_policy_pb2.SetIamPolicyRequest], Awaitable[policy_pb2.Policy]]:
513 r"""Return a callable for the set iam policy method over gRPC.
514 Sets the IAM access control policy on the specified
515 function. Replaces any existing policy.
516 Returns:
517 Callable[[~.SetIamPolicyRequest],
518 Awaitable[~.Policy]]:
519 A function that, when called, will call the underlying RPC
520 on the server.
521 """
522 # Generate a "stub function" on-the-fly which will actually make
523 # the request.
524 # gRPC handles serialization and deserialization, so we just need
525 # to pass in the functions for each.
526 if "set_iam_policy" not in self._stubs:
527 self._stubs["set_iam_policy"] = self.grpc_channel.unary_unary(
528 "/google.iam.v1.IAMPolicy/SetIamPolicy",
529 request_serializer=iam_policy_pb2.SetIamPolicyRequest.SerializeToString,
530 response_deserializer=policy_pb2.Policy.FromString,
531 )
532 return self._stubs["set_iam_policy"]
533
534 @property
535 def get_iam_policy(
536 self,
537 ) -> Callable[[iam_policy_pb2.GetIamPolicyRequest], Awaitable[policy_pb2.Policy]]:
538 r"""Return a callable for the get iam policy method over gRPC.
539 Gets the IAM access control policy for a function.
540 Returns an empty policy if the function exists and does
541 not have a policy set.
542 Returns:
543 Callable[[~.GetIamPolicyRequest],
544 Awaitable[~.Policy]]:
545 A function that, when called, will call the underlying RPC
546 on the server.
547 """
548 # Generate a "stub function" on-the-fly which will actually make
549 # the request.
550 # gRPC handles serialization and deserialization, so we just need
551 # to pass in the functions for each.
552 if "get_iam_policy" not in self._stubs:
553 self._stubs["get_iam_policy"] = self.grpc_channel.unary_unary(
554 "/google.iam.v1.IAMPolicy/GetIamPolicy",
555 request_serializer=iam_policy_pb2.GetIamPolicyRequest.SerializeToString,
556 response_deserializer=policy_pb2.Policy.FromString,
557 )
558 return self._stubs["get_iam_policy"]
559
560 @property
561 def test_iam_permissions(
562 self,
563 ) -> Callable[
564 [iam_policy_pb2.TestIamPermissionsRequest],
565 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
566 ]:
567 r"""Return a callable for the test iam permissions method over gRPC.
568 Tests the specified permissions against the IAM access control
569 policy for a function. If the function does not exist, this will
570 return an empty set of permissions, not a NOT_FOUND error.
571 Returns:
572 Callable[[~.TestIamPermissionsRequest],
573 Awaitable[~.TestIamPermissionsResponse]]:
574 A function that, when called, will call the underlying RPC
575 on the server.
576 """
577 # Generate a "stub function" on-the-fly which will actually make
578 # the request.
579 # gRPC handles serialization and deserialization, so we just need
580 # to pass in the functions for each.
581 if "test_iam_permissions" not in self._stubs:
582 self._stubs["test_iam_permissions"] = self.grpc_channel.unary_unary(
583 "/google.iam.v1.IAMPolicy/TestIamPermissions",
584 request_serializer=iam_policy_pb2.TestIamPermissionsRequest.SerializeToString,
585 response_deserializer=iam_policy_pb2.TestIamPermissionsResponse.FromString,
586 )
587 return self._stubs["test_iam_permissions"]
588
589 def close(self):
590 return self.grpc_channel.close()
591
592
593__all__ = ("SchemaServiceGrpcAsyncIOTransport",)