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

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