Exemple #1
0
        /// <summary>
        /// Fetch a random dog via their breed/sub-breed. Or leave null for any dog.
        /// </summary>
        /// <param name="breed"></param>
        /// <param name="subBreed"></param>
        /// <returns></returns>
        public static async Task <Dog> FetchAsync(string breed = null, string subBreed = null)
        {
            string _base;

            bool breedVal    = string.IsNullOrEmpty(breed);
            bool subBreedVal = string.IsNullOrEmpty(subBreed);

            if (breedVal && !subBreedVal)
            {
                throw new Exception("You cannot get a dog by a Sub-breed alone. Breed must not be null or Empty!");
            }

            if (breedVal && subBreedVal)
            {
                _base = "https://dog.ceo/api/breeds/image/random";
            }
            else if (!breedVal && subBreedVal)
            {
                _base = $"https://dog.ceo/api/breed/{breed}/images/random";
            }
            else
            {
                _base = $"https://dog.ceo/api/breed/{breed}/{subBreed}/images/random";
            }

            string json = await(await ApiRequester.RequestAPIAsync(_base, RequestType.Get)).Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <Dog>(json));
        }
Exemple #2
0
        /// <summary>
        /// Return multiple random dog images from a sub-breed, e.g. Afghan Hound
        /// </summary>
        /// <param name="breed"></param>
        /// <param name="subBreed"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static async Task <DogInfo> GetDogsOfSubAsync(string breed, string subBreed, int count)
        {
            if (string.IsNullOrWhiteSpace(breed))
            {
                throw new ArgumentException($"'{nameof(breed)}' cannot be null or whitespace.", nameof(breed));
            }

            if (string.IsNullOrWhiteSpace(subBreed))
            {
                throw new ArgumentException($"'{nameof(subBreed)}' cannot be null or whitespace.", nameof(subBreed));
            }

            string req = $"https://dog.ceo/api/breed/{breed}/{subBreed}/images/random/{count}";

            string json = await(await ApiRequester.RequestAPIAsync(req, RequestType.Get)).Content.ReadAsStringAsync();

            if (JsonError(json, out BadRequest request))
            {
                return new DogInfo()
                       {
                           Code    = request.Code,
                           Message = new string[] { "No information provided" },
                           Status  = request.Status
                       }
            }
            ;

            return(JsonConvert.DeserializeObject <DogInfo>(json));
        }
Exemple #3
0
        /// <summary>
        /// Get every single dog! | NOT IMAGES
        /// </summary>
        /// <returns></returns>
        public static async Task <DogList> GetAllAsync()
        {
            string json = await(await ApiRequester.RequestAPIAsync(ListAll, RequestType.Get)).Content.ReadAsStringAsync();

            _last = JsonConvert.DeserializeObject <DogList>(json);
            return(_last);
        }
Exemple #4
0
        /// <summary>
        /// Returns an array of all the images from a breed, e.g. hound
        /// </summary>
        /// <param name="breed"></param>
        /// <returns></returns>
        public static async Task <DogInfo> GetAllByBreedAsync(string breed)
        {
            string req = $"https://dog.ceo/api/breed/{breed}/images";

            string json = await(await ApiRequester.RequestAPIAsync(req, RequestType.Get)).Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <DogInfo>(json));
        }
Exemple #5
0
        /// <summary>
        /// Get a collection of random dogs.
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static async Task <DogInfo> GetRandomDogsAsync(int count)
        {
            string apiReq = DogCollection + count.ToString();

            string json = await(await ApiRequester.RequestAPIAsync(apiReq, RequestType.Get)).Content.ReadAsStringAsync();

            if (JsonError(json, out BadRequest request))
            {
                return new DogInfo()
                       {
                           Code    = request.Code,
                           Message = new string[] { "No information provided" },
                           Status  = request.Status
                       }
            }
            ;

            return(JsonConvert.DeserializeObject <DogInfo>(json));
        }