WhateverDotNet.API.Abstractions 0.1.0-preview.2

This is a prerelease version of WhateverDotNet.API.Abstractions.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package WhateverDotNet.API.Abstractions --version 0.1.0-preview.2
                    
NuGet\Install-Package WhateverDotNet.API.Abstractions -Version 0.1.0-preview.2
                    
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="WhateverDotNet.API.Abstractions" Version="0.1.0-preview.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WhateverDotNet.API.Abstractions" Version="0.1.0-preview.2" />
                    
Directory.Packages.props
<PackageReference Include="WhateverDotNet.API.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WhateverDotNet.API.Abstractions --version 0.1.0-preview.2
                    
#r "nuget: WhateverDotNet.API.Abstractions, 0.1.0-preview.2"
                    
#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.
#:package WhateverDotNet.API.Abstractions@0.1.0-preview.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WhateverDotNet.API.Abstractions&version=0.1.0-preview.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=WhateverDotNet.API.Abstractions&version=0.1.0-preview.2&prerelease
                    
Install as a Cake Tool

WhateverDotNet.API.Abstractions

Abstractions and helpers for building JSON-based REST API clients, including:

  • BaseJsonRestClient: reusable base class for sending JSON requests and deserializing JSON responses
  • DI helpers to register logging utilities
  • Test-execution logging abstractions (useful for NUnit / BDD-style step output)

Install

dotnet add package WhateverDotNet.API.Abstractions --prerelease

What’s inside

  • Base client: WhateverDotNet.API.Abstractions.BaseJsonRestClient
    • Sends requests via an initialized HttpClient
    • Serializes request bodies to JSON and deserializes responses from JSON
    • Logs outgoing calls (trace logger + optional test-step logger)
  • Options: BaseJsonRestClientOptions (BaseUrl, Timeout)
  • Logging:
    • ITestExecutionLogger (test output abstraction)
    • NUnitTestExecutionLogger (writes to Console)
    • ILogMessageFormatter / ApiLogMessageFormatter (human-readable step messages)
  • DI: ServiceCollectionExtensions
    • AddApiLogMessageFormatter()
    • AddNUnitTestExecutionLogger(options)

Basic usage

Create your API client by inheriting from BaseJsonRestClient and implementing ExecuteAndAssertAsync.

using System.Net;
using Microsoft.Extensions.Logging;
using WhateverDotNet.API.Abstractions;
using WhateverDotNet.API.Abstractions.Logging;

public sealed class MyApiClient : BaseJsonRestClient
{
    public MyApiClient(
        BaseJsonRestClientOptions options,
        ILogMessageFormatter formatter,
        ITestExecutionLogger? apiLogger,
        ILoggerFactory? loggerFactory = null)
        : base(options, formatter, apiLogger, loggerFactory)
    {
    }

    public void ConnectAs(string bearerToken)
    {
        // The base class throws if BaseUrl/token are missing.
        _httpClient = CreateClient($"Bearer {bearerToken}");
    }

    public Task<MyDto> GetSomethingAsync(CancellationToken ct = default)
        => ExecuteAndAssertAsync(
            () => TryGetAsJsonAsync<MyDto>("/v1/something", cancellationToken: ct),
            HttpStatusCode.OK);

    protected override async Task<TResponse> ExecuteAndAssertAsync<TResponse>(
        Func<Task<(TResponse? Response, HttpResponseMessage Message)>> function,
        HttpStatusCode expectedStatusCode)
    {
        var (response, message) = await function().ConfigureAwait(false);

        if (message.StatusCode != expectedStatusCode)
        {
            var body = message.Content is null
                ? null
                : await message.Content.ReadAsStringAsync().ConfigureAwait(false);

            throw new InvalidOperationException(
                $"Expected {(int)expectedStatusCode} but got {(int)message.StatusCode}. Body: {body}");
        }

        return response!;
    }
}

public sealed record MyDto(string Id);

DI registration (optional)

using Microsoft.Extensions.DependencyInjection;
using WhateverDotNet.API.Abstractions.Extensions;
using WhateverDotNet.API.Abstractions.Logging;

var services = new ServiceCollection();

services.AddApiLogMessageFormatter();
services.AddNUnitTestExecutionLogger(new TestExecutionLoggerOptions
{
    StepPattern = "STEP: {0}",
    PreconditionPattern = "PRE: {0}",
    ExpectedResultPattern = "EXP: {0}",
    ActualResultPattern = "ACT: {0}",
});

Notes

  • BaseJsonRestClient expects _httpClient to be initialized (for example in a ConnectAs(...) method). If it isn’t, calls throw HttpClientNotInitializedException.
  • The default JSON serializer options use camelCase naming and are case-insensitive on property names.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
0.1.0-preview.6 76 6/7/2026
0.1.0-preview.5 58 5/5/2026
0.1.0-preview.4 71 3/31/2026
0.1.0-preview.3 62 3/20/2026
0.1.0-preview.2 65 3/20/2026
0.1.0-preview.1 67 3/18/2026