Skip to content

Getting started

Luiz Felipe edited this page Jul 7, 2022 · 6 revisions

What is OutResp?

OutResp is a response wrapper! Every API has to return values, and it is a good practice to settle a response pattern, so the client knows what to expect from consuming your API. Besides, OutResp also provides ways to handle returns within the application workflow by adding notifications, messages, and verification.

Cool huh? So if you are looking for a package that will help you with that, OutResp is perfect!

How does it work?

You can do some cool things with OutResp; let's check them?

Responses

The only thing you need to do to use OutResp is use OutRespContract which is an abstraction that contains all the methods you need to fulfill your response; let's take a look at the code and see what it looks like.

public IOutResp ValidateName(string name)
{
    if(string.IsNullOrEmpty(name))
        return OutRespContract
            .Failure<dynamic>()
            .AddStatusCode(HttpStatusCode.BadRequest)
            .AddMessage("Invalid name.");


    return OutRespContract
        .Success<dynamic>()
        .AddStatusCode(HttpStatusCode.OK)
        .AddMessage("Valid name.")
        .AddValue(
            new
            {
                Data = ...
            });
}

Using the example above, you can see that it is possible to declare a Failure and a Success response by adding to it Status Code, Message (or messages), and a dynamic value; but you haven't seen all of OutResp can provide. It also can help you with your workflow within your application.

Success Response

Here's an example of how you can declare a successful response using OutResp. The success response provides by the default status code equals 200-OK and IsSuccess property is equal to true.

void SubmitUser()
{
    var response = OutRespContract
        .Success<dynamic>()
        .AddMessage("Success message")
        .AddValue(
            new
            {
                Data = ...
            });

    if(response.IsSuccess)
        Console.WriteLine($"User submitted. {response.Messages}");
    else
        Console.WriteLine($"Error: {response.Messages}");
}

Failure Response

Below you can see how to use the failure response. The failure response provides by the default status code equals to 400-BadRequest and the properties IsSuccess is equal to false.

void SubmitUser()
{
    var response = OutRespContract
        .Failure<dynamic>()
        .AddMessage("Something went wrong!")
        .AddValue(
            new
            {
                Data = ...
            });

    if(response.IsSuccess)
        Console.WriteLine($"User submitted. {response.Messages}");
    else
        Console.WriteLine($"Error: {response.Messages}");
}

Simple Responses

If you don't want to send data in your response Simple Responses are perfect for you! With OutResp Simple Responses, we can declare a response in just one line and OutResp will take care of the rest setting Failure or Success default values; cool huh? Here's how you can use it.

IOutResp ValidateName(string name)
{
    if(string.IsNullOrEmpty(name))
        return OutRespContract.Failure();

    return OutRespContract.Success();
}

Notifications

Notifications are for those moments when you want to have better control of your workflow. Using notifications you can log things and do validations in your workflow. Notifications won't be displayed in the final response! Notifications have two types: Warning and Error. If you add an error notification, all the properties for validations such as IsValid and IsSuccess are going to be false. With that said, let's see how to implement it!

void SubmitUser()
{
    var response = OutRespContract
        .Success<dynamic>()
        .AddNotification("User doesn't contain a last name", ENotificationType.Warning)
        .AddMessage("Success message");

    if(response.IsSuccess)
        Console.WriteLine($"User submitted. {response.Messages}");
    else
        Console.WriteLine($"Error: {response.Messages}");
}
Clone this wiki locally