GithubHelp home page GithubHelp logo

warden-stack / warden.watchers.web Goto Github PK

View Code? Open in Web Editor NEW
3.0 4.0 1.0 19 KB

Warden Web Watcher

Home Page: https://getwarden.net

License: MIT License

C# 99.79% Shell 0.21%
warden watcher web

warden.watchers.web's Introduction

Warden Web Watcher

Warden

OPEN SOURCE & CROSS-PLATFORM TOOL FOR SIMPLIFIED MONITORING

getwarden.net

Branch Build status
master master branch build status
develop develop branch build status

WebWatcher can be used either for simple website monitoring or more advanced API monitoring.

Installation:

Available as a NuGet package.

dotnet add package Warden.Watchers.Web

Configuration:

  • WithRequest() - sets the specific IHttpRequest type (GET, POST, PUT, DELETE), can be also configured via constructor.
  • WithTimeout() - timeout after which the invalid result will be returned.
  • EnsureThat() - predicate containing a received IHttpResponse that has to be met in order to return a valid result.
  • EnsureThatAsync() - async predicate containing a received IHttpResponse that has to be met in order to return a valid result.
  • SkipStatusCodeValidation() - return a valid result even if the HttpStatusCode is not valid (e.g. 400 Bad Request).
  • WithHttpServiceProvider() - provide a custom IHttpService which is responsible for making a request (HttpClient is being used by default).

WebWatcher can be configured by using the WebWatcherConfiguration class or via the lambda expression passed to a specialized constructor.

Example of configuring the watcher via provided configuration class:

//Website monitoring
var websiteConfiguration = WebWatcherConfiguration
    .Create("http://httpstat.us/200")
    .Build();
var websiteWatcher = WebWatcher.Create("Website watcher", websiteConfiguration);

//API monitoring
var apiConfiguration = WebWatcherConfiguration
    .Create("http://my-api.com", HttpRequest.Post("users", new {name = "test"},
        headers: new Dictionary<string, string>
        {
            ["Authorization"] = "Token MyBase64EncodedString",
        }))
    .EnsureThat(response => response.Headers.Any())
    .Build();
var apiWatcher = WebWatcher.Create("API watcher", apiConfiguration);

var wardenConfiguration = WardenConfiguration
    .Create()
    .AddWatcher(websiteWatcher)
    .AddWatcher(apiWatcher)
    //Configure other watchers, hooks etc.

Example of adding the watcher directly to the Warden via one of the extension methods:

var wardenConfiguration = WardenConfiguration
    .Create()
    .AddWebWatcher("http://httpstat.us/200")
    .AddWebWatcher("http://my-api.com", HttpRequest.Post("users", new {name = "test"},
        headers: new Dictionary<string, string>
        {
            ["Authorization"] = "Token MyBase64EncodedString",
        }), 
        cfg => cfg.EnsureThat(response => response.Headers.Any())
    )
    //Configure other watchers, hooks etc.

Please note that you may either use the lambda expression for configuring the watcher or pass the configuration instance directly. You may also configure the hooks by using another lambda expression available in the extension methods.

Check result type:

WebWatcher provides a custom WebWatcherCheckResult type which contains additional values.

public class WebWatcherCheckResult : WatcherCheckResult
{
    public Uri Uri { get; }
    public IHttpRequest Request { get; }
    public IHttpResponse Response { get; }
}

Custom interfaces:

public enum HttpMethod
{
    Get = 1,
    Put = 2,
    Post = 3,
    Delete = 4
}

public interface IHttpRequest
{
    HttpMethod Method { get; }
    string Endpoint { get; }
    object Data { get; }
    IDictionary<string, string> Headers { get; }
}

IHttpRequest represents the HTTP request that has method type, endpoint (e.g. "/users" or an empty one if only the base URL is being used), headers and data object if it's required either for POST or PUT operation.

public interface IHttpResponse
{
    HttpStatusCode StatusCode { get; }
    bool IsValid { get; }
    string ReasonPhrase { get; }
    IDictionary<string, string> Headers { get; }
    string Data { get; }
}

IHttpResponse is defined as the response returned by the web server, which contains basic fields such as StatusCode or ReasonPhrase. If the response has a body (e.g. JSON object), it will be serialized as a string to the Data property.

public interface IHttpService
{
    Task<IHttpResponse> ExecuteAsync(string baseUrl, IHttpRequest request, TimeSpan? timeout = null);
}

IHttpService is responsible for making an HTTP request. It can be configured via the WithHttpServiceProvider() method. By default it is based on the HttpClient.

warden.watchers.web's People

Contributors

spetz avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

daviddesloovere

warden.watchers.web's Issues

HTTPClient Caching (maybe?) inside the WithHTTPServiceProvider

Hey @spetz

Just wondering if you can point me in the right direction on an issue we're seeing, that I'm having trouble getting to the bottom of.

One of our web endpoints requires a bearertoken to be created. We're seeing that after a period of time, the bearertoken expires and so subsequent checks fail (as the watcher already exists with old bearertoken)

So we changed the code to the following using the WithHttpServiceProvider method which I assumed would create a new HttpClient with a new BearerToken for each watch iteration.

This doesn't seem to be working though (it seems like something is still being cached... possibly httpclient pooling maybe) but can't put my finger on it. Any ideas ?

var builder = WebWatcherConfiguration.Create(url);
builder = builder.EnsureThat(resp =>
{
	//check result code
});

builder.WithHttpServiceProvider(() =>
{
	tokenBearer = _authorizationManager
		.Authenticate(Settings.Current.ClientId, Settings.Current.Secret, Settings.Current.TenantId);

	var httpClient = new HttpClient();
	httpClient.DefaultRequestHeaders.Add("Authorization", $"bearer {tokenBearer}");
	return new HttpService(httpClient);
});

watcher = WebWatcher.Create(config.TestName, builder.Build(), config.Group);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.