GHM.HttpResult
1.0.0
See the version list below for details.
dotnet add package GHM.HttpResult --version 1.0.0
NuGet\Install-Package GHM.HttpResult -Version 1.0.0
<PackageReference Include="GHM.HttpResult" Version="1.0.0" />
paket add GHM.HttpResult --version 1.0.0
#r "nuget: GHM.HttpResult, 1.0.0"
// Install GHM.HttpResult as a Cake Addin #addin nuget:?package=GHM.HttpResult&version=1.0.0 // Install GHM.HttpResult as a Cake Tool #tool nuget:?package=GHM.HttpResult&version=1.0.0
GHM.HttpResult
GHM.HttpResult is nuget packege with the aim of typing http result.
Install Package
.NET CLI
dotnet add package GHM.HttpResult --version 1.0.0
Package Manager
NuGet\Install-Package GHM.HttpResult -Version 1.0.0
Exemple
The exemple presents a demo of how this lib works. Focused on implicit operator to facilitate all castings.
public class UserService
{
public Ok<User> GetUser(int id)
{
User user = _userRepo.GetUser(id);
if(user is null)
{
return Error.NotFound($"not found user by id {id}");
}
return user
}
}
public class UserController : ControllerBase
{
[HttpGet]
public IActionResult GetUser(int id)
{
Ok<User> result = _userService.GetUser(id);
return ConvertExempleTest(result);// you can create a Converting from Result to Action automaticly
}
[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
Result
Is a abstract class base to Ok, Created and NoContent.
Result
has the result base as Errors, StatusCode and IsSuccess property.
Result<TData>
is based on Result with data property
public abstract class Result
{
public IReadOnlyList<Error> Errors { get; init; }
public HttpStatusCode StatusCode { get; init; }
public bool IsSuccess => (int)StatusCode < 400;
}
public abstract class Result<TData> : Result
{
private readonly TData? _data;
public TData Data => IsSuccess ? _data! : throw new ArgumentException("http error has no data.");
}
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 is only one Error type 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);
}
Star
if you enjoy, don't forget the ⭐ and install the package 😊.
Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
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.