def make_stub(credentials, user_agent, stub_factory, host, port): """Makes a stub for an RPC service. Uses / depends on the beta implementation of gRPC. :type credentials: :class:`oauth2client.client.OAuth2Credentials` :param credentials: The OAuth2 Credentials to use for creating access tokens. :type user_agent: str :param user_agent: (Optional) The user agent to be used with API requests. :type stub_factory: callable :param stub_factory: A factory which will create a gRPC stub for a given service. :type host: str :param host: The host for the service. :type port: int :param port: The port for the service. :rtype: :class:`grpc.beta._stub._AutoIntermediary` :returns: The stub object used to make gRPC requests to a given API. """ # Leaving the first argument to ssl_channel_credentials() as None # loads root certificates from `grpc/_adapter/credentials/roots.pem`. transport_creds = implementations.ssl_channel_credentials(None, None, None) custom_metadata_plugin = MetadataPlugin(credentials, user_agent) auth_creds = implementations.metadata_call_credentials( custom_metadata_plugin, name='google_creds') channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) channel = implementations.secure_channel(host, port, channel_creds) return stub_factory(channel)
def make_channel(host, port): ssl_channel = implementations.ssl_channel_credentials(None, None, None) creds = get_credentials().create_scoped(args.speech_scope) auth_header = ("authorization", "Bearer " + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials(lambda _, func: func([auth_header], None), name="google_creds") composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def _make_stub(client, stub_factory, host, port): """Makes a stub for an RPC service. Uses / depends on the beta implementation of gRPC. :type client: :class:`.client.Client` :param client: The client that owns the cluster. Provides authorization and user agent. :type stub_factory: callable :param stub_factory: A factory which will create a gRPC stub for a given service. :type host: str :param host: The host for the service. :type port: int :param port: The port for the service. :rtype: :class:`grpc.beta._stub._AutoIntermediary` :returns: The stub object used to make gRPC requests to a given API. """ # Leaving the first argument to ssl_channel_credentials() as None # loads root certificates from `grpc/_adapter/credentials/roots.pem`. transport_creds = implementations.ssl_channel_credentials(None, None, None) custom_metadata_plugin = _MetadataPlugin(client) auth_creds = implementations.metadata_call_credentials( custom_metadata_plugin, name='google_creds') channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) channel = implementations.secure_channel(host, port, channel_creds) return stub_factory(channel)
def create_trace_stub(host=TRACE_ENDPOINT, port=SSL_PORT): """Creates a secure channel.""" ssl_creds = implementations.ssl_channel_credentials(None, None, None) call_creds = implementations.metadata_call_credentials(make_auth_func()) channel_creds = implementations.composite_channel_credentials(ssl_creds, call_creds) channel = implementations.secure_channel(host, port, channel_creds) return trace_pb2.beta_create_TraceService_stub(channel)
def _make_stub(client, stub_factory, host, port): """Makes a stub for an RPC service. Uses / depends on the beta implementation of gRPC. :type client: :class:`.client.Client` :param client: The client that owns the instance. Provides authorization and user agent. :type stub_factory: callable :param stub_factory: A factory which will create a gRPC stub for a given service. :type host: str :param host: The host for the service. :type port: int :param port: The port for the service. :rtype: :class:`grpc.beta._stub._AutoIntermediary` :returns: The stub object used to make gRPC requests to a given API. """ # Leaving the first argument to ssl_channel_credentials() as None # loads root certificates from `grpc/_adapter/credentials/roots.pem`. transport_creds = implementations.ssl_channel_credentials(None, None, None) custom_metadata_plugin = _MetadataPlugin(client) auth_creds = implementations.metadata_call_credentials( custom_metadata_plugin, name='google_creds') channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) channel = implementations.secure_channel(host, port, channel_creds) return stub_factory(channel)
def channel(self): ssl_channel = implementations.ssl_channel_credentials(None, None, None) creds = get_credentials().create_scoped([SPEECH_SCOPE]) auth_header = ('Authorization', 'Bearer ' + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials( lambda _, cb: cb([auth_header], None), name='google_creds') composite_channel = implementations.composite_channel_credentials( ssl_channel, auth_plugin) return implementations.secure_channel(SPEECH_API_HOST, SPEECH_API_PORT, composite_channel)
def make_channel(host, port): """Creates an SSL channel with auth credentials from the environment.""" # In order to make an https call, use an ssl channel with defaults ssl_channel = implementations.ssl_channel_credentials(None, None, None) # Grab application default credentials from the environment creds = get_credentials().create_scoped([SPEECH_SCOPE]) # Add a plugin to inject the creds into the header auth_header = ("Authorization", "Bearer " + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials(lambda _, cb: cb([auth_header], None), name="google_creds") # compose the two together for both ssl and google auth composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def run(): serverAddr = 'open.didiyunapi.com:8080' transport_creds = implementations.ssl_channel_credentials() auth_creds = implementations.metadata_call_credentials( oauth2token_credentials) channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) with grpc.secure_channel(serverAddr, channel_creds) as channel: dc2_stub = dc2_pb2_grpc.Dc2Stub(channel) try: response = dc2_stub.ListImage( dc2_pb2.ListImageRequest(header=base_pb2.Header( regionId='gz'))) print(response.error) print(response.data) except Exception as e: print("except: ", e)
def setUp(self): self._servicer = _Servicer() method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server = implementations.server(method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials([ ( resources.private_key(), resources.certificate_chain(), ), ]) port = self._server.add_secure_port('[::]:0', server_credentials) self._server.start() self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._call_credentials = implementations.metadata_call_credentials( _metadata_plugin) channel = test_utilities.not_really_secure_channel( 'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE) self._dynamic_stub = implementations.dynamic_stub(channel, _GROUP, cardinalities, options=stub_options)
def make_channel(host, port): """Creates an SSL channel with auth credentials from the environment.""" # In order to make an https call, use an ssl channel with defaults ssl_channel = implementations.ssl_channel_credentials(None, None, None) # Grab application default credentials from the environment creds = get_credentials().create_scoped([SPEECH_SCOPE]) # Add a plugin to inject the creds into the header auth_header = ('Authorization', 'Bearer ' + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials( lambda _, cb: cb([auth_header], None), name='google_creds') # compose the two together for both ssl and google auth composite_channel = implementations.composite_channel_credentials( ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def setUp(self): self._servicer = _Servicer() method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server = implementations.server( method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials([ ( resources.private_key(), resources.certificate_chain(), ), ]) port = self._server.add_secure_port('[::]:0', server_credentials) self._server.start() self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._call_credentials = implementations.metadata_call_credentials( _metadata_plugin) channel = test_utilities.not_really_secure_channel( 'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE) self._dynamic_stub = implementations.dynamic_stub( channel, _GROUP, cardinalities, options=stub_options)
def __init__(self, oauth2_token='Your-Token', addr='open.didiyunapi.com:8080'): def oauth2token_credentials(context, callback): callback([('authorization', 'Bearer %s' % oauth2_token)], None) transport_creds = implementations.ssl_channel_credentials() auth_creds = implementations.metadata_call_credentials( oauth2token_credentials) channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) self.channel = grpc.secure_channel(addr, channel_creds) self.commonStub = common_pb2_grpc.CommonStub(self.channel) self.billStub = bill_pb2_grpc.BillStub(self.channel) self.dc2Stub = dc2_pb2_grpc.Dc2Stub(self.channel) self.eipStub = eip_pb2_grpc.EipStub(self.channel) self.ebsStub = ebs_pb2_grpc.EbsStub(self.channel) self.sgStub = sg_pb2_grpc.SgStub(self.channel) self.snapStub = snap_pb2_grpc.SnapStub(self.channel) self.vpcStub = vpc_pb2_grpc.VpcStub(self.channel)
def google_grpc_channel(self, host, port): """Creates an SSL channel with auth credentials from the environment.""" # In order to make an https call, use an ssl channel with defaults ssl_channel = implementations.ssl_channel_credentials(None, None, None) # Grab application default credentials from the environment creds = GoogleCredentials.from_stream(os.path.join(app_dir,"audio_creds.json")).create_scoped([SPEECH_SCOPE]) # Add a plugin to inject the creds into the header auth_header = ( 'Authorization', 'Bearer ' + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials( lambda _, cb: cb([auth_header], None), name='google_creds') # compose the two together for both ssl and google auth composite_channel = implementations.composite_channel_credentials( ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def _make_channel_creds(auth_func, ssl_creds): """Converts the auth func into the composite creds expected by grpc.""" grpc_auth_func = _make_grpc_auth_func(auth_func) call_creds = implementations.metadata_call_credentials(grpc_auth_func) return implementations.composite_channel_credentials(ssl_creds, call_creds)
def make_channel_creds(ssl_creds, auth_func=auth_func): """Returns a channel with credentials callback.""" call_creds = implementations.metadata_call_credentials( lambda ctx, callback: callback(auth_func(), None)) return implementations.composite_channel_credentials(ssl_creds, call_creds)