GHM.HttpResult 2.0.1

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

// Install GHM.HttpResult as a Cake Tool
#tool nuget:?package=GHM.HttpResult&version=2.0.1                

<p align="center"> <img src="logo.png" alt="logo" width="200px"/> </p>

<h1 align="center"> GHM.HTTPResult </h1>

GHM.HTTPResult aims to type the HTTP result of your API service or others.

Install Package

.NET CLI

dotnet add package GHM.HttpResult

Package Manager

NuGet\Install-Package GHM.HttpResult

Example

This lib has been created to improve description of method returns called, and facilitating the casting of success and error cases

using GHM.HTTPResult;

public class UserService
{

    public Ok<string> GetUserName(GetUserNameRequest request)
    {
        User user =  _userRepo.GetUser(request.Id);

        if(user is null)
        {
            return Error.NotFound($"not found user by id {request.Id}");
        }

        return user.Name
    }
}

using GHM.HTTPResult;

public class UserController : ControllerBase
{

    [HttpGet]
    public IActionResult GetUser(int id)
    {
        Ok<User> result = _userService.GetUser(id);

        return ConvertExempleTest(result);// you can create a Converter to change return from Result to Action automatically
    }

    [HttpGet]
    public IActionResult GetUser(int id)
    {
        Ok<User> result = _userService.GetUser(id);

        return result.Match(
            (data) => Ok(data),
            (errors) => BadRequest(errors)
        );// using a match pattern
    }
}

Classes

HttpSuccess

Type of HttpSuccess:

  • Ok
  • Created
  • NoContent
public class Ok<TData> : Result<TData> { }
public class Ok : Result { }

public class Created<TData> : Result<TData>  { }
public class Created : Result { }

public class NoContent<TData> : Result<TData>  { }
public class NoContent : Result { }

Error

It's just one type of error with different HttpStatusCode:

  • NotFound
  • Forbidden
  • BadRequest
  • Unauthorized
  • Conflict
public class Error
{
    public string Message { get; init; }
    public HttpStatusCode StatusCode { get; init; }

    private Error(string message, HttpStatusCode httpStatusCode)
    {
        Message = message;
        StatusCode = httpStatusCode;
    }

    public static Error NotFound(string message) => new(message, HttpStatusCode.NotFound);

    public static Error Forbidden(string message) => new(message, HttpStatusCode.Forbidden);

    public static Error BadRequest(string message) => new(message, HttpStatusCode.BadRequest);

    public static Error Unauthorized(string message) => new(message, HttpStatusCode.Unauthorized);

    public static Error Conflict(string message) => new(message, HttpStatusCode.Conflict);
}

ValidationError

It's just one type of validation with different ErrorHttpStatusCode or OkStatus:

  • NotFound
  • Forbidden
  • BadRequest
  • Unauthorized
  • Conflict
public class ValidationResult
{
    public ValidationResult(bool isError)
    {
        IsError = isError;
    }

    public bool IsError { get; init; }

    public Result AsNotFound(string message) => IsError ? new(Error.NotFound(message)) : Result.Ok;

    public Result AsForbidden(string message) => IsError ? new(Error.Forbidden(message)) : Result.Ok;

    public Result AsBadRequest(string message) => IsError ? new(Error.BadRequest(message)) : Result.Ok;

    public Result AsUnauthorized(string message) => IsError ? new(Error.Unauthorized(message)) : Result.Ok;

    public Result AsConflict(string message) => IsError ? new(Error.Conflict(message)) : Result.Ok;

    public Result AsError(Error error) => IsError ? new(error) : Result.Ok;
}

Result

A class base for Ok, Created and NoContent classes. Result has properties base as Errors, StatusCode and IsSuccess.

public class Result
{
    protected readonly List<Error> _errors = new();
    public IReadOnlyList<Error> Errors => _errors;
    public bool IsValid => !_errors.Any();
}

public class Result<TData> : Result
{
     public TData Data { get; init; } = default!;
}

Star

if you enjoy, don't forget the ⭐ and install the package 😊.

Product Compatible and additional computed target framework versions.
.NET 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 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.
  • net7.0

    • No dependencies.

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
2.0.1 156 2/6/2024
1.2.3 115 2/2/2024
1.2.2 145 1/10/2024
1.2.1 135 1/6/2024
1.1.0 115 1/5/2024
1.0.0 173 11/23/2023