Skip to content

Response mapping

Stanislav Molchanovskiy edited this page Nov 15, 2021 · 1 revision

Response mapping allows you to transform the data received from the transport response into other models. This can be useful, for example, if you want to reduce dependence on transport and NClient. To use mapping, you need to implement the IResponseMapper<TRequest, TResponse> interface and pass implementation to the WithResponseMapping method:

public interface IMyClient
{
    [GetMethod] Task<MyModel> GetAsync(int id);
}
...

IResponseMapper<HttpRequestMessage, HttpResponseMessage> myMapper = ...; // Mapper for MyModel
IMyClient myClient = NClientGallery.Clients.GetRest()
    .For<IMyClient>(host: "http://localhost:8080")
    .WithResponseMapping(myMapper)
    .Build();

MyModel result = await myClient.GetAsync(id: 1);

By default, mappers for IHttpResponse and IResult models are already set in the client. Use the WithoutResponseMapping method to remove all mappers from client.

Clone this wiki locally