Skip to content

Commit

Permalink
Imrpoved HTTP methods for API watcher #16.
Browse files Browse the repository at this point in the history
  • Loading branch information
spetz committed Mar 17, 2016
1 parent 1438ffd commit 0c86796
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Sentry.Examples.Console/Sentry.Examples.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.Watchers.Api/ApiWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiWatcherConfiguration.Builder> configuration = null)
Expand Down
21 changes: 18 additions & 3 deletions src/Sentry.Watchers.Api/ApiWatcherConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Sentry.Watchers.Api
{
Expand All @@ -11,7 +12,8 @@ public class ApiWatcherConfiguration
public bool SkipStatusCodeValidation { get; protected set; }
public IDictionary<string, string> Headers { get; protected set; }
public TimeSpan Timeout { get; protected set; }
public Func<HttpResponseMessage, bool> WhereValidResponseIs { get; protected set; }
public Func<HttpResponseMessage, bool> EnsureThat { get; protected set; }
public Func<HttpResponseMessage, Task<bool>> EnsureThatAsync { get; protected set; }

protected internal ApiWatcherConfiguration(string url, HttpRequest request)
{
Expand Down Expand Up @@ -88,9 +90,22 @@ public Builder SkipStatusCodeValidation()
return this;
}

public Builder EnsureThat(Func<HttpResponseMessage, bool> predicate)
public Builder EnsureThat(Func<HttpResponseMessage, bool> 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<HttpResponseMessage, Task<bool>> ensureThat)
{
if (ensureThat == null)
throw new ArgumentException("Ensure that async predicate can not be null.", nameof(ensureThat));

_configuration.EnsureThatAsync = ensureThat;

return this;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Sentry.Watchers.Api/HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Sentry.Watchers.Api
namespace Sentry.Watchers.Api
{
public class HttpRequest
{
Expand All @@ -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
Expand Down

0 comments on commit 0c86796

Please sign in to comment.