PortiaNet.HealthCheck.Writer.HTTP 1.3.0

dotnet add package PortiaNet.HealthCheck.Writer.HTTP --version 1.3.0                
NuGet\Install-Package PortiaNet.HealthCheck.Writer.HTTP -Version 1.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PortiaNet.HealthCheck.Writer.HTTP" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PortiaNet.HealthCheck.Writer.HTTP --version 1.3.0                
#r "nuget: PortiaNet.HealthCheck.Writer.HTTP, 1.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PortiaNet.HealthCheck.Writer.HTTP as a Cake Addin
#addin nuget:?package=PortiaNet.HealthCheck.Writer.HTTP&version=1.3.0

// Install PortiaNet.HealthCheck.Writer.HTTP as a Cake Tool
#tool nuget:?package=PortiaNet.HealthCheck.Writer.HTTP&version=1.3.0                

<img src="https://github.com/PortiaNet/HealthCheck.Reporter/blob/master/Assets/banner.png" width="400">

Nuget

This library is the writer for the PortiaNet.HealthCheck.Reporter and pushes the API call reports to a specific URL using HTTP methods.

Installation

You can install this tool from Nuget using Package Manager Console:

PM> Install-Package PortiaNet.HealthCheck.Writer.HTTP

How do I get started?

After adding the HealthCheck middleware to the application pipeline, you need to add this library to DI and configure it as you prefer.

using PortiaNet.HealthCheck.Writer;

var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddHTTPWriter(options =>
{
    options.ListenerAddress = new Uri("Target API to push the report");
    options.MuteOnError = false;
    options.AuthenticationType = AuthenticationType.None;
    options.NodeName = "Main Node without authentication";
});

builder.Services.AddHTTPWriter(options =>
{
    options.ListenerAddress = new Uri("Target API to push the report");
    options.MuteOnError = false;
    options.AuthenticationType = AuthenticationType.StaticBearerToken;
    options.NodeName = "Main Node by Static Bearer Token";
    options.AuthenticationConfig = new StaticBearerTokenAuthentication
    {
        Token = "A very hard and long super secret token!!!"
    };
});

builder.Services.AddHTTPWriter(options =>
{
    options.ListenerAddress = new Uri("Target API to push the report");
    options.MuteOnError = false;
    options.AuthenticationType = AuthenticationType.ClientSecretBearerToken;
    options.NodeName = "Main Node by ClientSecret";
    options.AuthenticationConfig = new ClientSecretBearerTokenAuthentication
    {
        AuthenticationAPIPath = new Uri("Authentication API"),
        ClientSecret = "***ClientSecretText&&&"
    };
});

builder.Services.AddHTTPWriter(options =>
{
    options.ListenerAddress = new Uri("Target API to push the report");
    options.MuteOnError = false;
    options.AuthenticationType = AuthenticationType.UsernamePasswordBearerToken;
    options.NodeName = "Main Node by Username and Password";
    options.AuthenticationConfig = new UsernamePasswordBearerTokenAuthentication
    {
        AuthenticationAPIPath = new Uri("Authentication API"),
        Username = "TestUser",
        Password = "P@ssvor3d"
    };
});
...

This library supports 4 types of communication with the target service:

  1. Without Authentication

In the first method, the library posts the report to the target API without setting the Authorization header.

  1. Static Bearer Token Authentication (Doesn't need to call authentication API)

In the second method, the library sets the Authorization header with the Bearer prefix and the exact value of the specified token for all API calls.

  1. Client Secret Authentication

In the third method, the library posts the specified Client Secret to the target authentication API to receive the bearer token. after receiving the token, it sets the Authorization header with the Bearer prefix and the exact value of the received token for all API calls. In this case, you need to have a POST API endpoint to receive the Client Secret from the request body and send the token back as the method result with a 200 status code.

[HttpPost]
public IActionResult AuthenticateByClientSecret([FromBody]string clientSecret)
{
    if(string.IsNullOrEmpty(clientSecret))
        return Unauthorized();

    return Ok("Bearer Token");
}
  1. Username and Password Authentication

In the fourth method, the library posts the specified Username and Password to the target authentication API to receive the bearer token. after receiving the token, it sets the Authorization header with the Bearer prefix and the exact value of the received token for all API calls. In this case, you need to have a POST API endpoint to receive the credentials from the request body and send the token back as the method result with a 200 status code.

public class UsernamePasswordModel
{
    public string? Username { get; set; }

    public string? Password { get; set; }
}

[HttpPost]
public IActionResult AuthenticateByUsernamePassword([FromBody] UsernamePasswordModel credential)
{
    if (credential == null || credential.Username != "Username" && credential.Password != "Password")
        return Unauthorized();

    return Ok("Bearer Token");
}

After specifying the authentication method, you need to configure the target API to receive and save the reports. This API should be a POST method and receives the Reporter model.

public class RequestDetail
{
    public string? IpAddress { get; set; }

    public string? Username { get; set; }

    public string? Host { get; set; }

    public string? Method { get; set; }

    public string? Path { get; set; }

    public string? QueryString { get; set; }

    public string? UserAgent { get; set; }

    public double Duration { get; set; }

    public bool HadError { get; set; }

    public string? NodeName { get; set; }

    public DateTime EventDateTime { get; set; }
}

[HttpPost]
public IActionResult SaveReport([FromBody] RequestDetail report)
{
    Debugger.Log(0, null, JsonSerializer.Serialize(report));
    return Ok();
}

There is an option in the configuration to dump a set of logs instead of sending them one by one. To use this functionality, you need to set the BulkDataDumpingEnabled to true. Then the report writer will create a set of records based on the limitation has been set by DataDumpingSize and sends it in one package. In this case, the target API should receive a set of RequestDetail instead of an individual item. The following sample shows the configuration for the bulk data dumping:

builder.Services.AddHTTPWriter(options =>
{
    options.ListenerAddress = new Uri("Target API to push the report");
    options.MuteOnError = false;
    options.AuthenticationType = AuthenticationType.None;
    options.NodeName = "Main Node Bulk Reprot";
    options.BulkDataDumpingEnabled = true;
    options.DataDumpingSize = 100;
});

And the following sample shows an instance of the report listener:

[HttpPost]
public IActionResult SaveBulkReport([FromBody] List<RequestDetail> report)
{
    Debugger.Log(0, null, JsonSerializer.Serialize(report));
    return Ok();
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 143 2/1/2024
1.2.0 309 12/26/2022
1.1.0 425 7/7/2022
1.0.3 422 5/23/2022
1.0.2 430 4/16/2022
1.0.1 418 4/15/2022
1.0.0 417 4/14/2022