DiegoG.REST.Core 1.0.1

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

// Install DiegoG.REST.Core as a Cake Tool
#tool nuget:?package=DiegoG.REST.Core&version=1.0.1                

DiegoG.REST

NuGet Package

<b>Provides base types and utilities useful for creating standardized models for a REST API and consumers of such an API</b>

Types

RESTObject

A base type for model objects that provides methods to react when serialization and deserialization (when using a IRESTObjectSerializer or other compliant serializer) and a members that provide useful metadata about the object itself, allowing it to be identified as the actually appropriate model when such information is otherwise lacking, for example, when receiving a request from an API that could send a variety of responses.

IRESTObjectSerializer

An interface that defines methods and members that permit easy deserialization of the appropriate type for any given model using its RESTObject<TObjectCode>.Code

  • Note that this library does not provide any default serializers, see DiegoG.REST.Json

RESTObjectTypeTable

An abstract class that, at its root, is a dictionary that returns type information for a given REST model created using this library, discriminated by its RESTObject<TObjectCode>.Code

Examples

Base Response type
public enum ResponseCodeEnum : uint
{
    TestA = 0,

    ErrorResponse = 100
}


public readonly record struct ResponseCode(ResponseCodeEnum Code) : IEquatable<ResponseCode>
{
    public string Name { get; } = Code.ToString();

    public static implicit operator ResponseCodeEnum(ResponseCode code) => code.Code;
    public static implicit operator ResponseCode(ResponseCodeEnum code) => new(code);
}

public abstract class ResponseBase : RESTObject<ResponseCode>
{
    protected ResponseBase(ResponseCode code) : base(code)
    {
    }
}
Response Type Table
public class RESTResponseTypeTable : RESTObjectTypeTable<ResponseCode>
{
    private RESTResponseTypeTable() { }

    public static RESTResponseTypeTable Instance { get; } = new();

    public override Type GetTypeFor(ResponseCode code)
        => code.Code switch
        {
            ResponseCodeEnum.TestA => typeof(TestResponseA),

            ResponseCodeEnum.ErrorResponse => typeof(ErrorResponse),
            _ => throw new ArgumentException($"Unknown ResponseCode {code.Code}"),
        };

    protected override void AddType(Type type, ResponseCode code)
    {
        throw new NotImplementedException("Adding types to this table is not supported");
    }
}
Error Response
public class ErrorResponse : ResponseBase
{
    public ErrorResponse(params Exception?[] exceptions) : base(new ResponseCode(ResponseCodeEnum.ErrorResponse))
    {
        Errors = exceptions.Where(x => x is not null).Select(x => x!.Message).ToImmutableArray();
    }

    public ErrorResponse(params string[] errors) : base(new ResponseCode(ResponseCodeEnum.ErrorResponse))
    {
        Errors = ImmutableArray.Create(errors);
    }

    public ErrorResponse(IEnumerable<string>? errors = null, IEnumerable<Exception>? exceptions = null) : base(new ResponseCode(ResponseCodeEnum.ErrorResponse))
    {
        IEnumerable<string> query = errors ?? Array.Empty<string>();
        query = exceptions is null ? query : query.Concat(exceptions.Where(x => x is not null).Select(x => x.Message));
        Errors = query.ToImmutableArray();
    }

    public ImmutableArray<string> Errors { get; init; }
}
TestResponseA
public class TestResponseA : ResponseBase
{
    public string LeResponse { get; init; }

    public TestResponseA(string leResponse) : base(ResponseCodeEnum.TestA)
    {
        LeResponse = leResponse ?? throw new ArgumentNullException(nameof(leResponse));
    }
}
Example Usage
public static async Task Main(string[] args)
{
    var serializer = new JsonRESTSerializer<ResponseCode>();
    RESTObject<ResponseCode> resp = args.Contains("do-error") ? new ErrorResponse("My bad", "Whoops") : new TestResponseA("Superb!");

    var ms = new MemoryStream(); // usually, this wouldn't be a MemoryStream

    serializer.Serialize(resp, ms);

    // ---------------

    var r = serializer.Deserialize(ms, RESTResponseTypeTable.Instance);
    // This will deserialize the appropriate response
}
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 was computed.  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 was computed.  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.
  • net6.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on DiegoG.REST.Core:

Package Downloads
DiegoG.REST.ASPNET

A library containing useful utilities for making RESTful APIs with ASP.NET

DiegoG.REST.Json

A library containing classes and utilities for making RESTful APIs and consumers that serialize to and from JSON

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 341 3/29/2023
1.0.0 276 3/28/2023