SingleApi 1.5.2

dotnet add package SingleApi --version 1.5.2                
NuGet\Install-Package SingleApi -Version 1.5.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="SingleApi" Version="1.5.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SingleApi --version 1.5.2                
#r "nuget: SingleApi, 1.5.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.
// Install SingleApi as a Cake Addin
#addin nuget:?package=SingleApi&version=1.5.2

// Install SingleApi as a Cake Tool
#tool nuget:?package=SingleApi&version=1.5.2                

SingleApi

Single/generic WebApi endpoint for mediators

Features

  • Ready for mediators
  • Generics support
  • In/out file streams

Example 1: SingleApi with MediatR

.NET CLI

dotnet new web --name "SingleApiExample"
cd SingleApiExample
dotnet add package SingleApi
dotnet add package MediatR

Change Program.cs

using MediatR;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

var app = builder.Build();

app.MapSingleApi("sapi", 
    // invoke the MediatR
    x => x.ServiceProvider.GetRequiredService<IMediator>().Send(x.Data, x.CancellationToken),
    // assemblies for type resolving
    Assembly.GetExecutingAssembly()); 

app.Run();

Example project...

Example 2: Request/Response

Request

GET /sapi/Ping?data={"Message":"TEST"}

or

POST /sapi/Ping
{"Message":"TEST"}

Response

{"Message":"TEST PONG"}

Example 3: Generics requests

app.MapSingleApi("sapi", 
    // for simplicity, return the received data
    x => Task.FromResult(x.Data), 
    // existing generic types will suffice for this example
    typeof(List<>).Assembly, typeof(int).Assembly); 

Request #1: Equivalent of List<String>

POST /sapi/List(String)
["text1","text2","text3"]

Request #2: Equivalent of Dictionary<string,int?[]>

POST /sapi/Dictionary(String-Array(Nullable(Int32)))
{"key1":[555,null,777]}

Example 4: .NET client

.NET CLI

dotnet new console --name "SapiClientExample"
cd SapiClientExample
dotnet add package SingleApi.Client

Program.cs:

using SingleApi.Client;

// create client
using var sapi = new SapiClient("https://localhost:7263/sapi");

// send request
var response = await sapi.Send(new Ping { Message = "TEST" });

Console.WriteLine(response?.Message);

Console output:

TEST PONG

Example project...

Example 5: File upload

Create RequestHandler

using MediatR;
using MetaFile;
namespace Example;

public class FileUpload : StreamFile, IRequest<string>
{

}

public class FileUploadHandler : IRequestHandler<FileUpload, string>
{
    public async Task<string> Handle(FileUpload request, CancellationToken cancellationToken)
    {
        var filePath = Path.GetFullPath(request.Name);
        using var fileStream = File.Create(filePath);
        await request.Content.CopyToAsync(fileStream, cancellationToken);
        return filePath;
    }
}

Sending a file in JavaScript

let file = document.getElementById('my-input').files[0];

let response = await fetch('/sapi/FileUpload', {
    method: 'POST',
    headers: {
        'content-type': file.type || 'application/octet-stream',
        'content-disposition': `attachment; filename*=utf-8''${encodeURIComponent(file.name)}`,
    },
    body: file,
});

console.log('result:', await response.json());

Example project...

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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.  net9.0 is compatible. 
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.5.2 67 11/16/2024
1.5.0 157 1/12/2024
1.4.0 596 11/16/2022
1.3.0 335 11/9/2022
1.2.3 376 10/29/2022
1.2.1 386 10/28/2022
1.2.0 364 10/27/2022
1.1.6 416 9/29/2022
1.1.5 437 9/22/2022
1.1.4 418 9/21/2022
1.1.3 438 9/21/2022
1.1.2 425 9/20/2022
1.1.1 405 9/20/2022
1.1.0 419 9/19/2022
1.0.1 430 9/12/2022
1.0.0 415 9/11/2022