using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.Rest; using Microsoft.Rest.Serialization; using Newtonsoft.Json; namespace k8s { public partial class Kubernetes { /// /// The base URI of the service. /// public System.Uri BaseUri { get; set; } /// /// Gets json serialization settings. /// public JsonSerializerSettings SerializationSettings { get; private set; } /// /// Gets json deserialization settings. /// public JsonSerializerSettings DeserializationSettings { get; private set; } /// /// Subscription credentials which uniquely identify client subscription. /// public ServiceClientCredentials Credentials { get; private set; } /// /// Initializes a new instance of the class. /// /// /// HttpClient to be used /// /// /// True: will dispose the provided httpClient on calling Kubernetes.Dispose(). False: will not dispose provided httpClient protected Kubernetes(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClient) { Initialize(); } /// /// Initializes a new instance of the class. /// /// /// Optional. The delegating handlers to add to the http client pipeline. /// protected Kubernetes(params DelegatingHandler[] handlers) : base(handlers) { Initialize(); } /// /// Initializes a new instance of the class. /// /// /// Optional. The http client handler used to handle http transport. /// /// /// Optional. The delegating handlers to add to the http client pipeline. /// protected Kubernetes(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootHandler, handlers) { Initialize(); } /// /// Initializes a new instance of the class. /// /// /// Optional. The base URI of the service. /// /// /// Optional. The delegating handlers to add to the http client pipeline. /// /// /// Thrown when a required parameter is null /// protected Kubernetes(System.Uri baseUri, params DelegatingHandler[] handlers) : this(handlers) { BaseUri = baseUri ?? throw new ArgumentNullException(nameof(baseUri)); } /// /// Initializes a new instance of the class. /// /// /// Optional. The base URI of the service. /// /// /// Optional. The http client handler used to handle http transport. /// /// /// Optional. The delegating handlers to add to the http client pipeline. /// /// /// Thrown when a required parameter is null /// protected Kubernetes(System.Uri baseUri, HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : this(rootHandler, handlers) { BaseUri = baseUri ?? throw new ArgumentNullException(nameof(baseUri)); } /// /// Initializes a new instance of the class. /// /// /// Required. Subscription credentials which uniquely identify client subscription. /// /// /// Optional. The delegating handlers to add to the http client pipeline. /// /// /// Thrown when a required parameter is null /// public Kubernetes(ServiceClientCredentials credentials, params DelegatingHandler[] handlers) : this(handlers) { Credentials = credentials ?? throw new ArgumentNullException(nameof(credentials)); Credentials.InitializeServiceClient(this); } /// /// Initializes a new instance of the class. /// /// /// Required. Subscription credentials which uniquely identify client subscription. /// /// /// HttpClient to be used /// /// /// True: will dispose the provided httpClient on calling Kubernetes.Dispose(). False: will not dispose provided httpClient /// /// Thrown when a required parameter is null /// public Kubernetes(ServiceClientCredentials credentials, HttpClient httpClient, bool disposeHttpClient) : this(httpClient, disposeHttpClient) { Credentials = credentials ?? throw new ArgumentNullException(nameof(credentials)); Credentials.InitializeServiceClient(this); } /// /// Initializes a new instance of the class. /// /// /// Required. Subscription credentials which uniquely identify client subscription. /// /// /// Optional. The http client handler used to handle http transport. /// /// /// Optional. The delegating handlers to add to the http client pipeline. /// /// /// Thrown when a required parameter is null /// public Kubernetes(ServiceClientCredentials credentials, HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : this(rootHandler, handlers) { Credentials = credentials ?? throw new ArgumentNullException(nameof(credentials)); Credentials.InitializeServiceClient(this); } /// /// Initializes a new instance of the class. /// /// /// Optional. The base URI of the service. /// /// /// Required. Subscription credentials which uniquely identify client subscription. /// /// /// Optional. The delegating handlers to add to the http client pipeline. /// /// /// Thrown when a required parameter is null /// public Kubernetes(System.Uri baseUri, ServiceClientCredentials credentials, params DelegatingHandler[] handlers) : this(handlers) { BaseUri = baseUri ?? throw new ArgumentNullException(nameof(baseUri)); Credentials = credentials ?? throw new ArgumentNullException(nameof(credentials)); Credentials.InitializeServiceClient(this); } /// /// Initializes a new instance of the class. /// /// /// Optional. The base URI of the service. /// /// /// Required. Subscription credentials which uniquely identify client subscription. /// /// /// Optional. The http client handler used to handle http transport. /// /// /// Optional. The delegating handlers to add to the http client pipeline. /// /// /// Thrown when a required parameter is null /// public Kubernetes(System.Uri baseUri, ServiceClientCredentials credentials, HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : this(rootHandler, handlers) { BaseUri = baseUri ?? throw new ArgumentNullException(nameof(baseUri)); Credentials = credentials ?? throw new ArgumentNullException(nameof(credentials)); Credentials.InitializeServiceClient(this); } /// /// Initializes client properties. /// private void Initialize() { BaseUri = new System.Uri("http://localhost"); SerializationSettings = new JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.Indented, DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, ContractResolver = new ReadOnlyJsonContractResolver(), Converters = new List { new Iso8601TimeSpanConverter(), }, }; DeserializationSettings = new JsonSerializerSettings { DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, ContractResolver = new ReadOnlyJsonContractResolver(), Converters = new List { new Iso8601TimeSpanConverter(), }, }; CustomInitialize(); } private async Task> CreateResultAsync(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken) { var result = new HttpOperationResponse() { Request = httpRequest, Response = httpResponse }; if (watch == true) { httpResponse.Content = new LineSeparatedHttpContent(httpResponse.Content, cancellationToken); } try { JsonSerializer jsonSerializer = JsonSerializer.Create(DeserializationSettings); jsonSerializer.CheckAdditionalContent = true; #if NET5_0_OR_GREATER using (Stream stream = await httpResponse.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false)) #else using (Stream stream = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false)) #endif using (JsonTextReader reader = new JsonTextReader(new StreamReader(stream))) { result.Body = (T)jsonSerializer.Deserialize(reader, typeof(T)); } } catch (JsonException ex) { httpRequest.Dispose(); httpResponse.Dispose(); throw new SerializationException("Unable to deserialize the response.", ex); } return result; } } }