def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: PublishResponse with the response of the Publish operation. Raises: topics.EmptyMessageException: if neither message or attributes is specified. topics.PublishOperationException: When something went wrong with the publish operation. """ client = topics.TopicsClient() attributes = util.ParseAttributes(args.attribute, messages=client.messages) topic_ref = util.ParseTopic(args.topic) result = client.Publish(topic_ref, args.message_body, attributes) # We only allow to publish one message at a time, so do not return a # list of messageId. resource = resource_projector.MakeSerializable(result) resource['messageIds'] = result.messageIds[0] return resource
def _Run(args, message_body, legacy_output=False): """Publishes a message to a topic.""" client = topics.TopicsClient() attributes = util.ParseAttributes(args.attribute, messages=client.messages) topic_ref = args.CONCEPTS.topic.Parse() result = client.Publish(topic_ref, message_body, attributes) if legacy_output: # We only allow to publish one message at a time, so do not return a # list of messageId. result = resource_projector.MakeSerializable(result) result['messageIds'] = result['messageIds'][0] return result