RestSharp consuming SubSonic REST Api

815 views Asked by At

I am currently writing a c# library to use cross platform on windows, iOS, Android. I am consuming a Rest service and having some trouble with the objects being returned from the response. I am using RestSharp for the api calls. I used Xsd2Code.NET to generate my classes from an xsd provided by the api.

Problem is the responses are wrapping in a <subsonic-response>. The item I want is contained within. RestSharp tries to parse and does if I pass in the type as a List<NowPlaying> but the items within that do not get populated to the NowPlaying object. I generated the serialize/deserialize methods for NowPlaying but due to the <subsonic-response> as the root element an exception is thrown. Is there a way to remove <subsonic-response>? I tried response.RootElement = "subsonic-response" for the RestSharp call but does not work. See response below. Any help would be great.

RestResponse:

<?xml version="1.0" encoding="UTF-8"?>
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.7.0">
<nowPlaying>
    <entry     id="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b31325c38372e2044616e63652044616e63652028445542535445502052454d495829202d20426967205365616e2e6d7033" parent="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b3132" 
title="Dance Dance (DUBSTEP REMIX) - Big Sean" 
isDir="false" 
album="M3 MIXTAPE (MEMBA. ME. MAAD)" 
artist="DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE"
duration="67"
bitRate="192"
year="2012"
size="1615419" 
suffix="mp3" 
contentType="audio/mpeg" 
isVideo="false"
coverArt="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b31325c444a20434852495354554646202d204d454d4241204d45204d4141442046524f4e542e6a7067"
 path="Jan 2k12/DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE PRESENTS M3 (MEMBA ME MAAD) 2K12/DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE PRESENTS M3  (MEMBA ME MAAD) 2K12/87. Dance Dance (DUBSTEP REMIX) - Big Sean.mp3" 
username="admin" 
playerId="2" 
playerName="subAir"
minutesAgo="0"/>
 </nowPlaying>
</subsonic-response>

A class that was generated:

public partial class NowPlaying : EntityBase<NowPlaying>
{

    [EditorBrowsable(EditorBrowsableState.Never)]
    private List<NowPlayingEntry> entryField;

    public List<NowPlayingEntry> entry
    {
        get
        {
            if ((this.entryField == null))
            {
                this.entryField = new List<NowPlayingEntry>();
            }
            return this.entryField;
        }
        set
        {
            if ((this.entryField != null))
            {
                if ((entryField.Equals(value) != true))
                {
                    this.entryField = value;
                    this.OnPropertyChanged("entry");
                }
            }
            else
            {
                this.entryField = value;
                this.OnPropertyChanged("entry");
            }
        }
    }
}

My method I am calling to get the NowPlaying from the rest service

    public NowPlaying getNowPlaying()
    {
        NowPlaying playing;
        try
        {
            var request = new RestRequest();
            request.Resource = "getNowPlaying.view";
            playing = SendRequest<NowPlaying>(request);

        }
        catch (Exception ex)
        {

            throw ex;
        }

        return playing;

    }
2

There are 2 answers

0
Christopher D. Singh On

Solved my problem. End result was to not let RestSharp deserialize my object. Get the RestResponse content which is the the xml response. Then deserialize the Response object for the api. Grabbed the response.item and casted it to my NowPlaying object and all data was in object.

string xml = SendRequest(request);
var res = Response.Deserialize(xml);
playing =(NowPlaying)res.Item;
0
John Sheehan On

The general structure for a C# class that will match that schema is this:

public class SubsonicResource {
    public List<entry> NowPlaying { get; set; }
}

public class entry {
    public string Id { get; set; }
    public string Path { get; set; }
    public string Username { get; set; }
    ... 
}

Then you can call Execute<SubsonicResource>() and it should be populated.