|
| 1 | +namespace Fixtures.Azure.SwaggerBatAzureReport |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Diagnostics; |
| 6 | + using System.Net; |
| 7 | + using System.Net.Http; |
| 8 | + using System.Net.Http.Headers; |
| 9 | + using System.Text; |
| 10 | + using System.Text.RegularExpressions; |
| 11 | + using System.Threading; |
| 12 | + using System.Threading.Tasks; |
| 13 | + using Microsoft.Rest; |
| 14 | + using Microsoft.Rest.Serialization; |
| 15 | + using Newtonsoft.Json; |
| 16 | + using Microsoft.Azure; |
| 17 | + using Models; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Test Infrastructure for AutoRest |
| 21 | + /// </summary> |
| 22 | + public partial class AutoRestReportServiceForAzure : ServiceClient<AutoRestReportServiceForAzure>, IAutoRestReportServiceForAzure, IAzureClient |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// The base URI of the service. |
| 26 | + /// </summary> |
| 27 | + public Uri BaseUri { get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets or sets json serialization settings. |
| 31 | + /// </summary> |
| 32 | + public JsonSerializerSettings SerializationSettings { get; private set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets or sets json deserialization settings. |
| 36 | + /// </summary> |
| 37 | + public JsonSerializerSettings DeserializationSettings { get; private set; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// The Api Version. |
| 41 | + /// </summary> |
| 42 | + public string ApiVersion { get; private set; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Subscription credentials which uniquely identify Microsoft Azure |
| 46 | + /// subscription. |
| 47 | + /// </summary> |
| 48 | + public SubscriptionCloudCredentials Credentials { get; set; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// The retry timeout for Long Running Operations. |
| 52 | + /// </summary> |
| 53 | + public int? LongRunningOperationRetryTimeout { get; set; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Initializes a new instance of the AutoRestReportServiceForAzure class. |
| 57 | + /// </summary> |
| 58 | + public AutoRestReportServiceForAzure() : base() |
| 59 | + { |
| 60 | + this.Initialize(); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Initializes a new instance of the AutoRestReportServiceForAzure class. |
| 65 | + /// </summary> |
| 66 | + /// <param name='handlers'> |
| 67 | + /// Optional. The set of delegating handlers to insert in the http |
| 68 | + /// client pipeline. |
| 69 | + /// </param> |
| 70 | + public AutoRestReportServiceForAzure(params DelegatingHandler[] handlers) : base(handlers) |
| 71 | + { |
| 72 | + this.Initialize(); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Initializes a new instance of the AutoRestReportServiceForAzure class. |
| 77 | + /// </summary> |
| 78 | + /// <param name='rootHandler'> |
| 79 | + /// Optional. The http client handler used to handle http transport. |
| 80 | + /// </param> |
| 81 | + /// <param name='handlers'> |
| 82 | + /// Optional. The set of delegating handlers to insert in the http |
| 83 | + /// client pipeline. |
| 84 | + /// </param> |
| 85 | + public AutoRestReportServiceForAzure(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootHandler, handlers) |
| 86 | + { |
| 87 | + this.Initialize(); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Initializes a new instance of the AutoRestReportServiceForAzure class. |
| 92 | + /// </summary> |
| 93 | + /// <param name='baseUri'> |
| 94 | + /// Optional. The base URI of the service. |
| 95 | + /// </param> |
| 96 | + /// <param name='handlers'> |
| 97 | + /// Optional. The set of delegating handlers to insert in the http |
| 98 | + /// client pipeline. |
| 99 | + /// </param> |
| 100 | + public AutoRestReportServiceForAzure(Uri baseUri, params DelegatingHandler[] handlers) : this(handlers) |
| 101 | + { |
| 102 | + if (baseUri == null) |
| 103 | + { |
| 104 | + throw new ArgumentNullException("baseUri"); |
| 105 | + } |
| 106 | + this.BaseUri = baseUri; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Initializes a new instance of the AutoRestReportServiceForAzure class. |
| 111 | + /// </summary> |
| 112 | + /// <param name='credentials'> |
| 113 | + /// Required. Subscription credentials which uniquely identify Microsoft Azure subscription. |
| 114 | + /// </param> |
| 115 | + /// <param name='handlers'> |
| 116 | + /// Optional. The set of delegating handlers to insert in the http |
| 117 | + /// client pipeline. |
| 118 | + /// </param> |
| 119 | + public AutoRestReportServiceForAzure(SubscriptionCloudCredentials credentials, params DelegatingHandler[] handlers) : this(handlers) |
| 120 | + { |
| 121 | + if (credentials == null) |
| 122 | + { |
| 123 | + throw new ArgumentNullException("credentials"); |
| 124 | + } |
| 125 | + this.Credentials = credentials; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Initializes a new instance of the AutoRestReportServiceForAzure class. |
| 130 | + /// </summary> |
| 131 | + /// <param name='baseUri'> |
| 132 | + /// Optional. The base URI of the service. |
| 133 | + /// </param> |
| 134 | + /// <param name='credentials'> |
| 135 | + /// Required. Subscription credentials which uniquely identify Microsoft Azure subscription. |
| 136 | + /// </param> |
| 137 | + /// <param name='handlers'> |
| 138 | + /// Optional. The set of delegating handlers to insert in the http |
| 139 | + /// client pipeline. |
| 140 | + /// </param> |
| 141 | + public AutoRestReportServiceForAzure(Uri baseUri, SubscriptionCloudCredentials credentials, params DelegatingHandler[] handlers) : this(handlers) |
| 142 | + { |
| 143 | + if (baseUri == null) |
| 144 | + { |
| 145 | + throw new ArgumentNullException("baseUri"); |
| 146 | + } |
| 147 | + if (credentials == null) |
| 148 | + { |
| 149 | + throw new ArgumentNullException("credentials"); |
| 150 | + } |
| 151 | + this.BaseUri = baseUri; |
| 152 | + this.Credentials = credentials; |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Initializes client properties. |
| 157 | + /// </summary> |
| 158 | + private void Initialize() |
| 159 | + { |
| 160 | + this.BaseUri = new Uri("http://localhost"); |
| 161 | + this.ApiVersion = "1.0.0"; |
| 162 | + SerializationSettings = new JsonSerializerSettings |
| 163 | + { |
| 164 | + Formatting = Formatting.Indented, |
| 165 | + DateFormatHandling = DateFormatHandling.IsoDateFormat, |
| 166 | + DateTimeZoneHandling = DateTimeZoneHandling.Utc, |
| 167 | + NullValueHandling = NullValueHandling.Ignore, |
| 168 | + ReferenceLoopHandling = ReferenceLoopHandling.Serialize, |
| 169 | + ContractResolver = new ReadOnlyJsonContractResolver() |
| 170 | + }; |
| 171 | + SerializationSettings.Converters.Add(new ResourceJsonConverter()); |
| 172 | + DeserializationSettings = new JsonSerializerSettings{ |
| 173 | + DateFormatHandling = DateFormatHandling.IsoDateFormat, |
| 174 | + DateTimeZoneHandling = DateTimeZoneHandling.Utc, |
| 175 | + NullValueHandling = NullValueHandling.Ignore, |
| 176 | + ReferenceLoopHandling = ReferenceLoopHandling.Serialize, |
| 177 | + ContractResolver = new ReadOnlyJsonContractResolver() |
| 178 | + }; |
| 179 | + DeserializationSettings.Converters.Add(new ResourceJsonConverter()); |
| 180 | + DeserializationSettings.Converters.Add(new CloudErrorJsonConverter()); |
| 181 | + } |
| 182 | + /// <summary> |
| 183 | + /// Get test coverage report |
| 184 | + /// </summary> |
| 185 | + /// <param name='cancellationToken'> |
| 186 | + /// Cancellation token. |
| 187 | + /// </param> |
| 188 | + public async Task<AzureOperationResponse<IDictionary<string, int?>>> GetReportWithOperationResponseAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| 189 | + { |
| 190 | + // Tracing |
| 191 | + bool shouldTrace = ServiceClientTracing.IsEnabled; |
| 192 | + string invocationId = null; |
| 193 | + if (shouldTrace) |
| 194 | + { |
| 195 | + invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 196 | + Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 197 | + tracingParameters.Add("cancellationToken", cancellationToken); |
| 198 | + ServiceClientTracing.Enter(invocationId, this, "GetReport", tracingParameters); |
| 199 | + } |
| 200 | + // Construct URL |
| 201 | + string url = this.BaseUri.AbsoluteUri + |
| 202 | + "//report/azure"; |
| 203 | + List<string> queryParameters = new List<string>(); |
| 204 | + queryParameters.Add(string.Format("api-version={0}", Uri.EscapeDataString(this.ApiVersion))); |
| 205 | + if (queryParameters.Count > 0) |
| 206 | + { |
| 207 | + url += "?" + string.Join("&", queryParameters); |
| 208 | + } |
| 209 | + // trim all duplicate forward slashes in the url |
| 210 | + url = Regex.Replace(url, "([^:]/)/+", "$1"); |
| 211 | + // Create HTTP transport objects |
| 212 | + HttpRequestMessage httpRequest = new HttpRequestMessage(); |
| 213 | + httpRequest.Method = new HttpMethod("GET"); |
| 214 | + httpRequest.RequestUri = new Uri(url); |
| 215 | + // Set Headers |
| 216 | + // Set Credentials |
| 217 | + cancellationToken.ThrowIfCancellationRequested(); |
| 218 | + await this.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); |
| 219 | + // Send Request |
| 220 | + if (shouldTrace) |
| 221 | + { |
| 222 | + ServiceClientTracing.SendRequest(invocationId, httpRequest); |
| 223 | + } |
| 224 | + cancellationToken.ThrowIfCancellationRequested(); |
| 225 | + HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); |
| 226 | + if (shouldTrace) |
| 227 | + { |
| 228 | + ServiceClientTracing.ReceiveResponse(invocationId, httpResponse); |
| 229 | + } |
| 230 | + HttpStatusCode statusCode = httpResponse.StatusCode; |
| 231 | + cancellationToken.ThrowIfCancellationRequested(); |
| 232 | + string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 233 | + if (!(statusCode == (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), "OK"))) |
| 234 | + { |
| 235 | + var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", statusCode)); |
| 236 | + Error errorBody = JsonConvert.DeserializeObject<Error>(responseContent, this.DeserializationSettings); |
| 237 | + if (errorBody != null) |
| 238 | + { |
| 239 | + ex.Body = errorBody; |
| 240 | + } |
| 241 | + ex.Request = httpRequest; |
| 242 | + ex.Response = httpResponse; |
| 243 | + if (shouldTrace) |
| 244 | + { |
| 245 | + ServiceClientTracing.Error(invocationId, ex); |
| 246 | + } |
| 247 | + throw ex; |
| 248 | + } |
| 249 | + // Create Result |
| 250 | + var result = new AzureOperationResponse<IDictionary<string, int?>>(); |
| 251 | + result.Request = httpRequest; |
| 252 | + result.Response = httpResponse; |
| 253 | + // Deserialize Response |
| 254 | + if (statusCode == (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), "OK")) |
| 255 | + { |
| 256 | + result.Body = JsonConvert.DeserializeObject<IDictionary<string, int?>>(responseContent, this.DeserializationSettings); |
| 257 | + } |
| 258 | + if (shouldTrace) |
| 259 | + { |
| 260 | + ServiceClientTracing.Exit(invocationId, result); |
| 261 | + } |
| 262 | + return result; |
| 263 | + } |
| 264 | + |
| 265 | + } |
| 266 | +} |
0 commit comments