diff --git a/src/KubernetesClient.Models/KubernetesJson.cs b/src/KubernetesClient.Models/KubernetesJson.cs index 70709c6b7..eef374ce9 100644 --- a/src/KubernetesClient.Models/KubernetesJson.cs +++ b/src/KubernetesClient.Models/KubernetesJson.cs @@ -92,7 +92,6 @@ public static TValue Deserialize(Stream json, JsonSerializerOptions json return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); } - public static string Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null) { return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); diff --git a/src/KubernetesClient/GenericClient.cs b/src/KubernetesClient/GenericClient.cs index 1b5cb6ead..beb5c55ba 100644 --- a/src/KubernetesClient/GenericClient.cs +++ b/src/KubernetesClient/GenericClient.cs @@ -36,112 +36,112 @@ public GenericClient(IKubernetes kubernetes, string group, string version, strin public async Task CreateAsync(T obj, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.CreateClusterCustomObjectWithHttpMessagesAsync(obj, group, version, plural, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.CreateClusterCustomObjectWithHttpMessagesAsync(obj, group, version, plural, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task CreateNamespacedAsync(T obj, string ns, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(obj, group, version, ns, plural, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(obj, group, version, ns, plural, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task ListAsync(CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(group, version, plural, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(group, version, plural, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task ListNamespacedAsync(string ns, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.ListNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.ListNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task ReadNamespacedAsync(string ns, string name, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.GetNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.GetNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task ReadAsync(string name, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.GetClusterCustomObjectWithHttpMessagesAsync(group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.GetClusterCustomObjectWithHttpMessagesAsync(group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task DeleteAsync(string name, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.DeleteClusterCustomObjectWithHttpMessagesAsync(group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.DeleteClusterCustomObjectWithHttpMessagesAsync(group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task DeleteNamespacedAsync(string ns, string name, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.DeleteNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.DeleteNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task PatchAsync(V1Patch patch, string name, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.PatchClusterCustomObjectWithHttpMessagesAsync(patch, group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.PatchClusterCustomObjectWithHttpMessagesAsync(patch, group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task PatchNamespacedAsync(V1Patch patch, string ns, string name, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.PatchNamespacedCustomObjectWithHttpMessagesAsync(patch, group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.PatchNamespacedCustomObjectWithHttpMessagesAsync(patch, group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task ReplaceAsync(T obj, string name, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.ReplaceClusterCustomObjectWithHttpMessagesAsync(obj, group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.ReplaceClusterCustomObjectWithHttpMessagesAsync(obj, group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public async Task ReplaceNamespacedAsync(T obj, string ns, string name, CancellationToken cancel = default) where T : IKubernetesObject { - var resp = await kubernetes.CustomObjects.ReplaceNamespacedCustomObjectWithHttpMessagesAsync(obj, group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false); - return KubernetesJson.Deserialize(resp.Body.ToString()); + var resp = await kubernetes.CustomObjects.ReplaceNamespacedCustomObjectWithHttpMessagesAsync(obj, group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false); + return resp.Body; } public IAsyncEnumerable<(WatchEventType, T)> WatchAsync(Action onError = null, CancellationToken cancel = default) where T : IKubernetesObject { - var respTask = kubernetes.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(group, version, plural, watch: true, cancellationToken: cancel); - return respTask.WatchAsync(); + var respTask = kubernetes.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(group, version, plural, watch: true, cancellationToken: cancel); + return respTask.WatchAsync(); } public IAsyncEnumerable<(WatchEventType, T)> WatchNamespacedAsync(string ns, Action onError = null, CancellationToken cancel = default) where T : IKubernetesObject { - var respTask = kubernetes.CustomObjects.ListNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, watch: true, cancellationToken: cancel); - return respTask.WatchAsync(); + var respTask = kubernetes.CustomObjects.ListNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, watch: true, cancellationToken: cancel); + return respTask.WatchAsync(); } public Watcher Watch(Action onEvent, Action onError = null, Action onClosed = null) where T : IKubernetesObject { - var respTask = kubernetes.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(group, version, plural, watch: true); + var respTask = kubernetes.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(group, version, plural, watch: true); return respTask.Watch(onEvent, onError, onClosed); } public Watcher WatchNamespaced(string ns, Action onEvent, Action onError = null, Action onClosed = null) where T : IKubernetesObject { - var respTask = kubernetes.CustomObjects.ListNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, watch: true); + var respTask = kubernetes.CustomObjects.ListNamespacedCustomObjectWithHttpMessagesAsync(group, version, ns, plural, watch: true); return respTask.Watch(onEvent, onError, onClosed); } diff --git a/src/LibKubernetesGenerator/templates/IOperations.cs.template b/src/LibKubernetesGenerator/templates/IOperations.cs.template index 594b92cb6..ee3926a2e 100644 --- a/src/LibKubernetesGenerator/templates/IOperations.cs.template +++ b/src/LibKubernetesGenerator/templates/IOperations.cs.template @@ -32,5 +32,28 @@ public partial interface I{{name}}Operations IReadOnlyDictionary> customHeaders = null, CancellationToken cancellationToken = default); + {{#IfReturnType operation "object"}} + /// + /// {{ToXmlDoc operation.description}} + /// + {{#operation.parameters}} + /// + /// {{ToXmlDoc description}} + /// + {{/operation.parameters}} + /// + /// The headers that will be added to request. + /// + /// + /// A which can be used to cancel the asynchronous operation. + /// + Task> {{GetMethodName operation "WithHttpMessagesAsync"}}( +{{#operation.parameters}} + {{GetDotNetType .}} {{GetDotNetName . "true"}}, +{{/operation.parameters}} + IReadOnlyDictionary> customHeaders = null, + CancellationToken cancellationToken = default); + {{/IfReturnType operation "object"}} + {{/apis}} } diff --git a/src/LibKubernetesGenerator/templates/Operations.cs.template b/src/LibKubernetesGenerator/templates/Operations.cs.template index 94ad962b9..86d7d22ac 100644 --- a/src/LibKubernetesGenerator/templates/Operations.cs.template +++ b/src/LibKubernetesGenerator/templates/Operations.cs.template @@ -9,8 +9,15 @@ namespace k8s; public partial class AbstractKubernetes : I{{name}}Operations { {{#apis}} - /// - async Task"}}> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}( + {{#IfReturnType operation "void"}} + private async Task I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( + {{/IfReturnType operation "void"}} + {{#IfReturnType operation "obj"}} + private async Task> I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( + {{/IfReturnType operation "obj"}} + {{#IfReturnType operation "stream"}} + private async Task> I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( + {{/IfReturnType operation "stream"}} {{#operation.parameters}} {{GetDotNetType .}} {{GetDotNetName .}}, {{/operation.parameters}} @@ -64,7 +71,7 @@ public partial class AbstractKubernetes : I{{name}}Operations HttpOperationResponse result = new HttpOperationResponse() { Request = httpRequest, Response = httpResponse }; {{/IfReturnType operation "void"}} {{#IfReturnType operation "obj"}} - var result = await CreateResultAsync{{GetReturnType operation "<>"}}( + var result = await CreateResultAsync( httpRequest, httpResponse, {{#IfParamContains operation "watch"}} @@ -76,12 +83,64 @@ public partial class AbstractKubernetes : I{{name}}Operations cancellationToken); {{/IfReturnType operation "obj"}} {{#IfReturnType operation "stream"}} - var result = new HttpOperationResponse{{GetReturnType operation "<>"}}() { + var result = new HttpOperationResponse() { Request = httpRequest, Response = httpResponse, Body = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false) }; {{/IfReturnType operation "stream"}} return result; } + + /// + async Task"}}> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}( +{{#operation.parameters}} + {{GetDotNetType .}} {{GetDotNetName .}}, +{{/operation.parameters}} + IReadOnlyDictionary> customHeaders, + CancellationToken cancellationToken) + { + {{#IfReturnType operation "void"}} + return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( + {{#operation.parameters}} + {{GetDotNetName .}}, + {{/operation.parameters}} + customHeaders, + cancellationToken).ConfigureAwait(false); + {{/IfReturnType operation "void"}} + {{#IfReturnType operation "obj"}} + return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}{{GetReturnType operation "<>"}}( + {{#operation.parameters}} + {{GetDotNetName .}}, + {{/operation.parameters}} + customHeaders, + cancellationToken).ConfigureAwait(false); + {{/IfReturnType operation "obj"}} + {{#IfReturnType operation "stream"}} + return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( + {{#operation.parameters}} + {{GetDotNetName .}}, + {{/operation.parameters}} + customHeaders, + cancellationToken).ConfigureAwait(false); + {{/IfReturnType operation "stream"}} + } + + {{#IfReturnType operation "object"}} + /// + async Task> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}( +{{#operation.parameters}} + {{GetDotNetType .}} {{GetDotNetName .}}, +{{/operation.parameters}} + IReadOnlyDictionary> customHeaders, + CancellationToken cancellationToken) + { + return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( + {{#operation.parameters}} + {{GetDotNetName .}}, + {{/operation.parameters}} + customHeaders, + cancellationToken).ConfigureAwait(false); + } + {{/IfReturnType operation "object"}} {{/apis}} } diff --git a/src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template b/src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template index 0374990af..f19500723 100644 --- a/src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template +++ b/src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template @@ -36,7 +36,35 @@ public static partial class {{name}}OperationsExtensions {{/operation.parameters}} CancellationToken.None ).GetAwaiter().GetResult(); - } + } + + {{#IfReturnType operation "object"}} + /// + /// {{ToXmlDoc operation.description}} + /// + /// + /// The operations group for this extension method. + /// + {{#operation.parameters}} + /// + /// {{ToXmlDoc description}} + /// + {{/operation.parameters}} + public static T {{GetMethodName operation ""}}( + this I{{name}}Operations operations +{{#operation.parameters}} + ,{{GetDotNetType .}} {{GetDotNetName . "true"}} +{{/operation.parameters}} + ) + { + return operations.{{GetMethodName operation "Async"}}( +{{#operation.parameters}} + {{GetDotNetName .}}, +{{/operation.parameters}} + CancellationToken.None + ).GetAwaiter().GetResult(); + } + {{/IfReturnType operation "object"}} /// /// {{ToXmlDoc operation.description}} @@ -92,5 +120,39 @@ public static partial class {{name}}OperationsExtensions {{/IfReturnType operation "void"}} } + {{#IfReturnType operation "object"}} + /// + /// {{ToXmlDoc operation.description}} + /// + /// + /// The operations group for this extension method. + /// + {{#operation.parameters}} + /// + /// {{ToXmlDoc description}} + /// + {{/operation.parameters}} + /// + /// A which can be used to cancel the asynchronous operation. + /// + public static async Task {{GetMethodName operation "Async"}}( + this I{{name}}Operations operations, + {{#operation.parameters}} + {{GetDotNetType .}} {{GetDotNetName . "true"}}, + {{/operation.parameters}} + CancellationToken cancellationToken = default(CancellationToken)) + { + using (var _result = await operations.{{GetMethodName operation "WithHttpMessagesAsync"}}( + {{#operation.parameters}} + {{GetDotNetName .}}, + {{/operation.parameters}} + null, + cancellationToken).ConfigureAwait(false)) + { + return _result.Body; + } + } + {{/IfReturnType}} + {{/apis}} }