PuppyKittyOverflow.Portable.OverflowHelper.GetPictureAsync C# (CSharp) Method

GetPictureAsync() public static method

public static GetPictureAsync ( Animal animal ) : Task
animal Animal
return Task
        public async static Task<String> GetPictureAsync(Animal animal)
        {
            try
            {
                Xamarin.Insights.Track("GetPicture", new Dictionary<string, string>
                    {
                        {"animal", animal.ToString()}
                    });

                if(animal == Animal.Random)
                {
                    var next = random.Next(0, 100);
                    if(next < 35)
                        animal = Animal.Cat;
                    else if(next < 70)
                        animal = Animal.Dog;
                    else
                        animal = Animal.Otter;
                }
                
                
                var url = string.Empty;
                switch (animal)
                {
                    case Animal.Cat:
                        url = CatUrl;
                        break;
                    case Animal.Dog:
                        url = DogUrl;
                        break;
                    default:
                        url = OtterUrl;
                        break;
                }

                var client = new HttpClient(new ModernHttpClient.NativeMessageHandler());
                if (client.DefaultRequestHeaders.CacheControl == null)
                    client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue();

                client.DefaultRequestHeaders.CacheControl.NoCache = true;
                client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;
                client.DefaultRequestHeaders.CacheControl.NoStore = true;
                client.Timeout = new TimeSpan(0, 0, 10);

                var imageUrl = await client.GetStringAsync(url);
                if (!string.IsNullOrWhiteSpace(imageUrl) && imageUrl.EndsWith("\n"))
                    imageUrl = imageUrl.TrimEnd('\n');

                return imageUrl;


            }
            catch (Exception ex)
            {
                return string.Empty;

            }

        }
    }