Esempio n. 1
0
        public async Task <List <string> > GetImagesAsync(string searchQuery, int page = 0, int perPage = 20)
        {
            try
            {
                page++;

                var imageQueryBuilder = new ImageQueryBuilder {
                    Query = searchQuery, Page = page, PerPage = perPage
                };
                var imageResult = await _pixabaySharpClient.QueryImagesAsync(imageQueryBuilder);

                return(imageResult.Images.Select(i => i.PreviewURL).ToList());
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"{GetType().Name} | {nameof(GetImagesAsync)} | {ex}");
            }

            return(new List <string>());
        }
Esempio n. 2
0
        /// <summary>
        /// Complex search for images.
        /// </summary>
        /// <param name="qb"><see cref="ImageQueryBuilder"/> instance to search for</param>
        /// <returns><seealso cref="ImageResult"/></returns>
        public async Task <ImageResult> QueryImagesAsync(ImageQueryBuilder qb)
        {
            var s = qb.ToString();

            return(await _httpHelper.GetRequestAsync <ImageResult>($"{s}").ConfigureAwait(false));
        }
        public void ImageQueryBuilderTest()
        {
            var qb = new ImageQueryBuilder();

            qb.Id = "123";
            Assert.AreEqual("&id=123", qb.ToString());

            qb.Language = Language.DE;
            Assert.AreEqual("&lang=de&id=123", qb.ToString());

            qb.Query = "yellow flower";
            Assert.AreEqual("&q=yellow+flower&lang=de&id=123", qb.ToString());

            foreach (Category category in Enum.GetValues(typeof(Category)))
            {
                qb.Category = category;
                Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category={category.ToString().ToLower()}", qb.ToString());
            }
            qb.Category = Category.Business;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business", qb.ToString());

            qb.MinWidth = 100;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100", qb.ToString());

            qb.MinHeight = 100;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100", qb.ToString());

            qb.IsEditorsChoice = true;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true", qb.ToString());

            qb.IsEditorsChoice = true;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true", qb.ToString());

            qb.IsSafeSearch = true;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true", qb.ToString());

            qb.Order = Order.Latest;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=latest", qb.ToString());
            qb.Order = Order.Popular;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular", qb.ToString());

            qb.Page = 2;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2", qb.ToString());

            qb.PerPage = 5;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5", qb.ToString());


            qb.ResponseGroup = ResponseGroup.HighResolution;
            Assert.AreEqual("&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=high_resolution", qb.ToString());
            qb.ResponseGroup = ResponseGroup.ImageDetails;
            Assert.AreEqual("&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=image_details", qb.ToString());

            foreach (ImageType type in Enum.GetValues(typeof(ImageType)))
            {
                qb.ImageType = type;
                Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=image_details&image_type={type.ToString().ToLower()}", qb.ToString());
            }
            qb.ImageType = ImageType.All;
            Assert.AreEqual("&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=image_details&image_type=all", qb.ToString());
            qb.ImageType = ImageType.Illustration;
            Assert.AreEqual("&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=image_details&image_type=illustration", qb.ToString());
            qb.ImageType = ImageType.Photo;
            Assert.AreEqual("&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=image_details&image_type=photo", qb.ToString());
            qb.ImageType = ImageType.Vector;
            Assert.AreEqual("&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=image_details&image_type=vector", qb.ToString());

            foreach (Orientation orientation in Enum.GetValues(typeof(Orientation)))
            {
                qb.Orientation = orientation;
                Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=image_details&image_type=vector&orientation={orientation.ToString().ToLower()}", qb.ToString());
            }
            qb.Orientation = Orientation.Vertical;
            Assert.AreEqual($"&q=yellow+flower&lang=de&id=123&category=business&min_width=100&min_height=100&editors_choice=true&safesearch=true&order=popular&page=2&per_page=5&response_group=image_details&image_type=vector&orientation=vertical", qb.ToString());
        }