| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // <auto-generated/> |
| | 5 | |
|
| | 6 | | #nullable disable |
| | 7 | |
|
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using Azure; |
| | 13 | | using Azure.Core; |
| | 14 | | using Azure.Core.Pipeline; |
| | 15 | | using Azure.Messaging.EventGrid.Models; |
| | 16 | |
|
| | 17 | | namespace 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> |
| 44 | 30 | | public ServiceRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string apiVersion = "2018-0 |
| | 31 | | { |
| 44 | 32 | | if (apiVersion == null) |
| | 33 | | { |
| 0 | 34 | | throw new ArgumentNullException(nameof(apiVersion)); |
| | 35 | | } |
| | 36 | |
|
| 44 | 37 | | this.apiVersion = apiVersion; |
| 44 | 38 | | _clientDiagnostics = clientDiagnostics; |
| 44 | 39 | | _pipeline = pipeline; |
| 44 | 40 | | } |
| | 41 | |
|
| | 42 | | internal HttpMessage CreatePublishEventsRequest(string topicHostname, IEnumerable<EventGridEventInternal> events |
| | 43 | | { |
| 16 | 44 | | var message = _pipeline.CreateMessage(); |
| 16 | 45 | | var request = message.Request; |
| 16 | 46 | | request.Method = RequestMethod.Post; |
| 16 | 47 | | var uri = new RawRequestUriBuilder(); |
| 16 | 48 | | uri.AppendRaw("https://", false); |
| 16 | 49 | | uri.AppendRaw(topicHostname, false); |
| 16 | 50 | | uri.AppendPath("/api/events", false); |
| 16 | 51 | | uri.AppendQuery("api-version", apiVersion, true); |
| 16 | 52 | | request.Uri = uri; |
| 16 | 53 | | request.Headers.Add("Content-Type", "application/json"); |
| 16 | 54 | | var content = new Utf8JsonRequestContent(); |
| 16 | 55 | | content.JsonWriter.WriteStartArray(); |
| 352 | 56 | | foreach (var item in events) |
| | 57 | | { |
| 160 | 58 | | content.JsonWriter.WriteObjectValue(item); |
| | 59 | | } |
| 16 | 60 | | content.JsonWriter.WriteEndArray(); |
| 16 | 61 | | request.Content = content; |
| 16 | 62 | | 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 | | { |
| 8 | 72 | | if (topicHostname == null) |
| | 73 | | { |
| 0 | 74 | | throw new ArgumentNullException(nameof(topicHostname)); |
| | 75 | | } |
| 8 | 76 | | if (events == null) |
| | 77 | | { |
| 0 | 78 | | throw new ArgumentNullException(nameof(events)); |
| | 79 | | } |
| | 80 | |
|
| 8 | 81 | | using var message = CreatePublishEventsRequest(topicHostname, events); |
| 8 | 82 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 8 | 83 | | switch (message.Response.Status) |
| | 84 | | { |
| | 85 | | case 200: |
| 8 | 86 | | return message.Response; |
| | 87 | | default: |
| 0 | 88 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 89 | | } |
| 8 | 90 | | } |
| | 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 | | { |
| 8 | 99 | | if (topicHostname == null) |
| | 100 | | { |
| 0 | 101 | | throw new ArgumentNullException(nameof(topicHostname)); |
| | 102 | | } |
| 8 | 103 | | if (events == null) |
| | 104 | | { |
| 0 | 105 | | throw new ArgumentNullException(nameof(events)); |
| | 106 | | } |
| | 107 | |
|
| 8 | 108 | | using var message = CreatePublishEventsRequest(topicHostname, events); |
| 8 | 109 | | _pipeline.Send(message, cancellationToken); |
| 8 | 110 | | switch (message.Response.Status) |
| | 111 | | { |
| | 112 | | case 200: |
| 8 | 113 | | return message.Response; |
| | 114 | | default: |
| 0 | 115 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 116 | | } |
| 8 | 117 | | } |
| | 118 | |
|
| | 119 | | internal HttpMessage CreatePublishCloudEventEventsRequest(string topicHostname, IEnumerable<CloudEventInternal> |
| | 120 | | { |
| 20 | 121 | | var message = _pipeline.CreateMessage(); |
| 20 | 122 | | var request = message.Request; |
| 20 | 123 | | request.Method = RequestMethod.Post; |
| 20 | 124 | | var uri = new RawRequestUriBuilder(); |
| 20 | 125 | | uri.AppendRaw("https://", false); |
| 20 | 126 | | uri.AppendRaw(topicHostname, false); |
| 20 | 127 | | uri.AppendPath("/api/events", false); |
| 20 | 128 | | uri.AppendQuery("api-version", apiVersion, true); |
| 20 | 129 | | request.Uri = uri; |
| 20 | 130 | | request.Headers.Add("Content-Type", "application/cloudevents-batch+json; charset=utf-8"); |
| 20 | 131 | | var content = new Utf8JsonRequestContent(); |
| 20 | 132 | | content.JsonWriter.WriteStartArray(); |
| 480 | 133 | | foreach (var item in events) |
| | 134 | | { |
| 220 | 135 | | content.JsonWriter.WriteObjectValue(item); |
| | 136 | | } |
| 20 | 137 | | content.JsonWriter.WriteEndArray(); |
| 20 | 138 | | request.Content = content; |
| 20 | 139 | | 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 | | { |
| 10 | 149 | | if (topicHostname == null) |
| | 150 | | { |
| 0 | 151 | | throw new ArgumentNullException(nameof(topicHostname)); |
| | 152 | | } |
| 10 | 153 | | if (events == null) |
| | 154 | | { |
| 0 | 155 | | throw new ArgumentNullException(nameof(events)); |
| | 156 | | } |
| | 157 | |
|
| 10 | 158 | | using var message = CreatePublishCloudEventEventsRequest(topicHostname, events); |
| 10 | 159 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 10 | 160 | | switch (message.Response.Status) |
| | 161 | | { |
| | 162 | | case 200: |
| 10 | 163 | | return message.Response; |
| | 164 | | default: |
| 0 | 165 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 166 | | } |
| 10 | 167 | | } |
| | 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 | | { |
| 10 | 176 | | if (topicHostname == null) |
| | 177 | | { |
| 0 | 178 | | throw new ArgumentNullException(nameof(topicHostname)); |
| | 179 | | } |
| 10 | 180 | | if (events == null) |
| | 181 | | { |
| 0 | 182 | | throw new ArgumentNullException(nameof(events)); |
| | 183 | | } |
| | 184 | |
|
| 10 | 185 | | using var message = CreatePublishCloudEventEventsRequest(topicHostname, events); |
| 10 | 186 | | _pipeline.Send(message, cancellationToken); |
| 10 | 187 | | switch (message.Response.Status) |
| | 188 | | { |
| | 189 | | case 200: |
| 10 | 190 | | return message.Response; |
| | 191 | | default: |
| 0 | 192 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 193 | | } |
| 10 | 194 | | } |
| | 195 | |
|
| | 196 | | internal HttpMessage CreatePublishCustomEventEventsRequest(string topicHostname, IEnumerable<object> events) |
| | 197 | | { |
| 8 | 198 | | var message = _pipeline.CreateMessage(); |
| 8 | 199 | | var request = message.Request; |
| 8 | 200 | | request.Method = RequestMethod.Post; |
| 8 | 201 | | var uri = new RawRequestUriBuilder(); |
| 8 | 202 | | uri.AppendRaw("https://", false); |
| 8 | 203 | | uri.AppendRaw(topicHostname, false); |
| 8 | 204 | | uri.AppendPath("/api/events", false); |
| 8 | 205 | | uri.AppendQuery("api-version", apiVersion, true); |
| 8 | 206 | | request.Uri = uri; |
| 8 | 207 | | request.Headers.Add("Content-Type", "application/json"); |
| 8 | 208 | | var content = new Utf8JsonRequestContent(); |
| 8 | 209 | | content.JsonWriter.WriteStartArray(); |
| 176 | 210 | | foreach (var item in events) |
| | 211 | | { |
| 80 | 212 | | content.JsonWriter.WriteObjectValue(item); |
| | 213 | | } |
| 8 | 214 | | content.JsonWriter.WriteEndArray(); |
| 8 | 215 | | request.Content = content; |
| 8 | 216 | | 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 | | { |
| 4 | 226 | | if (topicHostname == null) |
| | 227 | | { |
| 0 | 228 | | throw new ArgumentNullException(nameof(topicHostname)); |
| | 229 | | } |
| 4 | 230 | | if (events == null) |
| | 231 | | { |
| 0 | 232 | | throw new ArgumentNullException(nameof(events)); |
| | 233 | | } |
| | 234 | |
|
| 4 | 235 | | using var message = CreatePublishCustomEventEventsRequest(topicHostname, events); |
| 4 | 236 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 4 | 237 | | switch (message.Response.Status) |
| | 238 | | { |
| | 239 | | case 200: |
| 4 | 240 | | return message.Response; |
| | 241 | | default: |
| 0 | 242 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 243 | | } |
| 4 | 244 | | } |
| | 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 | | { |
| 4 | 253 | | if (topicHostname == null) |
| | 254 | | { |
| 0 | 255 | | throw new ArgumentNullException(nameof(topicHostname)); |
| | 256 | | } |
| 4 | 257 | | if (events == null) |
| | 258 | | { |
| 0 | 259 | | throw new ArgumentNullException(nameof(events)); |
| | 260 | | } |
| | 261 | |
|
| 4 | 262 | | using var message = CreatePublishCustomEventEventsRequest(topicHostname, events); |
| 4 | 263 | | _pipeline.Send(message, cancellationToken); |
| 4 | 264 | | switch (message.Response.Status) |
| | 265 | | { |
| | 266 | | case 200: |
| 4 | 267 | | return message.Response; |
| | 268 | | default: |
| 0 | 269 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 270 | | } |
| 4 | 271 | | } |
| | 272 | | } |
| | 273 | | } |