Skip to content

Commit d2b4370

Browse files
committed
refactor: rename GetParameterValueForWatchCall to GetParameterValueForWatch and update templates
1 parent cab4f39 commit d2b4370

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/LibKubernetesGenerator/ParamHelper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Scriban.Runtime;
44
using System;
55
using System.Linq;
6+
using System.Collections.Generic;
67

78
namespace LibKubernetesGenerator
89
{
@@ -22,7 +23,7 @@ public void RegisterHelper(ScriptObject scriptObject)
2223
scriptObject.Import(nameof(GetModelCtorParam), new Func<JsonSchema, string>(GetModelCtorParam));
2324
scriptObject.Import(nameof(IfParamContains), IfParamContains);
2425
scriptObject.Import(nameof(FilterParameters), FilterParameters);
25-
scriptObject.Import(nameof(GetParameterValueForWatchCall), new Func<OpenApiParameter, string>(GetParameterValueForWatchCall));
26+
scriptObject.Import(nameof(GetParameterValueForWatch), new Func<OpenApiParameter, string>(GetParameterValueForWatch));
2627
}
2728

2829
public static bool IfParamContains(OpenApiOperation operation, string name)
@@ -41,12 +42,12 @@ public static bool IfParamContains(OpenApiOperation operation, string name)
4142
return found;
4243
}
4344

44-
public static System.Collections.Generic.IEnumerable<OpenApiParameter> FilterParameters(OpenApiOperation operation, string excludeParam)
45+
public static IEnumerable<OpenApiParameter> FilterParameters(OpenApiOperation operation, string excludeParam)
4546
{
4647
return operation.Parameters.Where(p => p.Name != excludeParam);
4748
}
4849

49-
public string GetParameterValueForWatchCall(OpenApiParameter parameter)
50+
public string GetParameterValueForWatch(OpenApiParameter parameter)
5051
{
5152
if (parameter.Name == "watch")
5253
{

src/LibKubernetesGenerator/templates/Client.cs.template

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,69 @@ public partial class {{name}}Client : ResourceClient
9696
}
9797
}
9898
{{end}}
99+
100+
{{if IfParamContains api.operation "watch"}}
101+
{{~ $filteredParams = FilterParameters api.operation "watch" ~}}
102+
/// <summary>
103+
/// Watch {{ToXmlDoc api.operation.description}}
104+
/// </summary>
105+
{{ for parameter in $filteredParams}}
106+
/// <param name="{{GetDotNetNameOpenApiParameter parameter "false"}}">
107+
/// {{ToXmlDoc parameter.description}}
108+
/// </param>
109+
{{ end }}
110+
/// <param name="onEvent">Callback when any event raised from api server</param>
111+
/// <param name="onError">Callback when any exception was caught during watching</param>
112+
/// <param name="onClosed">Callback when the server closes the connection</param>
113+
public Watcher<{{GetReturnType api.operation "T"}}> Watch{{GetActionName api.operation name ""}}(
114+
{{ for parameter in $filteredParams}}
115+
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}},
116+
{{ end }}
117+
Action<WatchEventType, {{GetReturnType api.operation "T"}}> onEvent = null,
118+
Action<Exception> onError = null,
119+
Action onClosed = null)
120+
{
121+
if (onEvent == null) throw new ArgumentNullException(nameof(onEvent));
122+
123+
var responseTask = Client.{{group}}.{{GetOperationId api.operation "WithHttpMessagesAsync"}}(
124+
{{ for parameter in api.operation.parameters}}
125+
{{GetParameterValueForWatch parameter}},
126+
{{ end }}
127+
null,
128+
CancellationToken.None);
129+
130+
return responseTask.Watch<{{GetReturnType api.operation "T"}}, {{GetReturnType api.operation "TList"}}>(
131+
onEvent, onError, onClosed);
132+
}
133+
134+
/// <summary>
135+
/// Watch {{ToXmlDoc api.operation.description}} as async enumerable
136+
/// </summary>
137+
{{ for parameter in $filteredParams}}
138+
/// <param name="{{GetDotNetNameOpenApiParameter parameter "false"}}">
139+
/// {{ToXmlDoc parameter.description}}
140+
/// </param>
141+
{{ end }}
142+
/// <param name="onError">Callback when any exception was caught during watching</param>
143+
/// <param name="cancellationToken">Cancellation token</param>
144+
public IAsyncEnumerable<(WatchEventType, {{GetReturnType api.operation "T"}})> Watch{{GetActionName api.operation name "Async"}}(
145+
{{ for parameter in $filteredParams}}
146+
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}},
147+
{{ end }}
148+
Action<Exception> onError = null,
149+
CancellationToken cancellationToken = default)
150+
{
151+
var responseTask = Client.{{group}}.{{GetOperationId api.operation "WithHttpMessagesAsync"}}(
152+
{{ for parameter in api.operation.parameters}}
153+
{{GetParameterValueForWatch parameter}},
154+
{{ end }}
155+
null,
156+
cancellationToken);
157+
158+
return responseTask.WatchAsync<{{GetReturnType api.operation "T"}}, {{GetReturnType api.operation "TList"}}>(
159+
onError, cancellationToken);
160+
}
99161
{{end}}
162+
163+
{{end}}
100164
}

src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static Watcher<{{GetReturnType api.operation "T"}}> Watch{{GetOperationId
183183

184184
var responseTask = operations.{{GetOperationId api.operation "WithHttpMessagesAsync"}}(
185185
{{ for parameter in api.operation.parameters}}
186-
{{GetParameterValueForWatchCall parameter}},
186+
{{GetParameterValueForWatch parameter}},
187187
{{end}}
188188
null,
189189
CancellationToken.None);
@@ -215,7 +215,7 @@ public static IAsyncEnumerable<(WatchEventType, {{GetReturnType api.operation "T
215215
{
216216
var responseTask = operations.{{GetOperationId api.operation "WithHttpMessagesAsync"}}(
217217
{{ for parameter in api.operation.parameters}}
218-
{{GetParameterValueForWatchCall parameter}},
218+
{{GetParameterValueForWatch parameter}},
219219
{{end}}
220220
null,
221221
cancellationToken);

0 commit comments

Comments
 (0)