def test_update_topic(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = pubsub_pb2.Topic(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = publisher_client.PublisherClient() # Setup Request topic = {} update_mask = {} response = client.update_topic(topic, update_mask) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.UpdateTopicRequest( topic=topic, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_topic(self): # Setup Expected Response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = pubsub_pb2.Topic(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = publisher_client.PublisherClient(channel=channel) # Setup Request name = client.topic_path('[PROJECT]', '[TOPIC]') response = client.create_topic(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.Topic(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_topic(self): # Setup Expected Response name_2 = "name2-1052831874" expected_response = {"name": name_2} expected_response = pubsub_pb2.Topic(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = publisher_client.PublisherClient() # Setup Request name = client.topic_path("[PROJECT]", "[TOPIC]") response = client.create_topic(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.Topic(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_topic(self, name, labels=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT): """ Creates the given topic with the given name. Example: >>> from google.cloud import pubsub_v1 >>> >>> client = pubsub_v1.PublisherClient() >>> >>> name = client.topic_path('[PROJECT]', '[TOPIC]') >>> >>> response = client.create_topic(name) Args: name (str): The name of the topic. It must have the format ``\"projects/{project}/topics/{topic}\"``. ``{topic}`` must start with a letter, and contain only letters (``[A-Za-z]``), numbers (``[0-9]``), dashes (``-``), underscores (``_``), periods (``.``), tildes (``~``), plus (``+``) or percent signs (``%``). It must be between 3 and 255 characters in length, and it must not start with ``\"goog\"``. labels (dict[str -> str]): User labels. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. Returns: A :class:`~google.cloud.pubsub_v1.types.Topic` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ request = pubsub_pb2.Topic(name=name, labels=labels) return self._create_topic(request, retry=retry, timeout=timeout)
def test_get_topic(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = pubsub_pb2.Topic(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = publisher_client.PublisherClient(channel=channel) # Setup Request topic = client.topic_path('[PROJECT]', '[TOPIC]') response = client.get_topic(topic) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.GetTopicRequest(topic=topic) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_topic(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = pubsub_pb2.Topic(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = publisher_client.PublisherClient(channel=channel) # Setup Request topic = {} update_mask = {} response = client.update_topic(topic, update_mask) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.UpdateTopicRequest( topic=topic, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_topic(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = pubsub_pb2.Topic(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = publisher_client.PublisherClient() # Setup Request topic = client.topic_path('[PROJECT]', '[TOPIC]') response = client.get_topic(topic) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.GetTopicRequest(topic=topic) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_topic(self): # Setup Expected Response name = "name3373707" kms_key_name = "kmsKeyName2094986649" expected_response = {"name": name, "kms_key_name": kms_key_name} expected_response = pubsub_pb2.Topic(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = publisher_client.PublisherClient() # Setup Request topic = client.topic_path("[PROJECT]", "[TOPIC]") response = client.get_topic(topic) assert expected_response == response assert len(channel.requests) == 1 expected_request = pubsub_pb2.GetTopicRequest(topic=topic) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_topic(self, name, labels=None, message_storage_policy=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None): """ Creates the given topic with the given name. See the resource name rules. Example: >>> from google.cloud import pubsub_v1 >>> >>> client = pubsub_v1.PublisherClient() >>> >>> name = client.topic_path('[PROJECT]', '[TOPIC]') >>> >>> response = client.create_topic(name) Args: name (str): The name of the topic. It must have the format `"projects/{project}/topics/{topic}"`. `{topic}` must start with a letter, and contain only letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`), underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent signs (`%`). It must be between 3 and 255 characters in length, and it must not start with `"goog"`. labels (dict[str -> str]): See <a href="/pubsub/docs/labels"> Creating and managing labels</a>. message_storage_policy (Union[dict, ~google.cloud.pubsub_v1.types.MessageStoragePolicy]): Policy constraining how messages published to the topic may be stored. It is determined when the topic is created based on the policy configured at the project level. It must not be set by the caller in the request to CreateTopic or to UpdateTopic. This field will be populated in the responses for GetTopic, CreateTopic, and UpdateTopic: if not present in the response, then no constraints are in effect. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.pubsub_v1.types.MessageStoragePolicy` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.pubsub_v1.types.Topic` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if 'create_topic' not in self._inner_api_calls: self._inner_api_calls[ 'create_topic'] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_topic, default_retry=self._method_configs['CreateTopic'].retry, default_timeout=self._method_configs['CreateTopic']. timeout, client_info=self._client_info, ) request = pubsub_pb2.Topic( name=name, labels=labels, message_storage_policy=message_storage_policy, ) return self._inner_api_calls['create_topic'](request, retry=retry, timeout=timeout, metadata=metadata)
def create_topic( self, name, labels=None, message_storage_policy=None, kms_key_name=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Creates the given topic with the given name. See the resource name rules. Example: >>> from google.cloud import pubsub_v1 >>> >>> client = pubsub_v1.PublisherClient() >>> >>> name = client.topic_path('[PROJECT]', '[TOPIC]') >>> >>> response = client.create_topic(name) Args: name (str): The name of the topic. It must have the format `"projects/{project}/topics/{topic}"`. `{topic}` must start with a letter, and contain only letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`), underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent signs (`%`). It must be between 3 and 255 characters in length, and it must not start with `"goog"`. labels (dict[str -> str]): See <a href="https://cloud.google.com/pubsub/docs/labels"> Creating and managing labels</a>. message_storage_policy (Union[dict, ~google.cloud.pubsub_v1.types.MessageStoragePolicy]): Policy constraining how messages published to the topic may be stored. It is determined when the topic is created based on the policy configured at the project level. It must not be set by the caller in the request to CreateTopic or to UpdateTopic. This field will be populated in the responses for GetTopic, CreateTopic, and UpdateTopic: if not present in the response, then no constraints are in effect. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.pubsub_v1.types.MessageStoragePolicy` kms_key_name (str): The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. The expected format is ``projects/*/locations/*/keyRings/*/cryptoKeys/*``. EXPERIMENTAL: This feature is part of a closed alpha release. This API might be changed in backward-incompatible ways and is not recommended for production use. It is not subject to any SLA or deprecation policy. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.pubsub_v1.types.Topic` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "create_topic" not in self._inner_api_calls: self._inner_api_calls[ "create_topic"] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_topic, default_retry=self._method_configs["CreateTopic"].retry, default_timeout=self._method_configs["CreateTopic"]. timeout, client_info=self._client_info, ) request = pubsub_pb2.Topic( name=name, labels=labels, message_storage_policy=message_storage_policy, kms_key_name=kms_key_name, ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("name", name)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header) metadata.append(routing_metadata) return self._inner_api_calls["create_topic"](request, retry=retry, timeout=timeout, metadata=metadata)