Exemplo n.º 1
0
        private static List <Wallpaper> Get(List <string> urls)
        {
            List <Wallpaper> toReturn = new List <Wallpaper>();

            foreach (string url in urls)
            {
                List <Wallpaper> returnedwallpapers;
                bool             isLastPage;
                RequestErrors    result = Get(url, out returnedwallpapers, out isLastPage);

                if (result != RequestErrors.NoError)
                {
                    if (ReturnPartialResults && toReturn.Count > 0 || (returnedwallpapers != null && returnedwallpapers.Count > 0))
                    {
                        if (returnedwallpapers != null)
                        {
                            toReturn.AddRange(returnedwallpapers);
                        }
                        return(toReturn);
                    }
                    throw new IOException(ErrorDescriptions.GetDescription(result));
                }

                toReturn.AddRange(returnedwallpapers);
                if (isLastPage)
                {
                    break;
                }
            }

            return(toReturn);
        }
Exemplo n.º 2
0
        private static RequestErrors Get(String requestUrl, out List <Wallpaper> returnedwallpapers, out bool isLastPage)
        {
            TimeSpan timeout = new TimeSpan(0, 0, 10);
            Stream   stream;

            Task <Stream> task = new Task <Stream>(() => { return(new System.Net.WebClient().OpenRead(requestUrl)); });

            task.Start();

            bool     timedout = false;
            DateTime start    = DateTime.Now;

            while (!task.IsCompleted)
            {
                Task.Delay(100);

                if (DateTime.Now - start > timeout)
                {
                    timedout = true;
                    break;
                }
            }

            if (!timedout)
            {
                stream = task.Result;
                // task completed within timeout
            }
            else
            {
                isLastPage         = true;
                returnedwallpapers = null;
                return(RequestErrors.request_timeout);
                // timeout logic
            }

            //Stream stream = new System.Net.WebClient().OpenRead(requestUrl);

            var serializer = new DataContractJsonSerializer(typeof(WallpaperResponse));
            WallpaperResponse deserializedResult = (WallpaperResponse)serializer.ReadObject(stream);


            if (deserializedResult.Success == true)
            {
                LastResult = "OK";

                returnedwallpapers = new List <Wallpaper>(deserializedResult.Wallpapers);
                isLastPage         = deserializedResult.IsLast;
                return(RequestErrors.NoError);
            }

            LastResult = deserializedResult.ErrorMessage;

            isLastPage         = true;
            returnedwallpapers = null;
            return(ErrorDescriptions.ToEnum(deserializedResult.ErrorMessage));
        }