{"id":3123,"date":"2024-07-18T09:49:45","date_gmt":"2024-07-18T16:49:45","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sdk\/?p=3123"},"modified":"2024-07-18T09:49:45","modified_gmt":"2024-07-18T16:49:45","slug":"announcing-stable-event-grid-namespaces-http-libraries","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sdk\/announcing-stable-event-grid-namespaces-http-libraries\/","title":{"rendered":"Announcing the stable release of Azure Event Grid Namespaces HTTP client libraries"},"content":{"rendered":"<p>We&#8217;re thrilled to announce the first stable release of the HTTP libraries for the Namespaces feature in Azure Event Grid, now available for .NET, Java, JavaScript, Python, and Go. This release marks a significant milestone in our journey to provide robust, reliable, and easy-to-use tools for developers building event-driven applications on Azure.<\/p>\n<p>Azure Event Grid is a highly scalable, fully managed service that facilitates Pub\/Sub message distribution using MQTT and HTTP protocols. Azure Event Grid Namespaces, a feature of the Standard tier of Event Grid, enhances this experience by allowing you to logically group related events and manage them under a single umbrella. This functionality is useful for complex applications handling a large number of events from diverse sources. With namespaces, you can apply consistent policies, security settings, and routing rules, making it easier to manage and scale your event-driven solutions.<\/p>\n<p>The stable release introduces client libraries that bring the power of Event Grid Namespaces to your favorite programming languages. These libraries are designed to be user-friendly, high-performing, and reliable. These libraries come with comprehensive documentation and code samples to help you get started quickly. These client libraries offer a streamlined API that simplifies the process of sending and receiving messages within your applications.<\/p>\n<p>These client libraries greatly enhance your ability to build and manage event-driven applications on Azure. In the following sections, we provide detailed instructions on how to get started with these libraries, including installation, initialization, and usage examples. Let&#8217;s dive in!<\/p>\n<h2>Language-specific packages<\/h2>\n<table>\n<thead>\n<tr>\n<th>Language<\/th>\n<th>Package and version<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>.NET<\/td>\n<td><a href=\"https:\/\/www.nuget.org\/packages\/Azure.Messaging.EventGrid.Namespaces\/1.0.0\"><code>Azure.Messaging.EventGrid.Namespaces<\/code> v1.0.0<\/a><\/td>\n<\/tr>\n<tr>\n<td>Go<\/td>\n<td><a href=\"https:\/\/pkg.go.dev\/github.com\/Azure\/azure-sdk-for-go\/sdk\/messaging\/eventgrid\/aznamespaces@v1.0.0\"><code>aznamespaces<\/code> v1.0.0<\/a><\/td>\n<\/tr>\n<tr>\n<td>Java<\/td>\n<td><a href=\"https:\/\/central.sonatype.com\/artifact\/com.azure\/azure-messaging-eventgrid-namespaces\/1.0.0\"><code>azure-messaging-eventgrid-namespaces<\/code> v1.0.0<\/a><\/td>\n<\/tr>\n<tr>\n<td>JavaScript<\/td>\n<td><a href=\"https:\/\/www.npmjs.com\/package\/@azure\/eventgrid-namespaces\/v\/1.0.0\"><code>@azure\/eventgrid-namespaces<\/code> v1.0.0<\/a><\/td>\n<\/tr>\n<tr>\n<td>Python<\/td>\n<td><a href=\"https:\/\/pypi.org\/project\/azure-eventgrid\/4.20.0\/\"><code>azure-eventgrid<\/code> v4.20.0<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote><p>TIP: The Python SDK didn&#8217;t create a new package for Event Grid Namespaces. Instead, this functionality is included in the existing Event Grid library.<\/p>\n<ul>\n<li>The <code>EventGridPublisherClient<\/code> creates an Event Grid Namespace Publisher client when specifying the <code>namespace_topic<\/code> parameter.\n<ul>\n<li><code>client = EventGridClient(endpoint, credential, namespace_topic=&lt;your-topic-name&gt;)<\/code><\/li>\n<\/ul>\n<\/li>\n<li>The <code>EventGridConsumerClient<\/code> is a new client only available for Event Grid Namespace.<\/li>\n<\/ul>\n<\/blockquote>\n<h2>Get started with .NET<\/h2>\n<h3>Create an Event Grid Namespace resource on Azure<\/h3>\n<ul>\n<li>You need an Azure subscription to use Azure Event Grid Namespaces. If you don&#8217;t have an existing Azure account, you may sign up for a free trial or use your Visual Studio subscriber benefits when you <a href=\"https:\/\/azure.microsoft.com\/free\/\">create an account<\/a>.<\/li>\n<li>You can follow these <a href=\"https:\/\/learn.microsoft.com\/azure\/event-grid\/create-view-manage-namespaces\">instructions<\/a> to create an Event Grid Namespace resource on Azure.<\/li>\n<\/ul>\n<h3>Installation<\/h3>\n<p>To install the library, run the following command using the .NET CLI:<\/p>\n<pre><code class=\"language-bash\">  dotnet add package Azure.Messaging.EventGrid.Namespaces --version 1.0.0<\/code><\/pre>\n<h3>Send events<\/h3>\n<pre><code class=\"language-csharp\">using Azure;\r\nusing Azure.Messaging;\r\nusing Azure.Messaging.EventGrid.Namespaces;\r\n\r\n\/* Construct the client using an Endpoint for a namespace \r\nas well as the shared access key*\/\r\nvar senderClient = new EventGridSenderClient(\r\n  new Uri(namespaceTopicHost), \r\n  topicName, \r\n  new AzureKeyCredential(namespaceKey));\r\n\r\nawait senderClient.SendAsync(\r\n    new[] {\r\n        new CloudEvent(\r\n          \"employee_source\", \r\n          \"type\", \r\n          new DataModel { Name = \"Tom\", Age = 55 }),\r\n        new CloudEvent(\r\n          \"employee_source\", \r\n          \"type\", \r\n          new DataModel { Name = \"Alice\", Age = 25 })\r\n    });<\/code><\/pre>\n<h3>Receive &amp; handle events<\/h3>\n<pre><code class=\"language-csharp\">using Azure;\r\nusing Azure.Messaging;\r\nusing Azure.Messaging.EventGrid.Namespaces;\r\n\r\n\/\/ Construct the client using an Endpoint for a namespace as well as the shared access key\r\nvar receiverClient = new EventGridReceiverClient(\r\n  new Uri(namespaceTopicHost), \r\n  topicName, \r\n  subscriptionName, \r\n  new AzureKeyCredential(namespaceKey));\r\n\r\nReceiveResult result = await receiverClient.ReceiveAsync(maxEvents: 3);\r\n\r\n\/\/ Iterate through the results and collect the lock tokens for events we want to release\/acknowledge\/result\r\nvar toRelease = new List&lt;string&gt;();\r\nvar toAcknowledge = new List&lt;string&gt;();\r\nvar toReject = new List&lt;string&gt;();\r\nforeach (ReceiveDetails detail in result.Details)\r\n{\r\n    CloudEvent @event = detail.Event;\r\n    BrokerProperties brokerProperties = detail.BrokerProperties;\r\n    Console.WriteLine(@event.Data.ToString());\r\n\r\n    \/\/ The lock token is used to acknowledge, reject or release the event\r\n    Console.WriteLine(brokerProperties.LockToken);\r\n\r\n    var data = @event.Data.ToObjectFromJson&lt;DataModel&gt;();\r\n    \/\/ If the data from the event has Name \"Bob\", we are not able to acknowledge it yet,\r\n    \/\/ so we release it, thereby allowing other consumers to receive it.\r\n    if (data.Name == \"Bob\")\r\n    {\r\n        toRelease.Add(brokerProperties.LockToken);\r\n    }\r\n    \/\/ If the data is for \"Tom\", we will acknowledge it thereby deleting it from the subscription.\r\n    else if (data.Name == \"Tom\")\r\n    {\r\n        toAcknowledge.Add(brokerProperties.LockToken);\r\n    }\r\n    \/\/ reject all other events which will move the event to the dead letter queue if it is configured\r\n    else\r\n    {\r\n        toReject.Add(brokerProperties.LockToken);\r\n    }\r\n}\r\n\r\nif (toRelease.Count &gt; 0)\r\n{\r\n    ReleaseResult releaseResult = await receiverClient.ReleaseAsync(toRelease);\r\n\r\n    \/\/ Inspect the Release result\r\n    Console.WriteLine($\"Failed count for Release: {releaseResult.FailedLockTokens.Count}\");\r\n    foreach (FailedLockToken failedLockToken in releaseResult.FailedLockTokens)\r\n    {\r\n        Console.WriteLine($\"Lock Token: {failedLockToken.LockToken}\");\r\n        Console.WriteLine($\"Error Code: {failedLockToken.Error.Code}\");\r\n        Console.WriteLine($\"Error Description: {failedLockToken.Error.Message}\");\r\n    }\r\n\r\n    Console.WriteLine($\"Success count for Release: {releaseResult.SucceededLockTokens.Count}\");\r\n    foreach (string lockToken in releaseResult.SucceededLockTokens)\r\n    {\r\n        Console.WriteLine($\"Lock Token: {lockToken}\");\r\n    }\r\n}\r\n\r\nif (toAcknowledge.Count &gt; 0)\r\n{\r\n    AcknowledgeResult acknowledgeResult = await receiverClient.AcknowledgeAsync(toAcknowledge);\r\n\r\n    \/\/ Inspect the Acknowledge result\r\n    Console.WriteLine($\"Failed count for Acknowledge: {acknowledgeResult.FailedLockTokens.Count}\");\r\n    foreach (FailedLockToken failedLockToken in acknowledgeResult.FailedLockTokens)\r\n    {\r\n        Console.WriteLine($\"Lock Token: {failedLockToken.LockToken}\");\r\n        Console.WriteLine($\"Error Code: {failedLockToken.Error.Code}\");\r\n        Console.WriteLine($\"Error Description: {failedLockToken.Error.Message}\");\r\n    }\r\n\r\n    Console.WriteLine($\"Success count for Acknowledge: {acknowledgeResult.SucceededLockTokens.Count}\");\r\n    foreach (string lockToken in acknowledgeResult.SucceededLockTokens)\r\n    {\r\n        Console.WriteLine($\"Lock Token: {lockToken}\");\r\n    }\r\n}\r\n\r\nif (toReject.Count &gt; 0)\r\n{\r\n    RejectResult rejectResult = await receiverClient.RejectAsync(toReject);\r\n\r\n    \/\/ Inspect the Reject result\r\n    Console.WriteLine($\"Failed count for Reject: {rejectResult.FailedLockTokens.Count}\");\r\n    foreach (FailedLockToken failedLockToken in rejectResult.FailedLockTokens)\r\n    {\r\n        Console.WriteLine($\"Lock Token: {failedLockToken.LockToken}\");\r\n        Console.WriteLine($\"Error Code: {failedLockToken.Error.Code}\");\r\n        Console.WriteLine($\"Error Description: {failedLockToken.Error.Message}\");\r\n    }\r\n\r\n    Console.WriteLine($\"Success count for Reject: {rejectResult.SucceededLockTokens.Count}\");\r\n    foreach (string lockToken in rejectResult.SucceededLockTokens)\r\n    {\r\n        Console.WriteLine($\"Lock Token: {lockToken}\");\r\n    }\r\n}<\/code><\/pre>\n<h3>Create DataModel class<\/h3>\n<p>Add the following code to the end of your file if you&#8217;d like to use the <code>DataModel<\/code> shown in the code samples.<\/p>\n<pre><code class=\"language-csharp\">public class DataModel\r\n{\r\n    public string Name { get; set; }\r\n    public int Age { get; set; }\r\n}<\/code><\/pre>\n<h2>More language samples<\/h2>\n<p>For samples and getting started guides, select a language-specific link:<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-net\/tree\/main\/sdk\/eventgrid\/Azure.Messaging.EventGrid.Namespaces\">.NET<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-go\/tree\/main\/sdk\/messaging\/eventgrid\/aznamespaces\">Go<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-java\/tree\/main\/sdk\/eventgrid\/azure-messaging-eventgrid-namespaces\">Java<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-js\/tree\/main\/sdk\/eventgrid\/eventgrid-namespaces\">JavaScript<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-python\/tree\/main\/sdk\/eventgrid\/azure-eventgrid\">Python<\/a><\/li>\n<\/ul>\n<h2>Support channels<\/h2>\n<p>You can share feedback, make feature requests, ask questions, or report bugs through issues on any of our GitHub repositories.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post announces stable release of the HTTP Azure Event Grid Namespaces client libraries in .NET, Java, JavaScript, Python, and Go.<\/p>\n","protected":false},"author":104000,"featured_media":3125,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[701,750,748,810,160,159,162,733],"class_list":["post-3123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sdk","tag-net","tag-azure-sdk","tag-event-grid","tag-go","tag-java","tag-javascript","tag-python","tag-typescript"],"acf":[],"blog_post_summary":"<p>This post announces stable release of the HTTP Azure Event Grid Namespaces client libraries in .NET, Java, JavaScript, Python, and Go.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/3123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/users\/104000"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/comments?post=3123"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/3123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media\/3125"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media?parent=3123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/categories?post=3123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/tags?post=3123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}