Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions src/KubernetesClient.Basic/AbstractKubernetes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,71 @@ namespace k8s;

public abstract partial class AbstractKubernetes
{
private static class HttpMethods
{
public static readonly HttpMethod Delete = HttpMethod.Delete;
public static readonly HttpMethod Get = HttpMethod.Get;
public static readonly HttpMethod Head = HttpMethod.Head;
public static readonly HttpMethod Options = HttpMethod.Options;
public static readonly HttpMethod Post = HttpMethod.Post;
public static readonly HttpMethod Put = HttpMethod.Put;
public static readonly HttpMethod Trace = HttpMethod.Trace;
public static readonly HttpMethod Patch = new HttpMethod("Patch");
}

private sealed class QueryBuilder
{
private List<string> parameters = new List<string>();

public void Append(string key, params object[] values)
{
foreach (var value in values)
{
switch (value)
{
case int intval:
parameters.Add($"{key}={intval}");
break;
case string strval:
parameters.Add($"{key}={Uri.EscapeDataString(strval)}");
break;
case bool boolval:
parameters.Add($"{key}={(boolval ? "true" : "false")}");
break;
default:
// null
break;
}
}
}

public override string ToString()
{
if (parameters.Count > 0)
{
return $"?{string.Join("&", parameters)}";
}

return "";
}
}

private Task<HttpResponseMessage> SendRequest<T>(T body, HttpRequestMessage httpRequest, CancellationToken cancellationToken)
{
if (body != null)
{
var requestContent = KubernetesJson.Serialize(body);
httpRequest.Content = new StringContent(requestContent, System.Text.Encoding.UTF8);
httpRequest.Content.Headers.ContentType = GetHeader(body);
return SendRequestRaw(requestContent, httpRequest, cancellationToken);
}

return SendRequestRaw("", httpRequest, cancellationToken);
}

public virtual TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);

protected internal virtual MediaTypeHeaderValue GetHeader(object body)
protected virtual MediaTypeHeaderValue GetHeader(object body)
{
if (body == null)
{
Expand Down Expand Up @@ -46,9 +106,9 @@ private MediaTypeHeaderValue GetHeader(V1Patch body)
}
}

protected internal abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);
protected abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);

protected internal abstract HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders);
protected abstract HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders);

protected internal abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
protected abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
}
15 changes: 0 additions & 15 deletions src/KubernetesClient.Basic/HttpMethods.cs

This file was deleted.

43 changes: 0 additions & 43 deletions src/KubernetesClient.Basic/Operations.cs

This file was deleted.

38 changes: 0 additions & 38 deletions src/KubernetesClient.Basic/QueryBuilder.cs

This file was deleted.

6 changes: 3 additions & 3 deletions src/KubernetesClient/Kubernetes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void Initialize()
BaseUri = new Uri("http://localhost");
}

protected internal override async Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken)
protected override async Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken)
{
if (httpRequest == null)
{
Expand Down Expand Up @@ -99,7 +99,7 @@ protected internal override async Task<HttpOperationResponse<T>> CreateResultAsy
return result;
}

protected internal override HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders)
protected override HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders)
{
var httpRequest = new HttpRequestMessage();
httpRequest.Method = method;
Expand All @@ -120,7 +120,7 @@ protected internal override HttpRequestMessage CreateRequest(string relativeUri,
return httpRequest;
}

protected internal override async Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken)
protected override async Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken)
{
if (httpRequest == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ namespace k8s;
public abstract partial class AbstractKubernetes
{
{{#.}}
public I{{.}}Operations {{.}} => new {{.}}Operations(this);
public I{{.}}Operations {{.}} => this;
{{/.}}
}
16 changes: 5 additions & 11 deletions src/LibKubernetesGenerator/templates/Operations.cs.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@

namespace k8s;

internal partial class {{name}}Operations : Operations, I{{name}}Operations
public partial class AbstractKubernetes : I{{name}}Operations
{

public {{name}}Operations(AbstractKubernetes kubernetes) : base(kubernetes)
{
}


{{#apis}}
/// <inheritdoc/>
public async Task<HttpOperationResponse{{GetReturnType operation "<>"}}> {{GetMethodName operation "WithHttpMessagesAsync"}}(
async Task<HttpOperationResponse{{GetReturnType operation "<>"}}> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName . "true"}},
{{GetDotNetType .}} {{GetDotNetName .}},
{{/operation.parameters}}
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders = null,
CancellationToken cancellationToken = default(CancellationToken))
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders,
CancellationToken cancellationToken)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(HttpClientTimeout);
Expand Down