diff --git a/src/Sentry.Examples.Console/Sentry.Examples.Console.csproj b/src/Sentry.Examples.Console/Sentry.Examples.Console.csproj index 11f471e..65f467c 100644 --- a/src/Sentry.Examples.Console/Sentry.Examples.Console.csproj +++ b/src/Sentry.Examples.Console/Sentry.Examples.Console.csproj @@ -52,6 +52,7 @@ + diff --git a/src/Sentry.Examples.WindowsService/Sentry.Examples.WindowsService.csproj b/src/Sentry.Examples.WindowsService/Sentry.Examples.WindowsService.csproj index 9d97204..dd22fb6 100644 --- a/src/Sentry.Examples.WindowsService/Sentry.Examples.WindowsService.csproj +++ b/src/Sentry.Examples.WindowsService/Sentry.Examples.WindowsService.csproj @@ -52,6 +52,7 @@ + diff --git a/src/Sentry.Watchers.Api/ApiWatcher.cs b/src/Sentry.Watchers.Api/ApiWatcher.cs index 8a192ee..b45c0fc 100644 --- a/src/Sentry.Watchers.Api/ApiWatcher.cs +++ b/src/Sentry.Watchers.Api/ApiWatcher.cs @@ -92,7 +92,7 @@ private void SetRequestHeaders() } private bool HasValidResponse(HttpResponseMessage response) - => (_configuration.WhereValidResponseIs?.Invoke(response) ?? true) && + => (_configuration.EnsureThat?.Invoke(response) ?? true) && (response.IsSuccessStatusCode || _configuration.SkipStatusCodeValidation); public static ApiWatcher Create(string name, string url, HttpRequest request, Action configuration = null) diff --git a/src/Sentry.Watchers.Api/ApiWatcherConfiguration.cs b/src/Sentry.Watchers.Api/ApiWatcherConfiguration.cs index 11e17bb..2e6b7e1 100644 --- a/src/Sentry.Watchers.Api/ApiWatcherConfiguration.cs +++ b/src/Sentry.Watchers.Api/ApiWatcherConfiguration.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net.Http; +using System.Threading.Tasks; namespace Sentry.Watchers.Api { @@ -11,7 +12,8 @@ public class ApiWatcherConfiguration public bool SkipStatusCodeValidation { get; protected set; } public IDictionary Headers { get; protected set; } public TimeSpan Timeout { get; protected set; } - public Func WhereValidResponseIs { get; protected set; } + public Func EnsureThat { get; protected set; } + public Func> EnsureThatAsync { get; protected set; } protected internal ApiWatcherConfiguration(string url, HttpRequest request) { @@ -88,9 +90,22 @@ public Builder SkipStatusCodeValidation() return this; } - public Builder EnsureThat(Func predicate) + public Builder EnsureThat(Func ensureThat) { - _configuration.WhereValidResponseIs = predicate; + if (ensureThat == null) + throw new ArgumentException("Ensure that predicate can not be null.", nameof(ensureThat)); + + _configuration.EnsureThat = ensureThat; + + return this; + } + + public Builder EnsureThatAsync(Func> ensureThat) + { + if (ensureThat == null) + throw new ArgumentException("Ensure that async predicate can not be null.", nameof(ensureThat)); + + _configuration.EnsureThatAsync = ensureThat; return this; } diff --git a/src/Sentry.Watchers.Api/HttpRequest.cs b/src/Sentry.Watchers.Api/HttpRequest.cs index e2e5ecc..d4e1c02 100644 --- a/src/Sentry.Watchers.Api/HttpRequest.cs +++ b/src/Sentry.Watchers.Api/HttpRequest.cs @@ -1,6 +1,4 @@ -using System; - -namespace Sentry.Watchers.Api +namespace Sentry.Watchers.Api { public class HttpRequest { @@ -10,17 +8,19 @@ public class HttpRequest protected HttpRequest(MethodType type, string endpoint, object data = null) { - if(string.IsNullOrWhiteSpace(endpoint)) - throw new ArgumentException("Endpoint can not be empty.", nameof(endpoint)); Type = type; Endpoint = endpoint; Data = data; } + public static HttpRequest Get() => new HttpRequest(MethodType.Get, string.Empty); public static HttpRequest Get(string endpoint) => new HttpRequest(MethodType.Get, endpoint); + public static HttpRequest Put(object data) => new HttpRequest(MethodType.Put, string.Empty, data); public static HttpRequest Put(string endpoint, object data = null) => new HttpRequest(MethodType.Put, endpoint, data); + public static HttpRequest Post(object data) => new HttpRequest(MethodType.Post, string.Empty, data); public static HttpRequest Post(string endpoint, object data = null) => new HttpRequest(MethodType.Post, endpoint, data); + public static HttpRequest Delete() => new HttpRequest(MethodType.Delete, string.Empty); public static HttpRequest Delete(string endpoint) => new HttpRequest(MethodType.Delete, endpoint); public enum MethodType