Files
csharp/gen/KubernetesGenerator/templates/Kubernetes.cs.template

185 lines
7.9 KiB
Plaintext
Raw Normal View History

// <auto-generated>
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
namespace k8s
{
using Microsoft.Rest;
using Microsoft.Rest.Serialization;
using Models;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
public partial class Kubernetes : ServiceClient<Kubernetes>, IKubernetes
{
{{#.}}
/// <inheritdoc/>
public async Task<HttpOperationResponse{{GetReturnType operation "<>"}}> {{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName . "true"}},
{{/operation.parameters}}
Dictionary<string, List<string>> customHeaders = null,
CancellationToken cancellationToken = default(CancellationToken))
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(HttpClientTimeout);
{{#IfParamContains operation "watch"}}
if (watch == true)
{
cts.CancelAfter(Timeout.InfiniteTimeSpan);
}
{{/IfParamContains operation "watch"}}
cancellationToken = cts.Token;
{{#operation.parameters}}
{{#isRequired}}
if ({{GetDotNetName name}} == null)
{
throw new ValidationException(ValidationRules.CannotBeNull, "{{GetDotNetName name}}");
}
{{/isRequired}}
{{/operation.parameters}}
// Tracing
bool _shouldTrace = ServiceClientTracing.IsEnabled;
string _invocationId = null;
if (_shouldTrace)
{
_invocationId = ServiceClientTracing.NextInvocationId.ToString();
Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
{{#operation.parameters}}
tracingParameters.Add("{{GetDotNetName name}}", {{GetDotNetName name}});
{{/operation.parameters}}
tracingParameters.Add("cancellationToken", cancellationToken);
ServiceClientTracing.Enter(_invocationId, this, "{{GetMethodName operation ""}}", tracingParameters);
}
// Construct URL
var _baseUrl = BaseUri.AbsoluteUri;
var _url = new System.Uri(new System.Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "{{path}}").ToString();
{{#operation.parameters}}
{{#IfKindIs . "path"}}
_url = _url.Replace("{{AddCurly name}}", {{GetDotNetName name}});
{{/IfKindIs . "path"}}
{{/operation.parameters}}
_url = _url.Replace("/apis//", "/api/");
List<string> _queryParameters = new List<string>();
{{#operation.parameters}}
{{#IfKindIs . "query"}}
if ({{GetDotNetName name}} != null)
{
_queryParameters.Add(string.Format("{{name}}={0}", {{EscapeDataString name type}}));
}
{{/IfKindIs . "query"}}
{{/operation.parameters}}
if (_queryParameters.Count > 0)
{
_url += "?" + string.Join("&", _queryParameters);
}
// Create HTTP transport
var _httpRequest = new HttpRequestMessage();
HttpResponseMessage _httpResponse = null;
_httpRequest.Method = HttpMethod.{{Method}};
_httpRequest.RequestUri = new System.Uri(_url);
_httpRequest.Version = HttpVersion.Version20;
// Set Headers
if (customHeaders != null)
{
foreach(var _header in customHeaders)
{
_httpRequest.Headers.Remove(_header.Key);
_httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
}
}
// Serialize Request
string _requestContent = null;
{{#IfParamContains operation "body"}}
if(body != null)
{
_requestContent = SafeJsonConvert.SerializeObject(body, SerializationSettings);
_httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
_httpRequest.Content.Headers.ContentType = GetHeader(body);
}
{{/IfParamContains operation "body"}}
// Set Credentials
if (Credentials != null)
{
cancellationToken.ThrowIfCancellationRequested();
await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
}
// Send Request
if (_shouldTrace)
{
ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
}
cancellationToken.ThrowIfCancellationRequested();
_httpResponse = await HttpClient.SendAsync(_httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
if (_shouldTrace)
{
ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
}
HttpStatusCode _statusCode = _httpResponse.StatusCode;
cancellationToken.ThrowIfCancellationRequested();
if ((int)_statusCode != 200 && (int)_statusCode != 201 && (int)_statusCode != 202)
{
string _responseContent = null;
var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
if (_httpResponse.Content != null) {
_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
}
else {
_responseContent = string.Empty;
}
ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
if (_shouldTrace)
{
ServiceClientTracing.Error(_invocationId, ex);
}
_httpRequest.Dispose();
if (_httpResponse != null)
{
_httpResponse.Dispose();
}
throw ex;
}
// Create Result
{{#IfReturnType operation "void"}}
HttpOperationResponse _result = new HttpOperationResponse() { Request = _httpRequest, Response = _httpResponse };
{{/IfReturnType operation "void"}}
{{#IfReturnType operation "obj"}}
var _result = await CreateResultAsync{{GetReturnType operation "<>"}}(_httpRequest,
_httpResponse,
{{#IfParamContains operation "watch"}}
watch,
{{/IfParamContains operation "watch"}}
{{#IfParamDoesNotContain operation "watch"}}
false,
{{/IfParamDoesNotContain operation "watch"}}
cancellationToken);
{{/IfReturnType operation "obj"}}
{{#IfReturnType operation "stream"}}
var _result = new HttpOperationResponse{{GetReturnType operation "<>"}}() {
Request = _httpRequest,
Response = _httpResponse,
Body = await _httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false) };
{{/IfReturnType operation "stream"}}
if (_shouldTrace)
{
ServiceClientTracing.Exit(_invocationId, _result);
}
return _result;
}
{{/.}}
}
}