Skip to content

Commit 53b3ed6

Browse files
committed
Add a WatchAsync method.
1 parent e00f67a commit 53b3ed6

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

examples/watch/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ private static void Main(string[] args)
1313

1414
IKubernetes client = new Kubernetes(config);
1515

16-
var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true).Result;
17-
using (podlistResp.Watch<V1Pod>((type, item) =>
16+
var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
17+
using (podlistResp.WatchAsync<V1Pod, V1PodList>((type, item) =>
1818
{
1919
Console.WriteLine("==on watch event==");
2020
Console.WriteLine(type);

src/KubernetesClient/Watcher.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,22 @@ public static Watcher<T> Watch<T>(this HttpOperationResponse<T> response,
185185
{
186186
return Watch((HttpOperationResponse)response, onEvent, onError, onClosed);
187187
}
188+
189+
public static async Task<Watcher<T>> WatchAsync<T, L>(this Task<HttpOperationResponse<L>> responseTask,
190+
Action<WatchEventType, T> onEvent,
191+
Action<Exception> onError = null,
192+
Action onClosed = null)
193+
{
194+
var response = await responseTask;
195+
196+
if (!(response.Response.Content is WatcherDelegatingHandler.LineSeparatedHttpContent content))
197+
{
198+
throw new KubernetesClientException("not a watchable request or failed response");
199+
}
200+
201+
return new Watcher<T>(content.StreamReader, onEvent, onError, onClosed);
202+
}
203+
204+
188205
}
189206
}

0 commit comments

Comments
 (0)