Skip to content

Models to facilitate implementation and handling successful and error responses in web api.

License

Notifications You must be signed in to change notification settings

ThiagoBarradas/webapi-models

Folders and files

NameName
Last commit message
Last commit date
Sep 25, 2018
Jan 17, 2023
Jan 17, 2023
Sep 18, 2018
Sep 18, 2018
Sep 25, 2018
Jul 9, 2020
Jul 9, 2020
Jul 9, 2020

Repository files navigation

Build Status Quality Gate Status Coverage NuGet Downloads NuGet Version

WebApi.Models

Models to facilitate implementation and handling successful and error responses in web api.

Sample

Sample - Entity to ApiResponse

var apiResponse = myEntity.ToApiResponse(HttpStatusCode.OK);

Sample with Exception

// Creating a exception with errors

var errors = new ErrorsReponse();

response.AddError("message1", "property1");
response.AddError("message2", "property2");

throw new BadRequestException(errors);

// On your exception handler 

var exceptionApiResponse = badRequestException.ToApiResponse();

You need implements ApiResponse mapping to your web api framework response, like ASP.NET or NancyFx model.

Sample - Casting ApiResponse to HttpResponseMessage

public class BaseApiController : ApiController
{
    protected HttpResponseMessage CreateResponse(ApiResponse response)
    {
        var response = Request.CreateResponse(apiResponse.StatusCode, apiResponse.Content);
        apiResponse.Headers.ForEach(header => 
        {
            response.Headers.Add(header.Key, header.Value);
        });

        return response;
    }
}

public class MyController : BaseApiController
{
    [HttpGet]
    [Route("")]
    public HttpResponseMessage GetSomething()
    {
        // do something and get ApiResponse

        return this.CreateResponse(apiResponse);
    }
}

Install via NuGet

PM> Install-Package WebApi.Models

How can I contribute?

Please, refer to CONTRIBUTING

Found something strange or need a new feature?

Open a new Issue following our issue template ISSUE_TEMPLATE

Changelog

See in nuget version history

Did you like it? Please, make a donate :)

if you liked this project, please make a contribution and help to keep this and other initiatives, send me some Satochis.

BTC Wallet: 1G535x1rYdMo9CNdTGK3eG6XJddBHdaqfX

1G535x1rYdMo9CNdTGK3eG6XJddBHdaqfX