| | 1 | | // <auto-generated> |
| | 2 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 3 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 4 | | // license information. |
| | 5 | | // |
| | 6 | | // Code generated by Microsoft (R) AutoRest Code Generator. |
| | 7 | | // Changes may cause incorrect behavior and will be lost if the code is |
| | 8 | | // regenerated. |
| | 9 | | // </auto-generated> |
| | 10 | |
|
| | 11 | | namespace Microsoft.Azure.EventGrid |
| | 12 | | { |
| | 13 | | using Microsoft.Rest; |
| | 14 | | using Microsoft.Rest.Azure; |
| | 15 | | using Microsoft.Rest.Serialization; |
| | 16 | | using Models; |
| | 17 | | using Newtonsoft.Json; |
| | 18 | | using System.Collections; |
| | 19 | | using System.Collections.Generic; |
| | 20 | | using System.Linq; |
| | 21 | | using System.Net; |
| | 22 | | using System.Net.Http; |
| | 23 | | using System.Threading; |
| | 24 | | using System.Threading.Tasks; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// EventGrid Client |
| | 28 | | /// </summary> |
| | 29 | | public partial class EventGridClient : ServiceClient<EventGridClient>, IEventGridClient, IAzureClient |
| | 30 | | { |
| | 31 | | /// <summary> |
| | 32 | | /// The base URI of the service. |
| | 33 | | /// </summary> |
| 4 | 34 | | internal string BaseUri {get; set;} |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets or sets json serialization settings. |
| | 38 | | /// </summary> |
| 6 | 39 | | public JsonSerializerSettings SerializationSettings { get; private set; } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets or sets json deserialization settings. |
| | 43 | | /// </summary> |
| 6 | 44 | | public JsonSerializerSettings DeserializationSettings { get; private set; } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Credentials needed for the client to connect to Azure. |
| | 48 | | /// </summary> |
| 10 | 49 | | public ServiceClientCredentials Credentials { get; private set; } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Version of the API to be used with the client request. |
| | 53 | | /// </summary> |
| 0 | 54 | | public string ApiVersion { get; private set; } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// The preferred language for the response. |
| | 58 | | /// </summary> |
| 6 | 59 | | public string AcceptLanguage { get; set; } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// The retry timeout in seconds for Long Running Operations. Default value is |
| | 63 | | /// 30. |
| | 64 | | /// </summary> |
| 0 | 65 | | public int? LongRunningOperationRetryTimeout { get; set; } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Whether a unique x-ms-client-request-id should be generated. When set to |
| | 69 | | /// true a unique x-ms-client-request-id value is generated and included in |
| | 70 | | /// each request. Default is true. |
| | 71 | | /// </summary> |
| 6 | 72 | | public bool? GenerateClientRequestId { get; set; } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Initializes a new instance of the EventGridClient class. |
| | 76 | | /// </summary> |
| | 77 | | /// <param name='httpClient'> |
| | 78 | | /// HttpClient to be used |
| | 79 | | /// </param> |
| | 80 | | /// <param name='disposeHttpClient'> |
| | 81 | | /// True: will dispose the provided httpClient on calling EventGridClient.Dispose(). False: will not dispose pro |
| 0 | 82 | | protected EventGridClient(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClient) |
| | 83 | | { |
| 0 | 84 | | Initialize(); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Initializes a new instance of the EventGridClient class. |
| | 89 | | /// </summary> |
| | 90 | | /// <param name='handlers'> |
| | 91 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 92 | | /// </param> |
| 2 | 93 | | protected EventGridClient(params DelegatingHandler[] handlers) : base(handlers) |
| | 94 | | { |
| 2 | 95 | | Initialize(); |
| 2 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Initializes a new instance of the EventGridClient class. |
| | 100 | | /// </summary> |
| | 101 | | /// <param name='rootHandler'> |
| | 102 | | /// Optional. The http client handler used to handle http transport. |
| | 103 | | /// </param> |
| | 104 | | /// <param name='handlers'> |
| | 105 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 106 | | /// </param> |
| 0 | 107 | | protected EventGridClient(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootHandler |
| | 108 | | { |
| 0 | 109 | | Initialize(); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Initializes a new instance of the EventGridClient class. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name='credentials'> |
| | 116 | | /// Required. Credentials needed for the client to connect to Azure. |
| | 117 | | /// </param> |
| | 118 | | /// <param name='handlers'> |
| | 119 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 120 | | /// </param> |
| | 121 | | /// <exception cref="System.ArgumentNullException"> |
| | 122 | | /// Thrown when a required parameter is null |
| | 123 | | /// </exception> |
| 2 | 124 | | public EventGridClient(ServiceClientCredentials credentials, params DelegatingHandler[] handlers) : this(handler |
| | 125 | | { |
| 2 | 126 | | if (credentials == null) |
| | 127 | | { |
| 0 | 128 | | throw new System.ArgumentNullException("credentials"); |
| | 129 | | } |
| 2 | 130 | | Credentials = credentials; |
| 2 | 131 | | if (Credentials != null) |
| | 132 | | { |
| 2 | 133 | | Credentials.InitializeServiceClient(this); |
| | 134 | | } |
| 2 | 135 | | } |
| | 136 | |
|
| | 137 | | /// <summary> |
| | 138 | | /// Initializes a new instance of the EventGridClient class. |
| | 139 | | /// </summary> |
| | 140 | | /// <param name='credentials'> |
| | 141 | | /// Required. Credentials needed for the client to connect to Azure. |
| | 142 | | /// </param> |
| | 143 | | /// <param name='httpClient'> |
| | 144 | | /// HttpClient to be used |
| | 145 | | /// </param> |
| | 146 | | /// <param name='disposeHttpClient'> |
| | 147 | | /// True: will dispose the provided httpClient on calling EventGridClient.Dispose(). False: will not dispose pro |
| | 148 | | /// <exception cref="System.ArgumentNullException"> |
| | 149 | | /// Thrown when a required parameter is null |
| | 150 | | /// </exception> |
| 0 | 151 | | public EventGridClient(ServiceClientCredentials credentials, HttpClient httpClient, bool disposeHttpClient) : th |
| | 152 | | { |
| 0 | 153 | | if (credentials == null) |
| | 154 | | { |
| 0 | 155 | | throw new System.ArgumentNullException("credentials"); |
| | 156 | | } |
| 0 | 157 | | Credentials = credentials; |
| 0 | 158 | | if (Credentials != null) |
| | 159 | | { |
| 0 | 160 | | Credentials.InitializeServiceClient(this); |
| | 161 | | } |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// Initializes a new instance of the EventGridClient class. |
| | 166 | | /// </summary> |
| | 167 | | /// <param name='credentials'> |
| | 168 | | /// Required. Credentials needed for the client to connect to Azure. |
| | 169 | | /// </param> |
| | 170 | | /// <param name='rootHandler'> |
| | 171 | | /// Optional. The http client handler used to handle http transport. |
| | 172 | | /// </param> |
| | 173 | | /// <param name='handlers'> |
| | 174 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 175 | | /// </param> |
| | 176 | | /// <exception cref="System.ArgumentNullException"> |
| | 177 | | /// Thrown when a required parameter is null |
| | 178 | | /// </exception> |
| 0 | 179 | | public EventGridClient(ServiceClientCredentials credentials, HttpClientHandler rootHandler, params DelegatingHan |
| | 180 | | { |
| 0 | 181 | | if (credentials == null) |
| | 182 | | { |
| 0 | 183 | | throw new System.ArgumentNullException("credentials"); |
| | 184 | | } |
| 0 | 185 | | Credentials = credentials; |
| 0 | 186 | | if (Credentials != null) |
| | 187 | | { |
| 0 | 188 | | Credentials.InitializeServiceClient(this); |
| | 189 | | } |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | /// <summary> |
| | 193 | | /// An optional partial-method to perform custom initialization. |
| | 194 | | /// </summary> |
| | 195 | | partial void CustomInitialize(); |
| | 196 | | /// <summary> |
| | 197 | | /// Initializes client properties. |
| | 198 | | /// </summary> |
| | 199 | | private void Initialize() |
| | 200 | | { |
| 2 | 201 | | BaseUri = "https://{topicHostname}"; |
| 2 | 202 | | ApiVersion = "2018-01-01"; |
| 2 | 203 | | AcceptLanguage = "en-US"; |
| 2 | 204 | | LongRunningOperationRetryTimeout = 30; |
| 2 | 205 | | GenerateClientRequestId = true; |
| 2 | 206 | | SerializationSettings = new JsonSerializerSettings |
| 2 | 207 | | { |
| 2 | 208 | | Formatting = Newtonsoft.Json.Formatting.Indented, |
| 2 | 209 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| 2 | 210 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| 2 | 211 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| 2 | 212 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| 2 | 213 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| 2 | 214 | | Converters = new List<JsonConverter> |
| 2 | 215 | | { |
| 2 | 216 | | new Iso8601TimeSpanConverter() |
| 2 | 217 | | } |
| 2 | 218 | | }; |
| 2 | 219 | | DeserializationSettings = new JsonSerializerSettings |
| 2 | 220 | | { |
| 2 | 221 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| 2 | 222 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| 2 | 223 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| 2 | 224 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| 2 | 225 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| 2 | 226 | | Converters = new List<JsonConverter> |
| 2 | 227 | | { |
| 2 | 228 | | new Iso8601TimeSpanConverter() |
| 2 | 229 | | } |
| 2 | 230 | | }; |
| 2 | 231 | | SerializationSettings.Converters.Add(new PolymorphicSerializeJsonConverter<MediaJobOutput>("@odata.type")); |
| 2 | 232 | | DeserializationSettings.Converters.Add(new PolymorphicDeserializeJsonConverter<MediaJobOutput>("@odata.type" |
| | 233 | | CustomInitialize(); |
| 2 | 234 | | DeserializationSettings.Converters.Add(new CloudErrorJsonConverter()); |
| 2 | 235 | | } |
| | 236 | | /// <summary> |
| | 237 | | /// Publishes a batch of events to an Azure Event Grid topic. |
| | 238 | | /// </summary> |
| | 239 | | /// <param name='topicHostname'> |
| | 240 | | /// The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net |
| | 241 | | /// </param> |
| | 242 | | /// <param name='events'> |
| | 243 | | /// An array of events to be published to Event Grid. |
| | 244 | | /// </param> |
| | 245 | | /// <param name='customHeaders'> |
| | 246 | | /// Headers that will be added to request. |
| | 247 | | /// </param> |
| | 248 | | /// <param name='cancellationToken'> |
| | 249 | | /// The cancellation token. |
| | 250 | | /// </param> |
| | 251 | | /// <exception cref="CloudException"> |
| | 252 | | /// Thrown when the operation returned an invalid status code |
| | 253 | | /// </exception> |
| | 254 | | /// <exception cref="ValidationException"> |
| | 255 | | /// Thrown when a required parameter is null |
| | 256 | | /// </exception> |
| | 257 | | /// <exception cref="System.ArgumentNullException"> |
| | 258 | | /// Thrown when a required parameter is null |
| | 259 | | /// </exception> |
| | 260 | | /// <return> |
| | 261 | | /// A response object containing the response body and response headers. |
| | 262 | | /// </return> |
| | 263 | | public async Task<AzureOperationResponse> PublishEventsWithHttpMessagesAsync(string topicHostname, IList<EventGr |
| | 264 | | { |
| 2 | 265 | | if (topicHostname == null) |
| | 266 | | { |
| 0 | 267 | | throw new ValidationException(ValidationRules.CannotBeNull, "topicHostname"); |
| | 268 | | } |
| 2 | 269 | | if (events == null) |
| | 270 | | { |
| 0 | 271 | | throw new ValidationException(ValidationRules.CannotBeNull, "events"); |
| | 272 | | } |
| 2 | 273 | | if (events != null) |
| | 274 | | { |
| 44 | 275 | | foreach (var element in events) |
| | 276 | | { |
| 20 | 277 | | if (element != null) |
| | 278 | | { |
| 20 | 279 | | element.Validate(); |
| | 280 | | } |
| | 281 | | } |
| | 282 | | } |
| 2 | 283 | | string apiVersion = "2018-01-01"; |
| | 284 | | // Tracing |
| 2 | 285 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 286 | | string _invocationId = null; |
| 2 | 287 | | if (_shouldTrace) |
| | 288 | | { |
| 0 | 289 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 290 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 291 | | tracingParameters.Add("topicHostname", topicHostname); |
| 0 | 292 | | tracingParameters.Add("events", events); |
| 0 | 293 | | tracingParameters.Add("apiVersion", apiVersion); |
| 0 | 294 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 295 | | ServiceClientTracing.Enter(_invocationId, this, "PublishEvents", tracingParameters); |
| | 296 | | } |
| | 297 | | // Construct URL |
| 2 | 298 | | var _baseUrl = BaseUri; |
| 2 | 299 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "api/events"; |
| 2 | 300 | | _url = _url.Replace("{topicHostname}", topicHostname); |
| 2 | 301 | | List<string> _queryParameters = new List<string>(); |
| 2 | 302 | | if (apiVersion != null) |
| | 303 | | { |
| 2 | 304 | | _queryParameters.Add(string.Format("api-version={0}", System.Uri.EscapeDataString(apiVersion))); |
| | 305 | | } |
| 2 | 306 | | if (_queryParameters.Count > 0) |
| | 307 | | { |
| 2 | 308 | | _url += (_url.Contains("?") ? "&" : "?") + string.Join("&", _queryParameters); |
| | 309 | | } |
| | 310 | | // Create HTTP transport objects |
| 2 | 311 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 312 | | HttpResponseMessage _httpResponse = null; |
| 2 | 313 | | _httpRequest.Method = new HttpMethod("POST"); |
| 2 | 314 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 315 | | // Set Headers |
| 2 | 316 | | if (GenerateClientRequestId != null && GenerateClientRequestId.Value) |
| | 317 | | { |
| 2 | 318 | | _httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", System.Guid.NewGuid().ToString()) |
| | 319 | | } |
| 2 | 320 | | if (AcceptLanguage != null) |
| | 321 | | { |
| 2 | 322 | | if (_httpRequest.Headers.Contains("accept-language")) |
| | 323 | | { |
| 0 | 324 | | _httpRequest.Headers.Remove("accept-language"); |
| | 325 | | } |
| 2 | 326 | | _httpRequest.Headers.TryAddWithoutValidation("accept-language", AcceptLanguage); |
| | 327 | | } |
| | 328 | |
|
| | 329 | |
|
| 2 | 330 | | if (customHeaders != null) |
| | 331 | | { |
| 0 | 332 | | foreach(var _header in customHeaders) |
| | 333 | | { |
| 0 | 334 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 335 | | { |
| 0 | 336 | | _httpRequest.Headers.Remove(_header.Key); |
| | 337 | | } |
| 0 | 338 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 339 | | } |
| | 340 | | } |
| | 341 | |
|
| | 342 | | // Serialize Request |
| 2 | 343 | | string _requestContent = null; |
| 2 | 344 | | if(events != null) |
| | 345 | | { |
| 2 | 346 | | _requestContent = SafeJsonConvert.SerializeObject(events, SerializationSettings); |
| 2 | 347 | | _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8); |
| 2 | 348 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("applicatio |
| | 349 | | } |
| | 350 | | // Set Credentials |
| 2 | 351 | | if (Credentials != null) |
| | 352 | | { |
| 2 | 353 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 354 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 355 | | } |
| | 356 | | // Send Request |
| 2 | 357 | | if (_shouldTrace) |
| | 358 | | { |
| 0 | 359 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 360 | | } |
| 2 | 361 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 362 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 363 | | if (_shouldTrace) |
| | 364 | | { |
| 0 | 365 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 366 | | } |
| 2 | 367 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 368 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 369 | | string _responseContent = null; |
| 2 | 370 | | if ((int)_statusCode != 200) |
| | 371 | | { |
| 0 | 372 | | var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode |
| | 373 | | try |
| | 374 | | { |
| 0 | 375 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 376 | | CloudError _errorBody = SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, Deserializa |
| 0 | 377 | | if (_errorBody != null) |
| | 378 | | { |
| 0 | 379 | | ex = new CloudException(_errorBody.Message); |
| 0 | 380 | | ex.Body = _errorBody; |
| | 381 | | } |
| 0 | 382 | | } |
| 0 | 383 | | catch (JsonException) |
| | 384 | | { |
| | 385 | | // Ignore the exception |
| 0 | 386 | | } |
| 0 | 387 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 388 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 389 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 390 | | { |
| 0 | 391 | | ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 392 | | } |
| 0 | 393 | | if (_shouldTrace) |
| | 394 | | { |
| 0 | 395 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 396 | | } |
| 0 | 397 | | _httpRequest.Dispose(); |
| 0 | 398 | | if (_httpResponse != null) |
| | 399 | | { |
| 0 | 400 | | _httpResponse.Dispose(); |
| | 401 | | } |
| 0 | 402 | | throw ex; |
| | 403 | | } |
| | 404 | | // Create Result |
| 2 | 405 | | var _result = new AzureOperationResponse(); |
| 2 | 406 | | _result.Request = _httpRequest; |
| 2 | 407 | | _result.Response = _httpResponse; |
| 2 | 408 | | if (_httpResponse.Headers.Contains("x-ms-request-id")) |
| | 409 | | { |
| 2 | 410 | | _result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); |
| | 411 | | } |
| 2 | 412 | | if (_shouldTrace) |
| | 413 | | { |
| 0 | 414 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 415 | | } |
| 2 | 416 | | return _result; |
| 2 | 417 | | } |
| | 418 | |
|
| | 419 | | } |
| | 420 | | } |