Exemplo n.º 1
0
    def __init__(self, **kwargs):
        # Sanity check: Is our goal to use the emulator?
        # If so, create a grpc insecure channel with the emulator host
        # as the target.
        if os.environ.get("PUBSUB_EMULATOR_HOST"):
            kwargs["channel"] = grpc.insecure_channel(
                target=os.environ.get("PUBSUB_EMULATOR_HOST"))
            kwargs["credentials"] = AnonymousCredentials()

        # The GAPIC client has mTLS logic to determine the api endpoint and the
        # ssl credentials to use. Here we create a GAPIC client to help compute the
        # api endpoint and ssl credentials. The api endpoint will be used to set
        # `self._target`, and ssl credentials will be passed to
        # `grpc_helpers.create_channel` to establish a mTLS channel (if ssl
        # credentials is not None).
        client_options = kwargs.get("client_options", None)
        credentials = kwargs.get("credentials", None)
        client_for_mtls_info = subscriber_client.SubscriberClient(
            credentials=credentials, client_options=client_options)

        self._target = client_for_mtls_info._transport._host

        # Use a custom channel.
        # We need this in order to set appropriate default message size and
        # keepalive options.
        if "transport" not in kwargs:
            channel = kwargs.pop("channel", None)
            if channel is None:
                channel = grpc_helpers.create_channel(
                    credentials=kwargs.pop("credentials", None),
                    target=self.target,
                    ssl_credentials=client_for_mtls_info._transport.
                    _ssl_channel_credentials,
                    scopes=subscriber_client.SubscriberClient._DEFAULT_SCOPES,
                    options={
                        "grpc.max_send_message_length": -1,
                        "grpc.max_receive_message_length": -1,
                        "grpc.keepalive_time_ms": 30000,
                    }.items(),
                )
            # cannot pass both 'channel' and 'credentials'
            kwargs.pop("credentials", None)
            transport = subscriber_grpc_transport.SubscriberGrpcTransport(
                channel=channel)
            kwargs["transport"] = transport

        # Add the metrics headers, and instantiate the underlying GAPIC
        # client.
        self._api = subscriber_client.SubscriberClient(**kwargs)
Exemplo n.º 2
0
    def __init__(self, **kwargs):
        # Sanity check: Is our goal to use the emulator?
        # If so, create a grpc insecure channel with the emulator host
        # as the target.
        if os.environ.get("PUBSUB_EMULATOR_HOST"):
            kwargs["channel"] = grpc.insecure_channel(
                target=os.environ.get("PUBSUB_EMULATOR_HOST"))

        # api_endpoint wont be applied if 'transport' is passed in.
        client_options = kwargs.pop("client_options", None)
        if (client_options and "api_endpoint" in client_options and isinstance(
                client_options["api_endpoint"], six.string_types)):
            self._target = client_options["api_endpoint"]
        else:
            self._target = subscriber_client.SubscriberClient.SERVICE_ADDRESS

        # Use a custom channel.
        # We need this in order to set appropriate default message size and
        # keepalive options.
        if "transport" not in kwargs:
            channel = kwargs.pop("channel", None)
            if channel is None:
                channel = grpc_helpers.create_channel(
                    credentials=kwargs.pop("credentials", None),
                    target=self.target,
                    scopes=subscriber_client.SubscriberClient._DEFAULT_SCOPES,
                    options={
                        "grpc.max_send_message_length": -1,
                        "grpc.max_receive_message_length": -1,
                        "grpc.keepalive_time_ms": 30000,
                    }.items(),
                )
            # cannot pass both 'channel' and 'credentials'
            kwargs.pop("credentials", None)
            transport = subscriber_grpc_transport.SubscriberGrpcTransport(
                channel=channel)
            kwargs["transport"] = transport

        # Add the metrics headers, and instantiate the underlying GAPIC
        # client.
        self._api = subscriber_client.SubscriberClient(**kwargs)