< Summary

Class:Azure.Messaging.EventGrid.ServiceRestClient
Assembly:Azure.Messaging.EventGrid
File(s):C:\Git\azure-sdk-for-net\sdk\eventgrid\Azure.Messaging.EventGrid\src\Generated\ServiceRestClient.cs
Covered lines:99
Uncovered lines:19
Coverable lines:118
Total lines:273
Line coverage:83.8% (99 of 118)
Covered branches:25
Total branches:44
Branch coverage:56.8% (25 of 44)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-85.71%50%
CreatePublishEventsRequest(...)-100%100%
PublishEventsAsync()-70%50%
PublishEvents(...)-70%50%
CreatePublishCloudEventEventsRequest(...)-100%100%
PublishCloudEventEventsAsync()-70%50%
PublishCloudEventEvents(...)-70%50%
CreatePublishCustomEventEventsRequest(...)-100%100%
PublishCustomEventEventsAsync()-70%50%
PublishCustomEventEvents(...)-70%50%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventgrid\Azure.Messaging.EventGrid\src\Generated\ServiceRestClient.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4// <auto-generated/>
 5
 6#nullable disable
 7
 8using System;
 9using System.Collections.Generic;
 10using System.Threading;
 11using System.Threading.Tasks;
 12using Azure;
 13using Azure.Core;
 14using Azure.Core.Pipeline;
 15using Azure.Messaging.EventGrid.Models;
 16
 17namespace Azure.Messaging.EventGrid
 18{
 19    internal partial class ServiceRestClient
 20    {
 21        private string apiVersion;
 22        private ClientDiagnostics _clientDiagnostics;
 23        private HttpPipeline _pipeline;
 24
 25        /// <summary> Initializes a new instance of ServiceRestClient. </summary>
 26        /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param>
 27        /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param>
 28        /// <param name="apiVersion"> Api Version. </param>
 29        /// <exception cref="ArgumentNullException"> <paramref name="apiVersion"/> is null. </exception>
 4430        public ServiceRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string apiVersion = "2018-0
 31        {
 4432            if (apiVersion == null)
 33            {
 034                throw new ArgumentNullException(nameof(apiVersion));
 35            }
 36
 4437            this.apiVersion = apiVersion;
 4438            _clientDiagnostics = clientDiagnostics;
 4439            _pipeline = pipeline;
 4440        }
 41
 42        internal HttpMessage CreatePublishEventsRequest(string topicHostname, IEnumerable<EventGridEventInternal> events
 43        {
 1644            var message = _pipeline.CreateMessage();
 1645            var request = message.Request;
 1646            request.Method = RequestMethod.Post;
 1647            var uri = new RawRequestUriBuilder();
 1648            uri.AppendRaw("https://", false);
 1649            uri.AppendRaw(topicHostname, false);
 1650            uri.AppendPath("/api/events", false);
 1651            uri.AppendQuery("api-version", apiVersion, true);
 1652            request.Uri = uri;
 1653            request.Headers.Add("Content-Type", "application/json");
 1654            var content = new Utf8JsonRequestContent();
 1655            content.JsonWriter.WriteStartArray();
 35256            foreach (var item in events)
 57            {
 16058                content.JsonWriter.WriteObjectValue(item);
 59            }
 1660            content.JsonWriter.WriteEndArray();
 1661            request.Content = content;
 1662            return message;
 63        }
 64
 65        /// <summary> Publishes a batch of events to an Azure Event Grid topic. </summary>
 66        /// <param name="topicHostname"> The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. </param>
 67        /// <param name="events"> An array of events to be published to Event Grid. </param>
 68        /// <param name="cancellationToken"> The cancellation token to use. </param>
 69        /// <exception cref="ArgumentNullException"> <paramref name="topicHostname"/> or <paramref name="events"/> is nu
 70        public async Task<Response> PublishEventsAsync(string topicHostname, IEnumerable<EventGridEventInternal> events,
 71        {
 872            if (topicHostname == null)
 73            {
 074                throw new ArgumentNullException(nameof(topicHostname));
 75            }
 876            if (events == null)
 77            {
 078                throw new ArgumentNullException(nameof(events));
 79            }
 80
 881            using var message = CreatePublishEventsRequest(topicHostname, events);
 882            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 883            switch (message.Response.Status)
 84            {
 85                case 200:
 886                    return message.Response;
 87                default:
 088                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 89            }
 890        }
 91
 92        /// <summary> Publishes a batch of events to an Azure Event Grid topic. </summary>
 93        /// <param name="topicHostname"> The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. </param>
 94        /// <param name="events"> An array of events to be published to Event Grid. </param>
 95        /// <param name="cancellationToken"> The cancellation token to use. </param>
 96        /// <exception cref="ArgumentNullException"> <paramref name="topicHostname"/> or <paramref name="events"/> is nu
 97        public Response PublishEvents(string topicHostname, IEnumerable<EventGridEventInternal> events, CancellationToke
 98        {
 899            if (topicHostname == null)
 100            {
 0101                throw new ArgumentNullException(nameof(topicHostname));
 102            }
 8103            if (events == null)
 104            {
 0105                throw new ArgumentNullException(nameof(events));
 106            }
 107
 8108            using var message = CreatePublishEventsRequest(topicHostname, events);
 8109            _pipeline.Send(message, cancellationToken);
 8110            switch (message.Response.Status)
 111            {
 112                case 200:
 8113                    return message.Response;
 114                default:
 0115                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 116            }
 8117        }
 118
 119        internal HttpMessage CreatePublishCloudEventEventsRequest(string topicHostname, IEnumerable<CloudEventInternal> 
 120        {
 20121            var message = _pipeline.CreateMessage();
 20122            var request = message.Request;
 20123            request.Method = RequestMethod.Post;
 20124            var uri = new RawRequestUriBuilder();
 20125            uri.AppendRaw("https://", false);
 20126            uri.AppendRaw(topicHostname, false);
 20127            uri.AppendPath("/api/events", false);
 20128            uri.AppendQuery("api-version", apiVersion, true);
 20129            request.Uri = uri;
 20130            request.Headers.Add("Content-Type", "application/cloudevents-batch+json; charset=utf-8");
 20131            var content = new Utf8JsonRequestContent();
 20132            content.JsonWriter.WriteStartArray();
 480133            foreach (var item in events)
 134            {
 220135                content.JsonWriter.WriteObjectValue(item);
 136            }
 20137            content.JsonWriter.WriteEndArray();
 20138            request.Content = content;
 20139            return message;
 140        }
 141
 142        /// <summary> Publishes a batch of events to an Azure Event Grid topic. </summary>
 143        /// <param name="topicHostname"> The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. </param>
 144        /// <param name="events"> An array of events to be published to Event Grid. </param>
 145        /// <param name="cancellationToken"> The cancellation token to use. </param>
 146        /// <exception cref="ArgumentNullException"> <paramref name="topicHostname"/> or <paramref name="events"/> is nu
 147        public async Task<Response> PublishCloudEventEventsAsync(string topicHostname, IEnumerable<CloudEventInternal> e
 148        {
 10149            if (topicHostname == null)
 150            {
 0151                throw new ArgumentNullException(nameof(topicHostname));
 152            }
 10153            if (events == null)
 154            {
 0155                throw new ArgumentNullException(nameof(events));
 156            }
 157
 10158            using var message = CreatePublishCloudEventEventsRequest(topicHostname, events);
 10159            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 10160            switch (message.Response.Status)
 161            {
 162                case 200:
 10163                    return message.Response;
 164                default:
 0165                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 166            }
 10167        }
 168
 169        /// <summary> Publishes a batch of events to an Azure Event Grid topic. </summary>
 170        /// <param name="topicHostname"> The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. </param>
 171        /// <param name="events"> An array of events to be published to Event Grid. </param>
 172        /// <param name="cancellationToken"> The cancellation token to use. </param>
 173        /// <exception cref="ArgumentNullException"> <paramref name="topicHostname"/> or <paramref name="events"/> is nu
 174        public Response PublishCloudEventEvents(string topicHostname, IEnumerable<CloudEventInternal> events, Cancellati
 175        {
 10176            if (topicHostname == null)
 177            {
 0178                throw new ArgumentNullException(nameof(topicHostname));
 179            }
 10180            if (events == null)
 181            {
 0182                throw new ArgumentNullException(nameof(events));
 183            }
 184
 10185            using var message = CreatePublishCloudEventEventsRequest(topicHostname, events);
 10186            _pipeline.Send(message, cancellationToken);
 10187            switch (message.Response.Status)
 188            {
 189                case 200:
 10190                    return message.Response;
 191                default:
 0192                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 193            }
 10194        }
 195
 196        internal HttpMessage CreatePublishCustomEventEventsRequest(string topicHostname, IEnumerable<object> events)
 197        {
 8198            var message = _pipeline.CreateMessage();
 8199            var request = message.Request;
 8200            request.Method = RequestMethod.Post;
 8201            var uri = new RawRequestUriBuilder();
 8202            uri.AppendRaw("https://", false);
 8203            uri.AppendRaw(topicHostname, false);
 8204            uri.AppendPath("/api/events", false);
 8205            uri.AppendQuery("api-version", apiVersion, true);
 8206            request.Uri = uri;
 8207            request.Headers.Add("Content-Type", "application/json");
 8208            var content = new Utf8JsonRequestContent();
 8209            content.JsonWriter.WriteStartArray();
 176210            foreach (var item in events)
 211            {
 80212                content.JsonWriter.WriteObjectValue(item);
 213            }
 8214            content.JsonWriter.WriteEndArray();
 8215            request.Content = content;
 8216            return message;
 217        }
 218
 219        /// <summary> Publishes a batch of events to an Azure Event Grid topic. </summary>
 220        /// <param name="topicHostname"> The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. </param>
 221        /// <param name="events"> An array of events to be published to Event Grid. </param>
 222        /// <param name="cancellationToken"> The cancellation token to use. </param>
 223        /// <exception cref="ArgumentNullException"> <paramref name="topicHostname"/> or <paramref name="events"/> is nu
 224        public async Task<Response> PublishCustomEventEventsAsync(string topicHostname, IEnumerable<object> events, Canc
 225        {
 4226            if (topicHostname == null)
 227            {
 0228                throw new ArgumentNullException(nameof(topicHostname));
 229            }
 4230            if (events == null)
 231            {
 0232                throw new ArgumentNullException(nameof(events));
 233            }
 234
 4235            using var message = CreatePublishCustomEventEventsRequest(topicHostname, events);
 4236            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
 4237            switch (message.Response.Status)
 238            {
 239                case 200:
 4240                    return message.Response;
 241                default:
 0242                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa
 243            }
 4244        }
 245
 246        /// <summary> Publishes a batch of events to an Azure Event Grid topic. </summary>
 247        /// <param name="topicHostname"> The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. </param>
 248        /// <param name="events"> An array of events to be published to Event Grid. </param>
 249        /// <param name="cancellationToken"> The cancellation token to use. </param>
 250        /// <exception cref="ArgumentNullException"> <paramref name="topicHostname"/> or <paramref name="events"/> is nu
 251        public Response PublishCustomEventEvents(string topicHostname, IEnumerable<object> events, CancellationToken can
 252        {
 4253            if (topicHostname == null)
 254            {
 0255                throw new ArgumentNullException(nameof(topicHostname));
 256            }
 4257            if (events == null)
 258            {
 0259                throw new ArgumentNullException(nameof(events));
 260            }
 261
 4262            using var message = CreatePublishCustomEventEventsRequest(topicHostname, events);
 4263            _pipeline.Send(message, cancellationToken);
 4264            switch (message.Response.Status)
 265            {
 266                case 200:
 4267                    return message.Response;
 268                default:
 0269                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
 270            }
 4271        }
 272    }
 273}