GHM.HttpResult 1.2.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package GHM.HttpResult --version 1.2.2
NuGet\Install-Package GHM.HttpResult -Version 1.2.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="GHM.HttpResult" Version="1.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GHM.HttpResult --version 1.2.2
#r "nuget: GHM.HttpResult, 1.2.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 GHM.HttpResult as a Cake Addin
#addin nuget:?package=GHM.HttpResult&version=1.2.2

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

<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
    }

    public Ok<string> GetUserWithRailWay(GetUserNameRequest request)
    {
        return Ok.Create(request)
            .BindData((req) => _userRepo.GetUser(req.Id));
            .BindError((user) => (user is null, Error.NotFound($"not found user by id {request.Id}")))
            .Map((user) => 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<BasicResponse> { }

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

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 Title { get; init; }
    public HttpStatusCode StatusCode { get; init; }

    private Error(string title, HttpStatusCode httpStatusCode)
    {
        Title = title;
        StatusCode = httpStatusCode;
    }

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

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

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

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

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

ValidationError

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

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

    public bool IsError { get; init; }

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

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

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

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

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

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

Result

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

public abstract class Result
{
    public IReadOnlyList<Error> Errors { get; init; }

    public HttpStatusCode StatusCode { get; init; }

    public bool IsSuccess => (int)StatusCode < 400;
}

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 127 2/6/2024
1.2.3 83 2/2/2024
1.2.2 126 1/10/2024
1.2.1 110 1/6/2024
1.1.0 95 1/5/2024
1.0.0 166 11/23/2023