using System;
using System.Threading.Tasks;
using k8s.Exceptions;
using Microsoft.Rest;
namespace k8s
{
public static class WatcherExt
{
///
/// create a watch object from a call to api server with watch=true
///
/// type of the event object
/// type of the HttpOperationResponse object
/// the api response
/// a callback when any event raised from api server
/// a callbak when any exception was caught during watching
///
/// The action to invoke when the server closes the connection.
///
/// a watch object
public static Watcher Watch(
this Task> responseTask,
Action onEvent,
Action onError = null,
Action onClosed = null)
{
return new Watcher(
async () =>
{
var response = await responseTask.ConfigureAwait(false);
if (!(response.Response.Content is WatcherDelegatingHandler.LineSeparatedHttpContent content))
{
throw new KubernetesClientException("not a watchable request or failed response");
}
return content.StreamReader;
}, onEvent, onError, onClosed);
}
///
/// create a watch object from a call to api server with watch=true
///
/// type of the event object
/// type of the HttpOperationResponse object
/// the api response
/// a callback when any event raised from api server
/// a callbak when any exception was caught during watching
///
/// The action to invoke when the server closes the connection.
///
/// a watch object
public static Watcher Watch(
this HttpOperationResponse response,
Action onEvent,
Action onError = null,
Action onClosed = null)
{
return Watch(Task.FromResult(response), onEvent, onError, onClosed);
}
}
}