New Deserialization Feature: Different Root on Error
Some APIs out there return different schemas when an error is encountered. Using the default deserializers and their direct mappings to a single schema it can be difficult to handle these situations. Because error conditions vary so widely, we needed a way to let you specify them for each request. I’ve checked in a change tonight to accomplish this. This only exists in the desktop framework sync methods right now, but I wanted to show it off and get feedback.
Here’s what it looks like:
var client = new RestClient(BaseUrl);
var request = new RestRequest("BadUrl");
request.RootElement = "Success";
request.ErrorRootElement = "Error";
request.ErrorCondition = resp =>
{
return resp.StatusCode == HttpStatusCode.BadRequest;
};
var restResponse = client.Execute<RestResponse>(request);
// Write(restResponse.Message);
-- RestResponse.cs --
public class RestResponse
{
public string Message { get; set; }
}
If ErrorCondition returns true, the deserialization will start from a root element named Error. If everything works and there are no errors, deserialization will start from Success. You’ll still have to pack everything into a single class. I may add another overload to like Execute<TSuccess, TError>(request).
What do you think?
