c# - Use RestSharp to deserialize different data outputs from one resource -
i have quite generic webservice, responds many standard http statuses. however, returns different types according these statuses. proper rest implementation: person entity status 200 (ok), when error occures, example auth problem, receive 403 (forbidden) list of error messages (stating token expired or account inactive, etc).
unfortunately, using restsharp can "bind" 1 type of data should expect:
client.executeasync<person>(request, (response) => { callback(response.data); });
how should capture error messages, when status different 200? of course, hand , deserialize response content myself, think there should cleaner, better way accomplish that. i'd use this:
client.executeasync<person, errorlist>(request, (response) => { if (response.statuscode == httpstatuscode.ok) { callback(response.data); } else if (response.statuscode == httpstatuscode.ok) { errorcallback(response.errors); } });
the way i've handled write post processor restsharp response. this,
public class response<t> { public t data {get;set;} public list<error> errors {get;set;} } public static response<t> getresponse<t>(this irestresponse restresponse) { var response = new response<t>(); if (response.statuscode == httpstatuscode.ok) { response.data = jsonconvert.deserilizeobject<t>(restresponse.data) } response.errors =jsonconvert.deserilizeobject<list<error>>(restresponse.error) return response; }
and can use this,
var response = client.execute(request).getresponse<t>();
Comments
Post a Comment