1616from typing import Awaitable , Callable , Dict , Optional , Sequence , Tuple , Union
1717import warnings
1818
19+ from google .api_core import exceptions as core_exceptions
1920from google .api_core import gapic_v1 , grpc_helpers_async , operations_v1
21+ from google .api_core import retry_async as retries
2022from google .auth import credentials as ga_credentials # type: ignore
2123from google .auth .transport .grpc import SslCredentials # type: ignore
2224from google .cloud .location import locations_pb2 # type: ignore
@@ -73,7 +75,6 @@ def create_channel(
7375 the credentials from the environment.
7476 credentials_file (Optional[str]): A file with credentials that can
7577 be loaded with :func:`google.auth.load_credentials_from_file`.
76- This argument is ignored if ``channel`` is provided.
7778 scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
7879 service. These are only used when credentials are not specified and
7980 are passed to :func:`google.auth.default`.
@@ -103,7 +104,7 @@ def __init__(
103104 credentials : Optional [ga_credentials .Credentials ] = None ,
104105 credentials_file : Optional [str ] = None ,
105106 scopes : Optional [Sequence [str ]] = None ,
106- channel : Optional [aio .Channel ] = None ,
107+ channel : Optional [Union [ aio .Channel , Callable [..., aio . Channel ]] ] = None ,
107108 api_mtls_endpoint : Optional [str ] = None ,
108109 client_cert_source : Optional [Callable [[], Tuple [bytes , bytes ]]] = None ,
109110 ssl_channel_credentials : Optional [grpc .ChannelCredentials ] = None ,
@@ -123,15 +124,18 @@ def __init__(
123124 credentials identify the application to the service; if none
124125 are specified, the client will attempt to ascertain the
125126 credentials from the environment.
126- This argument is ignored if ``channel`` is provided.
127+ This argument is ignored if a ``channel`` instance is provided.
127128 credentials_file (Optional[str]): A file with credentials that can
128129 be loaded with :func:`google.auth.load_credentials_from_file`.
129- This argument is ignored if ``channel`` is provided.
130+ This argument is ignored if a ``channel`` instance is provided.
130131 scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
131132 service. These are only used when credentials are not specified and
132133 are passed to :func:`google.auth.default`.
133- channel (Optional[aio.Channel]): A ``Channel`` instance through
134- which to make calls.
134+ channel (Optional[Union[aio.Channel, Callable[..., aio.Channel]]]):
135+ A ``Channel`` instance through which to make calls, or a Callable
136+ that constructs and returns one. If set to None, ``self.create_channel``
137+ is used to create the channel. If a Callable is given, it will be called
138+ with the same arguments as used in ``self.create_channel``.
135139 api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
136140 If provided, it overrides the ``host`` argument and tries to create
137141 a mutual TLS channel with client SSL credentials from
@@ -141,11 +145,11 @@ def __init__(
141145 private key bytes, both in PEM format. It is ignored if
142146 ``api_mtls_endpoint`` is None.
143147 ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
144- for the grpc channel. It is ignored if ``channel`` is provided.
148+ for the grpc channel. It is ignored if a ``channel`` instance is provided.
145149 client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
146150 A callback to provide client certificate bytes and private key bytes,
147151 both in PEM format. It is used to configure a mutual TLS channel. It is
148- ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
152+ ignored if a ``channel`` instance or ``ssl_channel_credentials`` is provided.
149153 quota_project_id (Optional[str]): An optional project to use for billing
150154 and quota.
151155 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -172,7 +176,7 @@ def __init__(
172176 if client_cert_source :
173177 warnings .warn ("client_cert_source is deprecated" , DeprecationWarning )
174178
175- if channel :
179+ if isinstance ( channel , aio . Channel ) :
176180 # Ignore credentials if a channel was passed.
177181 credentials = False
178182 # If a channel was explicitly provided, set it.
@@ -212,7 +216,9 @@ def __init__(
212216 )
213217
214218 if not self ._grpc_channel :
215- self ._grpc_channel = type (self ).create_channel (
219+ # initialize with the provided callable or the default channel
220+ channel_init = channel or type (self ).create_channel
221+ self ._grpc_channel = channel_init (
216222 self ._host ,
217223 # use the credentials which are saved
218224 credentials = self ._credentials ,
@@ -915,6 +921,151 @@ def list_evaluations(
915921 )
916922 return self ._stubs ["list_evaluations" ]
917923
924+ def _prep_wrapped_messages (self , client_info ):
925+ """Precompute the wrapped methods, overriding the base class method to use async wrappers."""
926+ self ._wrapped_methods = {
927+ self .process_document : gapic_v1 .method_async .wrap_method (
928+ self .process_document ,
929+ default_retry = retries .AsyncRetry (
930+ initial = 0.1 ,
931+ maximum = 60.0 ,
932+ multiplier = 1.3 ,
933+ predicate = retries .if_exception_type (
934+ core_exceptions .DeadlineExceeded ,
935+ core_exceptions .ServiceUnavailable ,
936+ ),
937+ deadline = 300.0 ,
938+ ),
939+ default_timeout = 300.0 ,
940+ client_info = client_info ,
941+ ),
942+ self .batch_process_documents : gapic_v1 .method_async .wrap_method (
943+ self .batch_process_documents ,
944+ default_retry = retries .AsyncRetry (
945+ initial = 0.1 ,
946+ maximum = 60.0 ,
947+ multiplier = 1.3 ,
948+ predicate = retries .if_exception_type (
949+ core_exceptions .DeadlineExceeded ,
950+ core_exceptions .ServiceUnavailable ,
951+ ),
952+ deadline = 120.0 ,
953+ ),
954+ default_timeout = 120.0 ,
955+ client_info = client_info ,
956+ ),
957+ self .fetch_processor_types : gapic_v1 .method_async .wrap_method (
958+ self .fetch_processor_types ,
959+ default_timeout = None ,
960+ client_info = client_info ,
961+ ),
962+ self .list_processor_types : gapic_v1 .method_async .wrap_method (
963+ self .list_processor_types ,
964+ default_timeout = None ,
965+ client_info = client_info ,
966+ ),
967+ self .get_processor_type : gapic_v1 .method_async .wrap_method (
968+ self .get_processor_type ,
969+ default_timeout = None ,
970+ client_info = client_info ,
971+ ),
972+ self .list_processors : gapic_v1 .method_async .wrap_method (
973+ self .list_processors ,
974+ default_timeout = None ,
975+ client_info = client_info ,
976+ ),
977+ self .get_processor : gapic_v1 .method_async .wrap_method (
978+ self .get_processor ,
979+ default_timeout = None ,
980+ client_info = client_info ,
981+ ),
982+ self .train_processor_version : gapic_v1 .method_async .wrap_method (
983+ self .train_processor_version ,
984+ default_timeout = None ,
985+ client_info = client_info ,
986+ ),
987+ self .get_processor_version : gapic_v1 .method_async .wrap_method (
988+ self .get_processor_version ,
989+ default_timeout = None ,
990+ client_info = client_info ,
991+ ),
992+ self .list_processor_versions : gapic_v1 .method_async .wrap_method (
993+ self .list_processor_versions ,
994+ default_timeout = None ,
995+ client_info = client_info ,
996+ ),
997+ self .delete_processor_version : gapic_v1 .method_async .wrap_method (
998+ self .delete_processor_version ,
999+ default_timeout = None ,
1000+ client_info = client_info ,
1001+ ),
1002+ self .deploy_processor_version : gapic_v1 .method_async .wrap_method (
1003+ self .deploy_processor_version ,
1004+ default_timeout = None ,
1005+ client_info = client_info ,
1006+ ),
1007+ self .undeploy_processor_version : gapic_v1 .method_async .wrap_method (
1008+ self .undeploy_processor_version ,
1009+ default_timeout = None ,
1010+ client_info = client_info ,
1011+ ),
1012+ self .create_processor : gapic_v1 .method_async .wrap_method (
1013+ self .create_processor ,
1014+ default_timeout = None ,
1015+ client_info = client_info ,
1016+ ),
1017+ self .delete_processor : gapic_v1 .method_async .wrap_method (
1018+ self .delete_processor ,
1019+ default_timeout = None ,
1020+ client_info = client_info ,
1021+ ),
1022+ self .enable_processor : gapic_v1 .method_async .wrap_method (
1023+ self .enable_processor ,
1024+ default_timeout = None ,
1025+ client_info = client_info ,
1026+ ),
1027+ self .disable_processor : gapic_v1 .method_async .wrap_method (
1028+ self .disable_processor ,
1029+ default_timeout = None ,
1030+ client_info = client_info ,
1031+ ),
1032+ self .set_default_processor_version : gapic_v1 .method_async .wrap_method (
1033+ self .set_default_processor_version ,
1034+ default_timeout = None ,
1035+ client_info = client_info ,
1036+ ),
1037+ self .review_document : gapic_v1 .method_async .wrap_method (
1038+ self .review_document ,
1039+ default_retry = retries .AsyncRetry (
1040+ initial = 0.1 ,
1041+ maximum = 60.0 ,
1042+ multiplier = 1.3 ,
1043+ predicate = retries .if_exception_type (
1044+ core_exceptions .DeadlineExceeded ,
1045+ core_exceptions .ServiceUnavailable ,
1046+ ),
1047+ deadline = 120.0 ,
1048+ ),
1049+ default_timeout = 120.0 ,
1050+ client_info = client_info ,
1051+ ),
1052+ self .evaluate_processor_version : gapic_v1 .method_async .wrap_method (
1053+ self .evaluate_processor_version ,
1054+ default_timeout = None ,
1055+ client_info = client_info ,
1056+ ),
1057+ self .get_evaluation : gapic_v1 .method_async .wrap_method (
1058+ self .get_evaluation ,
1059+ default_timeout = None ,
1060+ client_info = client_info ,
1061+ ),
1062+ self .list_evaluations : gapic_v1 .method_async .wrap_method (
1063+ self .list_evaluations ,
1064+ default_timeout = None ,
1065+ client_info = client_info ,
1066+ ),
1067+ }
1068+
9181069 def close (self ):
9191070 return self .grpc_channel .close ()
9201071
0 commit comments