public void CallSettings_Client() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Sample: CallSettings_ClientWide // Create a default PublisherSettings, with a custom header for calls to all RPC methods. PublisherSettings publisherSettings = new PublisherSettings { CallSettings = new CallSettings { Headers = new Metadata { { "ClientVersion", "1.0.0" } }, } }; PublisherClient client = PublisherClient.Create(settings: publisherSettings); // Format topicName from the projectId and topicId. string topicName = PublisherClient.FormatTopicName(projectId, topicId); // The custom 'ClientVersion' header will be included in the RPC call, due to // the client being configured with 'publishersettings' above. Topic topic = client.CreateTopic(topicName); // End sample }
public async Task PublishAsync() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Snippet: PublishAsync(*,*,CallSettings) // Additional: PublishAsync(*,*,CancellationToken) PublisherClient client = PublisherClient.Create(); // Make sure we have a topic to publish to string topicName = PublisherClient.FormatTopicName(projectId, topicId); await client.CreateTopicAsync(topicName); PubsubMessage message = new PubsubMessage { // The data is any arbitrary ByteString. Here, we're using text. Data = ByteString.CopyFromUtf8("Hello, Pubsub"), // The attributes provide metadata in a string-to-string dictionary. Attributes = { { "description", "Simple text message" } } }; await client.PublishAsync(topicName, new[] { message }); // End snippet }
public void ListTopicSubscriptions() { // Snippet: ListTopicSubscriptions(string,string,int?,CallSettings) // Create client PublisherClient publisherClient = PublisherClient.Create(); // Initialize request argument(s) string formattedTopic = PublisherClient.FormatTopicName("[PROJECT]", "[TOPIC]"); // Make the request IPagedEnumerable <ListTopicSubscriptionsResponse, string> response = publisherClient.ListTopicSubscriptions(formattedTopic); // Iterate over all response items, lazily performing RPCs as required foreach (string item in response) { // Do something with each item Console.WriteLine(item); } // Or iterate over fixed-sized pages, lazily performing RPCs as required int pageSize = 10; foreach (FixedSizePage <string> page in response.AsPages().WithFixedSize(pageSize)) { // Do something with each page of items Console.WriteLine("A page of results:"); foreach (string item in page) { Console.WriteLine(item); } } // End snippet }
public Topic GetTopic(string topicId, PublisherClient publisher) { string topicName = PublisherClient.FormatTopicName(_projectId, topicId); Topic topic = _publisher.GetTopic(topicName); return(topic); }
public void DeleteTopic(string topicId, PublisherClient publisher) { // [START delete_topic] string topicName = PublisherClient.FormatTopicName(_projectId, topicId); publisher.DeleteTopic(topicName); // [END delete_topic] }
public void CreateTopic(string topicId, PublisherClient publisher) { // [START create_topic] string topicName = PublisherClient.FormatTopicName(_projectId, topicId); publisher.CreateTopic(topicName); // [END create_topic] }
public void Overview() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); string subscriptionId = _fixture.CreateSubscriptionId(); // Sample: Overview // First create a topic. PublisherClient publisher = PublisherClient.Create(); string topicName = PublisherClient.FormatTopicName(projectId, topicId); publisher.CreateTopic(topicName); // Subscribe to the topic. SubscriberClient subscriber = SubscriberClient.Create(); string subscriptionName = SubscriberClient.FormatSubscriptionName(projectId, subscriptionId); subscriber.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60); // Publish a message to the topic. PubsubMessage message = new PubsubMessage { // The data is any arbitrary ByteString. Here, we're using text. Data = ByteString.CopyFromUtf8("Hello, Pubsub"), // The attributes provide metadata in a string-to-string dictionary. Attributes = { { "description", "Simple text message" } } }; publisher.Publish(topicName, new[] { message }); // Pull messages from the subscription. We're returning immediately, whether or not there // are messages; in other cases you'll want to allow the call to wait until a message arrives. PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 10); foreach (ReceivedMessage received in response.ReceivedMessages) { PubsubMessage msg = received.Message; Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}"); Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'"); } // Acknowledge that we've received the messages. If we don't do this within 60 seconds (as specified // when we created the subscription) we'll receive the messages again when we next pull. subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId)); // Tidy up by deleting the subscription and the topic. subscriber.DeleteSubscription(subscriptionName); publisher.DeleteTopic(topicName); // End sample Assert.Equal(1, response.ReceivedMessages.Count); Assert.Equal("Hello, Pubsub", response.ReceivedMessages[0].Message.Data.ToStringUtf8()); Assert.Equal("Simple text message", response.ReceivedMessages[0].Message.Attributes["description"]); }
public void TestDeleteTopic() { string topicId = "testTopicForDeleteTopic"; string topicName = PublisherClient.FormatTopicName(_projectId, topicId); CreateTopic(topicId, _publisher); DeleteTopic(topicId, _publisher); Exception ex = Assert.Throws <Grpc.Core.RpcException>(() => _publisher.GetTopic(topicName)); }
// [START retry] public void RpcRetry(string topicId, string subscriptionId, PublisherClient publisher, SubscriberClient subscriber) { string topicName = PublisherClient.FormatTopicName(_projectId, topicId); string subscriptionName = SubscriberClient.FormatSubscriptionName(_projectId, subscriptionId); var delayMs = _retryDelayMs; for (int tries = 1; true; ++tries) { try { // Subscribe to Topic // This may fail if the Subscription already exists or // the Topic has not yet been created. subscriber.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60); break; } catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists) { break; } catch (RpcException) when(tries < _retryCount) { System.Threading.Thread.Sleep(delayMs); delayMs *= 2; // Exponential back-off. } catch (RpcException) when(tries == _retryCount) { //Number of retries set in _retryCount has been reached break; } } // Create Topic delayMs = _retryDelayMs; for (int tries = 1; true; ++tries) { try { publisher.CreateTopic(topicName); break; } catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists) { break; } catch (RpcException) when(tries < 3) { System.Threading.Thread.Sleep(delayMs); delayMs *= 2; // Exponential back-off. } } }
public void CreateTopic() { // Snippet: CreateTopic(string,CallSettings) // Create client PublisherClient publisherClient = PublisherClient.Create(); // Initialize request argument(s) string formattedName = PublisherClient.FormatTopicName("[PROJECT]", "[TOPIC]"); // Make the request Topic response = publisherClient.CreateTopic(formattedName); // End snippet }
public void TestListTopics() { string topicId = "testTopicForListingTopics"; string topicName = PublisherClient.FormatTopicName(_projectId, topicId); CreateTopic(topicId, _publisher); IEnumerable <Topic> topics = ListProjectTopics(_publisher); Assert.False(topics.Count() == 0); DeleteTopic(topicId, _publisher); }
public void GetIamPolicy() { // Snippet: GetIamPolicy(string,CallSettings) // Create client PublisherClient publisherClient = PublisherClient.Create(); // Initialize request argument(s) string formattedResource = PublisherClient.FormatTopicName("[PROJECT]", "[TOPIC]"); // Make the request Policy response = publisherClient.GetIamPolicy(formattedResource); // End snippet }
public void TestCreateTopic() { string topicId = "testTopicForTopicCreation"; CreateTopic(topicId, _publisher); string topicName = PublisherClient.FormatTopicName(_projectId, topicId); Topic topic = GetTopic(topicId, _publisher); Assert.Equal(topicName, topic.Name); DeleteTopic(topicId, _publisher); }
public void FormatResourceName() { // Sample: FormatResourceName string projectId = "petstore"; string topicId = "offers"; string topicName = PublisherClient.FormatTopicName(projectId, topicId); // The value of topicName is now "projects/petstore/topics/offers" // End sample Assert.Equal(topicName, "projects/petstore/topics/offers"); }
public void TestIamPermissions() { // Snippet: TestIamPermissions(string,IEnumerable<string>,CallSettings) // Create client PublisherClient publisherClient = PublisherClient.Create(); // Initialize request argument(s) string formattedResource = PublisherClient.FormatTopicName("[PROJECT]", "[TOPIC]"); IEnumerable <string> permissions = new List <string>(); // Make the request TestIamPermissionsResponse response = publisherClient.TestIamPermissions(formattedResource, permissions); // End snippet }
public void TestRpcRetry() { string topicId = "testTopicForRpcRetry"; string subscriptionId = "testSubscriptionForRpcRetry"; RpcRetry(topicId, subscriptionId, _publisher, _subscriber); string topicName = PublisherClient.FormatTopicName(_projectId, topicId); Topic topic = GetTopic(topicId, _publisher); Assert.Equal(topicName, topic.Name); DeleteTopic(topicId, _publisher); }
public async Task CreateTopicAsync() { // Snippet: CreateTopicAsync(string,CallSettings) // Additional: CreateTopicAsync(string,CancellationToken) // Create client PublisherClient publisherClient = PublisherClient.Create(); // Initialize request argument(s) string formattedName = PublisherClient.FormatTopicName("[PROJECT]", "[TOPIC]"); // Make the request Topic response = await publisherClient.CreateTopicAsync(formattedName); // End snippet }
public void CreateSubscription(string topicId, string subscriptionId, SubscriberClient subscriber) { // [START create_subscription] string topicName = PublisherClient.FormatTopicName(_projectId, topicId); string subscriptionName = SubscriberClient.FormatSubscriptionName(_projectId, subscriptionId); Subscription subscription = subscriber.CreateSubscription( subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60); // [END create_subscription] }
public async Task TestIamPermissionsAsync() { // Snippet: TestIamPermissionsAsync(string,IEnumerable<string>,CallSettings) // Additional: TestIamPermissionsAsync(string,IEnumerable<string>,CancellationToken) // Create client PublisherClient publisherClient = PublisherClient.Create(); // Initialize request argument(s) string formattedResource = PublisherClient.FormatTopicName("[PROJECT]", "[TOPIC]"); IEnumerable <string> permissions = new List <string>(); // Make the request TestIamPermissionsResponse response = await publisherClient.TestIamPermissionsAsync(formattedResource, permissions); // End snippet }
public async Task SetIamPolicyAsync() { // Snippet: SetIamPolicyAsync(string,Policy,CallSettings) // Additional: SetIamPolicyAsync(string,Policy,CancellationToken) // Create client PublisherClient publisherClient = PublisherClient.Create(); // Initialize request argument(s) string formattedResource = PublisherClient.FormatTopicName("[PROJECT]", "[TOPIC]"); Policy policy = new Policy(); // Make the request Policy response = await publisherClient.SetIamPolicyAsync(formattedResource, policy); // End snippet }
public void GetIamPolicy() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); PublisherClient.Create().CreateTopic(PublisherClient.FormatTopicName(projectId, topicId)); // Snippet: GetIamPolicy PublisherClient client = PublisherClient.Create(); string topicName = PublisherClient.FormatTopicName(projectId, topicId); Policy policy = client.GetIamPolicy(topicName); Console.WriteLine($"Policy for {topicName}: {policy}"); // End snippet }
public void CreateTopic() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Snippet: CreateTopic PublisherClient client = PublisherClient.Create(); // Alternative: use a known topic resource name // "projects/{PROJECT_ID}/topics/{TOPIC_ID}" string topicName = PublisherClient.FormatTopicName(projectId, topicId); Topic topic = client.CreateTopic(topicName); Console.WriteLine($"Created {topic.Name}"); // End snippet }
public void TestDeleteSubscription() { string topicId = "testTopicForDeleteSubscription"; string subscriptionId = "testSubscriptionForDeleteSubscription"; string topicName = PublisherClient.FormatTopicName(_projectId, topicId); string subscriptionName = SubscriberClient.FormatSubscriptionName(_projectId, subscriptionId); CreateTopic(topicId, _publisher); CreateSubscription(topicId, subscriptionId, _subscriber); DeleteSubscription(subscriptionId, _subscriber); Exception e = Assert.Throws <Grpc.Core.RpcException>(() => _subscriber.GetSubscription(subscriptionName)); DeleteTopic(topicId, _publisher); }
public void PerRpc() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Sample: PerRpc // Create a PublisherClient with default settings. PublisherClient client = PublisherClient.Create(); // Format topicName from the projectId and topicId. string topicName = PublisherClient.FormatTopicName(projectId, topicId); // Create a CallSettings with a custom header. CallSettings callSettings = new CallSettings(null, null, null, metadata => metadata.Add("ClientVersion", "1.0.0"), null, null); // This will cause the custom 'ClientVersion' header to be included in the RPC call. Topic topic = client.CreateTopic(topicName, callSettings); // End sample }
public async Task CreateTopicAsync() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Snippet: CreateTopicAsync(string,CallSettings) // Additional: CreateTopicAsync(string,CancellationToken) PublisherClient client = PublisherClient.Create(); // Alternative: use a known topic resource name // "projects/{PROJECT_ID}/topics/{TOPIC_ID}" string topicName = PublisherClient.FormatTopicName(projectId, topicId); Topic topic = await client.CreateTopicAsync(topicName); Console.WriteLine($"Created {topic.Name}"); // End snippet }
public void CallSettings_Overrides() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); DateTime deadline = DateTime.MaxValue; CancellationToken cancellationToken = new CancellationTokenSource().Token; // Sample: CallSettings_Overrides // Create a default PublisherSettings, with customizations for CreateTopic RPCs: // * A custom "ClientVersion" header. // * A custom 5-second timeout Timing. // * No cancellation token. PublisherSettings publisherSettings = new PublisherSettings(); publisherSettings.CreateTopicSettings.Headers = new Metadata { { "ClientVersion", "1.0.0" } }; publisherSettings.CreateTopicSettings.Timing = CallTiming.FromTimeout(TimeSpan.FromSeconds(5)); publisherSettings.CreateTopicSettings.CancellationToken = CancellationToken.None; // Override the above Timing and CancellationToken in the client-wide CallSettings; // the Headers are not overridden. publisherSettings.CallSettings = new CallSettings { Timing = CallTiming.FromDeadline(deadline), CancellationToken = CancellationToken.None, }; // Create the client with the configured publisherSettings PublisherClient client = PublisherClient.Create(settings: publisherSettings); // Format topicName from the projectId and topicId. string topicName = PublisherClient.FormatTopicName(projectId, topicId); // Call CreateTopic(). Override only the CancellationToken, using a per-RPC-method CallSettings. // The CallSettings used during this RPC invocation is: // * A custom "ClientVersion" header. // * A Timing deadline of 'deadline' (*not* the overridden 5-second timeout). // * The CancellationToken 'cancellationToken' (*not* CancellationToken.None). Topic topic = client.CreateTopic(topicName, new CallSettings { CancellationToken = cancellationToken }); // End sample }
public void TestListSubscriptions() { string topicId = "testTopicForListingSubscriptions"; string subscriptionId = "testSubscriptionForListingSubscriptions"; string topicName = PublisherClient.FormatTopicName(_projectId, topicId); string subscriptionName = SubscriberClient.FormatSubscriptionName(_projectId, subscriptionId); CreateTopic(topicId, _publisher); CreateSubscription(topicId, subscriptionId, _subscriber); IEnumerable <Subscription> subscriptions = ListSubscriptions( _subscriber); Assert.False(subscriptions.Count() == 0); DeleteSubscription(subscriptionId, _subscriber); DeleteTopic(topicId, _publisher); }
public void CreateTopic(string topicId, PublisherClient publisher) { // [START create_topic] string topicName = PublisherClient.FormatTopicName(_projectId, topicId); try { publisher.CreateTopic(topicName); } catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists) { // Already exists. That's fine. } // [END create_topic] }
public void Publish() { // Snippet: Publish(string,IEnumerable<PubsubMessage>,CallSettings) // Create client PublisherClient publisherClient = PublisherClient.Create(); // Initialize request argument(s) string formattedTopic = PublisherClient.FormatTopicName("[PROJECT]", "[TOPIC]"); IEnumerable <PubsubMessage> messages = new[] { new PubsubMessage { Data = ByteString.CopyFromUtf8(""), }, }; // Make the request PublishResponse response = publisherClient.Publish(formattedTopic, messages); // End snippet }
public static void Main_HelloWorldPubSub() { // Instantiates a client PublisherClient publisher = PublisherClient.Create(); // Your Google Cloud Platform project ID string projectId = "pushnotificationpoc-19baf"; // The name for the new topic string topicName = "my-new-topic"; // The fully qualified name for the new topic string formattedTopicName = PublisherClient.FormatTopicName(projectId, topicName); // Creates the new topic Topic topic = publisher.CreateTopic(formattedTopicName); Console.WriteLine($"Topic {topic.Name} created."); }