public void GetAlbumsForArtist(SynoItem artist, Action <IEnumerable <SynoItem>, long, SynoItem> callback, Action <Exception> callbackError) { string urlBase = string.Format("http://{0}:{1}", this.Host, this.Port); var url = urlBase + this.versionDependentResourcesProvider.GetAudioSearchWebserviceRelativePath(this.DsmVersion); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Accept = "*/*"; request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; // Not supported yet, but that would decrease the bandwidth usage from 1.3 Mb to 83 Kb ... Pretty dramatic, ain't it ? //request.Headers["Accept-Encoding"] = "gzip, deflate"; request.UserAgent = "OpenSyno"; request.CookieContainer = new CookieContainer(); request.CookieContainer.SetCookies(new Uri(url), this.Token); request.Method = "POST"; int limit = 10000; string postString = string.Format(@"action=browse&target={0}&server=musiclib_music_aa&category=&keyword=&start=0&sort=title&dir=ASC&limit={1}", HttpUtility.UrlEncode(artist.ItemID), limit); byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postString); request.BeginGetRequestStream(ar => { // Just make sure we retrieve the right web request : no access to modified closure. HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState; var requestStream = webRequest.EndGetRequestStream(ar); requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); request.BeginGetResponse( responseAr => { // Just make sure we retrieve the right web request : no access to modified closure. var httpWebRequest = responseAr.AsyncState; var webResponse = webRequest.EndGetResponse(responseAr); var responseStream = webResponse.GetResponseStream(); var reader = new StreamReader(responseStream); var content = reader.ReadToEnd(); long count; IEnumerable <SynoItem> albums; SynologyJsonDeserializationHelper.ParseSynologyAlbums(content, out albums, out count, urlBase); var isOnUiThread = Deployment.Current.Dispatcher.CheckAccess(); if (isOnUiThread) { if (count > limit) { // FIXME : Use an error handling service // MessageBox.Show(string.Format("number of available albums ({0}) exceeds supported limit ({1})", count, limit)); } callback(albums, count, artist); } else { Deployment.Current.Dispatcher.BeginInvoke(() => { if (count > limit) { // FIXME : Use an error handling service // MessageBox.Show(string.Format("number of available artists ({0}) exceeds supported limit ({1})", count, limit)); } callback(albums, count, artist); }); } }, webRequest); }, request); }
public Task <IEnumerable <SynoItem> > SearchArtistAsync(string artistName) { TaskCompletionSource <IEnumerable <SynoItem> > tcs = new TaskCompletionSource <IEnumerable <SynoItem> >(); string urlBase = string.Format("http://{0}:{1}", this.Host, this.Port); //var url = urlBase + "/webman/modules/AudioStation/webUI/audio_browse.cgi"; var url = urlBase + this.versionDependentResourcesProvider.GetAudioSearchWebserviceRelativePath(this.DsmVersion); HttpWebRequest request = BuildRequest(url); int limit = 100; string postString = string.Format(@"sort=title&dir=ASC&action=browse&target=musiclib_music_aa&server=musiclib_music_aa&category=&keyword={0}&start=0&limit={1}", artistName, limit); byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postString); var requestStreamAr = request.BeginGetRequestStream(ar => { // Just make sure we retrieve the right web request : no access to modified closure. HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState; var requestStream = webRequest.EndGetRequestStream(ar); requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); var getResponseAr = webRequest.BeginGetResponse( responseAr => { // Just make sure we retrieve the right web request : no access to modified closure. var httpWebRequest = responseAr.AsyncState; var webResponse = webRequest.EndGetResponse(responseAr); var responseStream = webResponse.GetResponseStream(); var reader = new StreamReader(responseStream); var content = reader.ReadToEnd(); long count; IEnumerable <SynoItem> tracks; SynologyJsonDeserializationHelper.ParseSynologyAlbums( content, out tracks, out count, urlBase); var isOnUiThread = Deployment.Current.Dispatcher.CheckAccess(); if (isOnUiThread) { if (count > limit) { // MessageBox.Show(string.Format("number of available artists ({0}) exceeds supported limit ({1})", count, limit)); } tcs.SetResult(tracks); // callback(tracks); } else { Deployment.Current.Dispatcher.BeginInvoke( () => { if (count > limit) { // MessageBox.Show(string.Format("number of available artists ({0}) exceeds supported limit ({1})", count, limit)); } tcs.SetResult(tracks); }); } }, webRequest); }, request); //var getRequestStreamTask = Task.Factory.FromAsync( // requestStreamAr, // ar => // , // TaskCreationOptions.None, // TaskScheduler.FromCurrentSynchronizationContext()); return(tcs.Task); }