3030except ImportError :
3131 app_identity = None
3232try :
33- from grpc .beta import implementations
33+ from google .gax .grpc import exc_to_code as beta_exc_to_code
34+ import grpc
35+ from grpc ._channel import _Rendezvous
3436except ImportError : # pragma: NO COVER
35- implementations = None
37+ beta_exc_to_code = None
38+ grpc = None
39+ _Rendezvous = Exception
3640import six
3741from six .moves .http_client import HTTPConnection
3842from six .moves import configparser
@@ -572,10 +576,10 @@ def __call__(self, unused_context, callback):
572576 callback (headers , None )
573577
574578
575- def make_stub (credentials , user_agent , stub_factory , host , port ):
579+ def make_stub (credentials , user_agent , stub_class , host , port ):
576580 """Makes a stub for an RPC service.
577581
578- Uses / depends on the beta implementation of gRPC.
582+ Uses / depends on gRPC.
579583
580584 :type credentials: :class:`oauth2client.client.OAuth2Credentials`
581585 :param credentials: The OAuth2 Credentials to use for creating
@@ -584,29 +588,44 @@ def make_stub(credentials, user_agent, stub_factory, host, port):
584588 :type user_agent: str
585589 :param user_agent: (Optional) The user agent to be used with API requests.
586590
587- :type stub_factory: callable
588- :param stub_factory: A factory which will create a gRPC stub for
589- a given service.
591+ :type stub_class: type
592+ :param stub_class: A gRPC stub type for a given service.
590593
591594 :type host: str
592595 :param host: The host for the service.
593596
594597 :type port: int
595598 :param port: The port for the service.
596599
597- :rtype: :class:`grpc.beta._stub._AutoIntermediary `
600+ :rtype: object, instance of ``stub_class` `
598601 :returns: The stub object used to make gRPC requests to a given API.
599602 """
600603 # Leaving the first argument to ssl_channel_credentials() as None
601604 # loads root certificates from `grpc/_adapter/credentials/roots.pem`.
602- transport_creds = implementations .ssl_channel_credentials (None , None , None )
605+ transport_creds = grpc .ssl_channel_credentials (None , None , None )
603606 custom_metadata_plugin = MetadataPlugin (credentials , user_agent )
604- auth_creds = implementations .metadata_call_credentials (
607+ auth_creds = grpc .metadata_call_credentials (
605608 custom_metadata_plugin , name = 'google_creds' )
606- channel_creds = implementations .composite_channel_credentials (
609+ channel_creds = grpc .composite_channel_credentials (
607610 transport_creds , auth_creds )
608- channel = implementations .secure_channel (host , port , channel_creds )
609- return stub_factory (channel )
611+ target = '%s:%d' % (host , port )
612+ channel = grpc .secure_channel (target , channel_creds )
613+ return stub_class (channel )
614+
615+
616+ def exc_to_code (exc ):
617+ """Retrieves the status code from a gRPC exception.
618+
619+ :type exc: :class:`Exception`
620+ :param exc: An exception from gRPC beta or stable.
621+
622+ :rtype: :class:`grpc.StatusCode`
623+ :returns: The status code attached to the exception.
624+ """
625+ if isinstance (exc , _Rendezvous ):
626+ return exc .code ()
627+ else :
628+ return beta_exc_to_code (exc )
610629
611630
612631try :
0 commit comments