Skip to content

A JSON deserializer that unwraps the Result property of the AutoWrapper ApiResponse object.

License

Notifications You must be signed in to change notification settings

proudmonkey/AutoWrapper.Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutoWrapper.Server Nuget Nuget downloads

AutoWrapper.Server is a simple library that enables you unwrap the Result property of the AutoWrapper's ApiResponse object in your C# server-side code. The goal is to deserialize the Result object directly to your matching Model without having you to create the ApiResponse schema.

For details about AutoWrapper, check the official repo here: AutoWrapper

Installation

  1. Download and Install the latest AutoWrapper.Server from NuGet or via CLI:
PM> Install-Package AutoWrapper.Server -Version 2.0.0
  1. Declare the following namespace in the class where you want to use it.
using AutoWrapper.Server;

Sample Usage

[HttpGet]
public async Task<IEnumerable<PersonDTO>> Get()
{
    var client = HttpClientFactory.Create();
    var httpResponse = await client.GetAsync("https://localhost:5001/api/v1/persons");

    IEnumerable<PersonDTO> persons = null;
    if (httpResponse.IsSuccessStatusCode)
    {
        var jsonString = await httpResponse.Content.ReadAsStringAsync();
        persons = Unwrapper.Unwrap<IEnumerable<PersonDTO>>(jsonString);
    }

    return persons;
}

If you are using the [AutoWrapperPropertyMap] to replace the default Result property to something else like Payload, then you can use the following overload method below and pass the matching property:

Unwrapper.Unwrap<IEnumerable<PersonDTO>>(jsonString, "payload");

Using the UnwrappingResponseHandler

Alternatively you can use the UnwrappingResponseHandler like below:

[HttpGet]
public async Task<IEnumerable<PersonDTO>> Get()
{
    var client = HttpClientFactory.Create(new UnwrappingResponseHandler());
    var httpResponse = await client.GetAsync("https://localhost:5001/api/v1/persons");

    IEnumerable<PersonDTO> persons = null;
    if (httpResponse.IsSuccessStatusCode)
    {
        var jsonString = await httpResponse.Content.ReadAsStringAsync();
        persons = JsonSerializer.Deserialize<IEnumerable<PersonDTO>>(jsonString);
    }

    return persons;
}

You can also pass the matching property to the handler like in the following:

var client = HttpClientFactory.Create(new UnwrappingResponseHandler("payload"));

Give a Star! ⭐

That's it. If you used AutoWrapper or if you find this useful, please give it a star to show your support and share it to others. :)

About

A JSON deserializer that unwraps the Result property of the AutoWrapper ApiResponse object.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages