c# - Is it possible to pass 2 types of objects to Restsharp? -


we have scenario external api returns either user xml or error xml based on whether request succeed or failed.

at moment i'm passing user poco restsharp , works fine. if fails, object null. , won't know why failed unless parse error xml manually.

is there way workaround this?

e.g.

var restclient = new restclient(baseurl); var request = new restrequest(uri); request.method = method.post; var response = restclient.execute<user>(request); 

on execution of above method api can return error xml object. how error object on fail , user on success?

this possible, although code little ugly. restsharp allows specify own xml deserializer, we'll need in order make work.

first, though, need data type can store either error or user (i made generic works more users):

public class result<t> {     public t data { get; set; }     public error error { get; set; } } 

so idea is, when execute request, ask restsharp result<user> instead of user, i.e.:

var result = client.execute<result<user>>(request); 

now here's magic required deserialize either error or user. it's custom deserializer inherits restsharp's xmldeserializer. warning: code not tested @ all, can point in right direction.

public class xmlresultdeserializer : xmldeserializer {     public override t deserialize<t>(irestresponse response)     {         if (!typeof(t).isgenerictype || typeof(t).getgenerictypedefinition() != typeof(result<>))             return base.deserialize<t>(response);          // determine whether response contains error or normal data.         var doc = xdocument.parse(response.content);         var result = activator.createinstance<t>();         if (doc.root != null && doc.root.name == "error")         {             // it's error             var error = base.deserialize<error>(response);             var errorproperty = result.gettype().getproperty("error");             errorproperty.setvalue(result, error);         }         else         {             // it's normal data             var innertype = typeof(t).getgenericarguments()[0];             var deserializemethod = typeof(xmldeserializer)                 .getmethod("deserialize", new[] { typeof(irestresponse) })                 .makegenericmethod(innertype);             var data = deserializemethod.invoke(this, new object[] { response });              var dataproperty = result.gettype().getproperty("data");             dataproperty.setvalue(result, data);         }         return result;     } } 

then wire this:

var restclient = new restclient(baseurl); client.addhandler("application/xml", new xmlresultdeserializer()); var request = new restrequest(uri); request.method = method.post; var result = restclient.execute<result<user>>(request); if (response.data.data != null) {     var user = response.data.data;     // user... } else if (response.data.error != null) {     var error = response.data.error;     // handle error... } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -