public static T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient {BaseUrl = new Uri(_serverName + "rest/")};
request.AddParameter("u", Username);
var salt = NewSalt();
var token = Md5(Password + salt);
request.AddParameter("t", token);
request.AddParameter("s", salt);
request.AddParameter("v", ApiVersion);
request.AddParameter("c", "MusicBee");
var response = client.Execute<T>(request);
if (response.ErrorException != null)
{
const string message = "Error retrieving response from Subsonic server:";
MessageBox.Show(message + "\n\n" + response.ErrorException, @"Subsonic Plugin Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return response.Data;
}
I used the "XSD" tool from Microsoft's .NET Tools package to create the classes directly from the API's .xsd file, so here's an extract on one class to give you an idea:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class Playlists {
private List<Playlist> playlistField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("playlist")]
public List<Playlist> playlist {
get {
return this.playlistField;
}
set {
this.playlistField = value;
}
}
}
And here's the call to Execute which was supposed to get mapped to the "Playlists" class:
var request = new RestRequest
{
Resource = "getPlaylists.view",
RootElement = "Playlists"
};
var result = Execute<Playlists>(request);
When this call executes, I get an exception containing the following:
System.NullReferenceException: Object reference not set to an instance of an object.
at RestSharp.Deserializers.XmlDeserializer.GetElementByName(XElement root, XName name)
at RestSharp.Deserializers.XmlDeserializer.Map(Object x, XElement root)
at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response)
at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)
meanwhile, in the response the Content is what I expected it to be (contains the correct information), although I noticed backslashes in the same way Lee mentions below:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<subsonic-response xmlns=\"
http://subsonic.org/restapi\" status=\"ok\" version=\"1.14.0\">\r\n <playlists>\r\n
<playlist id=\"714\" name=\"1920s, 1930s, 1940s\" comment=\"Auto-imported from 1920s, 1930s, 1940s.pls\" owner=\"admin\" public=\"true\" songCount=\"107\" duration=\"23859\" created=\"2016-06-13T05:22:13.824Z\" changed=\"2016-07-29T22:09:08.677Z\" coverArt=\"pl-714\"/>\r\n
...
Should I try a custom deserializer, or does anyone see anything obviously wrong with my approach?